1

我有个问题。在我的活动中,我有一个 ListView,其值是从数据库编译的。数据库返回三列:_Id、AccountName 和 Comments。我想要的是,当用户单击 listView 中的一个项目时,将提取该项目的位置,然后将该位置与具有数据的 ArrayList 中的位置进行比较,然后将特定字段和行找出来,_Id字段会被取出来,以String的形式传递给另一个activity。你可能不太了解;这可能会有所帮助:

public void search(View view){
    final ListView vLst_IDCommName=(ListView)findViewById(R.id.lst_IDCommName);
    EditText vTxt_searchTxt=(EditText)findViewById(R.id.search_txt);
    String srch_str= vTxt_searchTxt.getText().toString();

    if(srch_str.length()>0){
        //The DataSource for the LstView
        List<Comment> values = datasource.getContacts(srch_str);

        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
        vLst_IDCommName.setAdapter(adapter);
    }
    else{
        Toast.makeText(getBaseContext(), "Please type in a value to proceed!", Toast.LENGTH_LONG).show();
    }

    vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            //Here I want to create a function that will take the position of the item clicked
            //and then searched for that position in the ArrayList and then return the _Id of 
            //the account whose value was clicked.

    }});  
}

我用来获取数据的这段代码:

public List<Comment> getContacts(String search_str) { 
    List<Comment> comments = new ArrayList<Comment>(); 
    String srch_str=(String) search_str; 
    Cursor cur_comments= database.rawQuery("Select * from comments where acc_name like('%"+srch_str+"%')"+" "+ "or comment like('%"+srch_str+"%')", null); 
    cur_comments.moveToFirst(); 
    while (!cur_comments.isAfterLast()) { 
        Comment comment = cursorToComment(cur_comments); 
        comments.add(comment); cur_comments.moveToNext(); 
        } 
    cur_comments.close(); 
    return comments; 
} 

对于任何反馈,我们都表示感谢。

4

2 回答 2

0

你可以试试

 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    String currentId=yourArrayList.get(position).toString();
         }
于 2013-07-29T08:02:47.980 回答
0
vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
      Object s=parent.getItemAtPosition(position);
      String j=s.toString();
}});  
于 2013-07-29T09:09:53.573 回答