我想从ListView
删除所选项目,但我不能。我尝试了很多方法,但它们并不适合我。
这是我的适配器列表 - 项目来自SD卡。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
FindFilesByType.this, android.R.layout.test_list_item,
Ringtones);
如何解决这个问题?
答案 0 :(得分:3)
您可以从基础列表中删除该项,并通知适配器,数据集已更改。
请查看this method示例。
答案 1 :(得分:2)
实现onitemclickListener并获取在arrayadapter中单击的项目(获取ID)然后调用 arrayList.remove([ID]); adapter.notifyDataSetChanged();
答案 2 :(得分:1)
@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();
}
}
}