2011-11-30 40 views
0

我知道數據被插入到數據庫中,並且它已正確列出,但notifydatasetchanges不起作用,列表視圖不會刷新,所以我必須重新啓動我的活動....notifyDataSetChanged不會被觸發,即使我調用了兩次

Cursor c = db.selectAll(); 
      final ListView lv = (ListView) findViewById(R.id.listView1); 
      adapter=new MyContactsAdapter(this, c); 
      lv.setAdapter(adapter); 
      db.insertInTable("asd","asd"); 
      //insertion in table works, I am sure, cause when I start the activity the new values are listed 

    //this the part that doesn't works 
     ((MyContactsAdapter)lll.getAdapter()).notifyDataSetChanged(); 
        adapter.notifyDataSetChanged(); 
        lll.refreshDrawableState(); 





    private class MyContactsAdapter extends CursorAdapter{ 
      private Cursor mCursor; 
      private Context mContext; 
      private final LayoutInflater mInflater; 

      public MyContactsAdapter(Context context, Cursor cursor) { 
       super(context, cursor, true); 
       mInflater = LayoutInflater.from(context); 
       mContext = context; 
      } 

      @Override 
      public void bindView(View view, Context context, Cursor cursor) { 
       TextView t = (TextView) view.findViewById(R.id.textView2); 
       t.setText(cursor.getString(0)); 

       t = (TextView) view.findViewById(R.id.TextView01); 
       t.setText(cursor.getString(1)); 

      } 

      @Override 
      public View newView(Context context, Cursor cursor, ViewGroup parent) { 
       final View view = mInflater.inflate(R.layout.item, parent, false); 
       return view; 
      } 
     }//adapter 
+0

什麼是同 – Lukap

回答

0

好吧,有點像,從你的代碼,

Cursor c; 
     @Override 
     onCreate(.... {  

     setContentView(...); 
     final ListView lv = (ListView) findViewById(R.id.listView1); 
     db.insertInTable("asd","asd"); 
     c = db.selectAll(); 
     adapter=new MyContactsAdapter(this, c); 
     lv.setAdapter(adapter); 

     // some button click... 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

      db.insertInTable("xyz","xyz"); 
         c = db.selectAll(); 

      adapter.notifyDataSetChanged(); 
      } 
     }); 

試試這個,讓我們看看會發生什麼......

+0

我得到了和我一樣代碼插入後,我有adapter.notifyDataSetChanged();但沒有得到更新,我有一個感覺,我的適配器有一些錯誤,這阻止了視圖刷新...... – Lukap

+0

你必須在插入數據後檢查遊標的值也將調試點放在你的適配器的bindView( )或newView()如果notifyDataSetChanged()被調用,那麼你的執行指針來自那個方法。 – user370305

相關問題