如何获取ListView中单击项的ID?

时间:2017-04-09 16:22:20

标签: android-layout listview

我正在尝试使用Contextual Action Bar从数据库中删除。当我实现OnItemLongClickListener来检索所单击项的id时,它返回了位置值。这是代码。 这是我的第一个问题,请原谅任何输入错误

public class ChecklistActivity extends AppCompatActivity{
ListDBHelper mHelper;
private long currentListItemIndex;
public int ids;
private CLAdapter mAdapter;
public ArrayList<String> taskList;
ListView lv;
private static final String TAG = "CheckListActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mHelper=new ListDBHelper(this);
    setContentView(R.layout.activity_checklist);
    mAdapter=new CLAdapter(this,R.layout.activity_checklist_item,R.id.check_textView,taskList);

    lv=(ListView) findViewById(R.id.item_listView);
    final Bundle extras=getIntent().getExtras();
    if(extras!=null){
        ids=(int)extras.getLong("key_id");
    }
    updateUI((int)extras.getLong("key_id"));
    Button addButton =(Button)findViewById(R.id.addButton);
    addButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            EditText addTask =(EditText)findViewById(R.id.addTask);
            String list = String.valueOf(addTask.getText());
            SQLiteDatabase db = mHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(ListContract.CheckListEntry.COLUMN_NAME_TITLE, list);
            values.put(ListContract.CheckListEntry.COLUMN_LIST_KEY,(int)extras.getLong("key_id"));
            db.insertWithOnConflict(ListContract.CheckListEntry.TABLE_NAME,
                    null,
                    values,
                    SQLiteDatabase.CONFLICT_REPLACE);
            db.close();
            addTask.setText("");
            updateUI((int)extras.getLong("key_id"));

        }
    });
   lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
       @Override
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
           currentListItemIndex=id;
           lv.setItemChecked(position, !mAdapter.isPositionChecked(position));
           return false;
       }

   });}
  lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE_MODAL);
    lv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        private int nr = 0;
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (checked) {
                nr++;
                mAdapter.setNewSelection(position, checked);
            } else {
                nr--;
                mAdapter.removeSelection(position);
            }
            mode.setTitle(nr + " selected");

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Remove Item");
            nr = 0;
            mode.getMenuInflater().inflate(R.menu.checklist_cab_menu,menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.action_delete_item:
                    nr = 0;

                    deleteRow((int)currentListItemIndex);
                    mAdapter.notifyDataSetChanged();
                    mode.finish();
                    return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
}


public void updateUI(int keyid){

    ListDBHelper mHelper=new ListDBHelper(this);
    SQLiteDatabase db= mHelper.getReadableDatabase();
    Cursor c= db.rawQuery("SELECT CheckListEntry.*,CheckListEntry._id as _id FROM CheckListEntry WHERE "+
             "CheckListEntry.ListKey == "+keyid+";",null);
    ArrayList<String> taskList=new ArrayList<>();
    while(c.moveToNext()){
        int idx=c.getColumnIndex(ListContract.CheckListEntry.COLUMN_NAME_TITLE);
        taskList.add(c.getString(idx));
        Log.d(TAG,"cursorRead"+c.getString(idx));
    }
        mAdapter = new CLAdapter(this,
                R.layout.activity_checklist_item,
                R.id.check_textView,
                taskList);
        lv.setAdapter(mAdapter);

    }

public void deleteRow(int id){
    ListDBHelper mHelper = new ListDBHelper(this);
    SQLiteDatabase db= mHelper.getWritableDatabase();
    db.delete(ListContract.CheckListEntry.TABLE_NAME,"_id =="+id+";",null);
    updateUI(ids);
}
}

1 个答案:

答案 0 :(得分:0)

尝试view.getId()获取所点击视图的ID。 id参数是行ID。请参阅AdapterView.OnItemLongClickListener

  

<强> onItemLongClick

     

boolean onItemLongClick(AdapterView parent,                   查看视图,                   int位置,                   长id)

     

单击并保持此视图中的项目时要调用的回调方法。如果需要访问与所选项目关联的数据,实施者可以调用getItemAtPosition(position)。

     

参数

     

父AdapterView:发生点击的AbsListView

     

视图视图:单击的AbsListView中的视图

     

position int:列表中视图的位置

     

id long:已点击的项目的行ID