关于setOnItemClickListener(new OnItemClickListener()

时间:2017-12-01 08:19:57

标签: android

我的数据库数据存储在列表视图中,当我点击其中一个列表视图时,如何根据参数“position”获取每个项目?

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
    }
});

3 个答案:

答案 0 :(得分:0)

你可以通过 - :

来做到
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
           case 0:
            break;
          case 1:
            break;

        }
    }
});

但是当我们在listview中有很多项目时,这将是复杂的。 感谢

答案 1 :(得分:0)

试试这个......

此示例将帮助您...

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

for more

答案 2 :(得分:0)

如果您在listview中设置数据库数据,如下所示

ArrayAdapter<Object> mDatabaseData = new ArrayAdapter<Object>(this,android.R.layout.simple_expandable_list_item_2, array);
lv.setAdapter(mDatabaseData);

然后您可以根据参数“position”获取每个项目,如下所示

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
  // When clicked, get data for selected position
   Object selectedItem=mDatabaseData.get(position);
   }
});