在listView中获取选定的值

时间:2015-01-24 07:24:46

标签: android listview android-listview

我在android中有两个列表。其中一个是标题,另一个是ID。我在listView中显示了标题列表。每个标题都有自己的ID。我想得到我检查的每个标题的ID。我如何获得每个标题的ID?

 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_remark);

 context = getApplicationContext();

    sh = new SchemaHelper(context);

 // -- Display mode of the ListView
 ListView listview= getListView();
 // listview.setChoiceMode(listview.CHOICE_MODE_NONE);
 // listview.setChoiceMode(listview.CHOICE_MODE_SINGLE);
 listview.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);

 //--   text filtering
 listview.setTextFilterEnabled(true);


    final List<String> textList= new ArrayList<String>();

    final List<String> idList= new ArrayList<String>();

    Cursor c= sh.getAllRemark();

    for(int i=0; c.moveToNext();i++){
        String caption= c.getString(c.getColumnIndex(P_RemarkTable.REM_CAPTION.toString()));
        String id= c.getString(c.getColumnIndex(P_RemarkTable.REM_ID.toString()));
        textList.add(caption);
        idList.add(id);


 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, textList);
    listview.setAdapter(adapter);

 listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
     // idList.get(i).toString()
     public void onItemClickListener(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(getApplicationContext(),idList.get(i).toString() , Toast.LENGTH_SHORT).show();
        // barrierID=Integer.parseInt(idList.get(i));
        }

     public void onItemChecked(AdapterView<?> adapterView, View view, int i, long l){

         Toast.makeText(getApplicationContext(), idList.get(i).toString(), Toast.LENGTH_SHORT).show();

     }

     public void onListItemClick(AdapterView<?> adapterView, View view, int i, long l){

        // super.onListItemClick(adapterView, view, i, l);

         Toast.makeText(getApplicationContext(),idList.get(i).toString(), Toast.LENGTH_SHORT).show();
     }

     @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(getApplicationContext(), idList.get(i).toString(), Toast.LENGTH_SHORT).show();
        // barrierID=Integer.parseInt(idList.get(i));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            // do nothing
        }
    });

 }
}

}

此方法不起作用。 我该怎么办?

0 个答案:

没有答案