2013-10-04 33 views
3

我很困惑,我想在deleting any item後刷新listview。我只是使用intentcall the same activity,但它不能工作。任何其他技巧或想法,然後請建議。 my sample code低於:列表視圖更新和刪除選項通過contextmenu

update_page.java

protected Object adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
     { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.update_list); 
      final ProgressDialog myPd_ring=ProgressDialog.show(update_page.this, "Please wait", "Login please wait..", true); 
      myPd_ring.setCancelable(true); 
      myPd_ring.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      new Thread() { 

       public void run() { 

       try{ 

       sleep(1000); 

       } catch (Exception e) { 

       Log.e("tag", e.getMessage()); 

       } 

       // dismiss the progress dialog 

       myPd_ring.dismiss(); 

       } 

       }.start(); 


      myview = (ListView)this.findViewById(R.id.list); 
      ctx=this; 





      database = act.getInstance().openOrCreateDatabase("contactsManager", 
      SQLiteDatabase.CREATE_IF_NECESSARY, null);  
      Toast.makeText(getApplicationContext(), "Database is open", 1500).show(); 


     // database=this.openOrCreateDatabase("contactsManager", SQLiteDatabase.CREATE_IF_NECESSARY,null); 

      final String[] columnsone={"_id","task_name","date_time"}; 
      String[] columnstwo={"task_name","date_time"}; 
      int to[] = {R.id.lbl_task,R.id.lbl_datetime}; 
      c = database.query("contacts", columnsone, null, null, null, null, null); 
      status=c.getCount(); 

      Toast.makeText(getApplicationContext(), "totle task :" +status, 1500).show(); 



      if(c!=null) 
       { 

       Toast.makeText(getApplicationContext(), "in courser", 1500).show(); 
       SimpleCursorAdapter adapter=new 
         SimpleCursorAdapter(update_page.this,R.layout.update_listview, c, columnstwo, to); 
       Toast.makeText(getApplicationContext(), "records are in list", 1500).show(); 

       myview.setAdapter(adapter); 


         myview.setOnItemLongClickListener(new OnItemLongClickListener() 
         { 

          public boolean onItemLongClick(AdapterView<?> arg0, 
            View record, int arg2, long arg3) 
          { 


           update_page.this.registerForContextMenu(record); 
           /*task= ((TextView)record.findViewById(R.id.lbl_task)).getText().toString(); 
           date_time1= ((TextView)record.findViewById(R.id.lbl_datetime)).getText().toString(); 
           Toast.makeText(getApplicationContext(), "task is"+task, 1500).show(); 
           database = act.getInstance().openOrCreateDatabase("contactsManager", 
             SQLiteDatabase.CREATE_IF_NECESSARY, null);  

           String query = "SELECT _id FROM contacts WHERE task_name='"+task + "&date_time="+date_time1+"'"; 


           c=database.rawQuery(query, null); 
           c.moveToNext(); 

           new_id=c.getInt(c.getColumnIndex("_id")); 
           */ 

           return false; 
          } 

         }); 
       } 
      } 
      //@Override 
      public boolean onContextItemSelected(MenuItem item) 
      { 
       AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); 
       int choice = item.getItemId(); 
       switch(choice) 
       { 
       case 1: 
        Toast.makeText(getApplicationContext(), "Update Task", 2000).show(); 

        /*Intent op_intent = new Intent(update_page.this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

        op_intent.putExtra("task_id", new_id); 
        op_intent.putExtra("task", task); 
        op_intent.putExtra("datetime", date_time1); 

        update_page.this.startActivity(op_intent);*/ 

        break; 

       case 2: 


        Toast.makeText(getApplicationContext(), "position :" +new_id , 2000).show(); 

        String table_name = "contacts"; 
        String where = "task_name='"+task+ "&date_time="+date_time1+"'"; 
        String[] whereArgs = null; 
        database.delete(table_name, where, whereArgs); 
        //over 

         c.close(); 
         database.close(); 
         Toast.makeText(getApplicationContext(), "Task Deleted Successfully", 1500).show(); 

         update_page.this.startActivity(new Intent(update_page.this, update_page.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 

        break; 

       } 

       return super.onContextItemSelected(item); 
      } 


      @Override 
      public void onCreateContextMenu(ContextMenu menu, View v, 
        ContextMenuInfo menuInfo) 
      { 
       menu.add(0,1,0,"Update Task"); 
       menu.add(0,2,0,"Delete"); 

       super.onCreateContextMenu(menu, v, menuInfo); 
      } 


    } 
+0

這將有助於http://www.vogella.com/articles/AndroidSQLite/article.html –

+0

只關注「4.教程:使用SQLite」,請不要關注「5.內容提供者和共享數據」。 –

+0

@ M-WaJeEh:謝謝你,先生,我會嘗試它。:-) – Radhey

回答

0

你可以嘗試的onResume()方法,並通知您列出。

@Override 
public void onResume() { 
    //set your adapter or referesh 
    super.onResume(); 
} 
+0

yup bro,我認爲這似乎很好..我會嘗試:) thnx爲建議@Elesh – Radhey

+0

感謝和enjoy.if可能我的答案請投票.. .. :) –

2

當您完成對列表視圖中項目的刪除操作時,您必須調用適配器的adapter.notifydatasetchanged()