Remove selected item from ListView
I want to delete the selected item from the ListView
but I can't. I've tried a lot of methods but they aren't working for me.
This is my adapter list- items are coming from the SD Card.
ArrayAdapter<String> adapter = new ArrayAdapter开发者_运维知识库<String>(
FindFilesByType.this, android.R.layout.test_list_item,
Ringtones);
How do I solve this problem?
You can remove the item from the underlying list and signal the adapter, that the dataset has changed.
Have a look at this method for an example.
Implement the onitemclickListener and get the item(get ID) clicked in the arrayadapter and then call arrayList.remove([ID]); adapter.notifyDataSetChanged();
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=cntx.getLayoutInflater();
View row=inflater.inflate(R.layout.bookmark_row,null);
TextView tv=(TextView)row.findViewById(R.id.txtToc);
final TextView tv2=(TextView)row.findViewById(R.id.txtPgNo);
mCursor.moveToPosition(position);
System.out.println("Count Cursor"+mCursor.getCount());
if(mCursor.getCount()<=0)
{
tv.setText("No Bookmark Found");
}
else
{
tv.setText(mCursor.getString(1).toString());
tv2.setTag(mCursor.getString(0).toString());
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println(position);
db_conn.onDelete("tab_book", mCursor.getColumnName(0),Integer.parseInt((String) tv2.getTag()));
// Toast.makeText(ctx,"Bookmark Deleted", Toast.LENGTH_SHORT).show();
mToastTextView=new toastTextview(0, 0, ctx,listAct);
mToastTextView.showMessage("Bookmark Deleted");
refresh();
}
});
row.setTag(mCursor.getString(2).toString());
}
return row;
}
public void refresh()
{
mCursor=db_conn.onQueryGetCursor("tab_book",mItems,null, null, null, null, null);
mCursor.moveToPosition(0);
notifyDataSetChanged();
if(mCursor.getCount()<=0)
{
// Toast.makeText(ctx, "No Bookmark", Toast.LENGTH_SHORT).show();
try {
mFlingAct.dialogBookmark.dismiss();
} catch (Exception e) {
// TODO: handle exception3
e.printStackTrace();
}
}
}
精彩评论