Remove selected item from ListView

前端 未结 3 596
無奈伤痛
無奈伤痛 2020-12-21 13:53

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

相关标签:
3条回答
  • 2020-12-21 14:36
    @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();
                    }
    
                }
    
           }
    
    0 讨论(0)
  • 2020-12-21 14:37

    Implement the onitemclickListener and get the item(get ID) clicked in the arrayadapter and then call arrayList.remove([ID]); adapter.notifyDataSetChanged();

    0 讨论(0)
  • 2020-12-21 14:39

    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.

    0 讨论(0)
提交回复
热议问题