2012-03-07 45 views
0

buttononclicklistener不工作,如何从列表如何从列表视图中使用的CursorAdapter

public void onClick(View arg0) { 
    //TestDBAdapter.deleteEntry(itemId); 
    TestDBAdapter.delete("TABLE_NAME", "_id="+itemId, null);      
    Toast.makeText(DisplayActivity.this, "you want delete this item", Toast.LENGTH_SHORT).show(); 
    cursor.requery(); 
    updateList(); 
    notifyDataSetChanged(); 
} 
+0

Post'TestDBAdapter'源代码 – Caner 2012-03-07 10:23:10

+0

请参阅此链接,它可能会帮助你,http://android-er.blogspot.in/2011/06/edit-row-in-sqlite-database-using.html http: //anujarosha.wordpress.com/2012/01/06/how-to-update-and-delete-data-from-sqlite-database-in-android/ – Aerrow 2012-03-07 10:20:17

回答

1

删除所选的项目尝试这样的删除选中的项目,,

@Override 
public void bindView(View view, Context context, final Cursor cursor) { 

    TextView txtName = (TextView) view.findViewById(R.id.txt_name); 
    txtName.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_username))); 
    TextView txtPassword = (TextView) view.findViewById(R.id.txt_password); 
    txtPassword.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_password))); 

final String itemId = cursor.getString(cursor.getColumnIndex("id")); 

    Button button = (Button) view.findViewById(R.id.btn_delete); 
    button.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      Log.d(TAG, "Button Click "); 
      deleteRecordWithId(itemId); 
      cursor.requery(); 
      notifyDataSetChanged(); 
     } 
    }); 
} 

也提到这个环节,How to remove a selected item from ListView using CursorAdapter

+0

坏主意。 'cursor.requery()'现在已被弃用 – R00We 2014-12-16 05:25:05

0

在CursorAdapter支持的ListView中刷新数据的其他可能性是创建一个新的游标并在CursorAdapter上调用swapCursor。

cursor = newCursor(); 
    ((CursorAdapter) getListAdapter()).swapCursor(cursor); 

希望这可以提供帮助。

相关问题