简体   繁体   English

单击ListView项时将_id从一个活动传递到另一个活动

[英]Passing an _id from one activity to another when ListView item is clicked

I have a problem. 我有个问题。 In my activity i have a ListView whose values are compiled from a Database. 在我的活动中,我有一个ListView,其值是从数据库编译的。 The database returns three columns: _Id, AccountName and Comments. 数据库返回三列:_Id,AccountName和Comments。 What i want is that the when the user clicks on one of the items in the listView, that Item's position will be extracted and then that position will be compared with the postion in the ArrayList which has the data and then when the specific field and row is found out, the _Id field will be taken out and passed to another activity in the form of a String. 我想要的是,当用户单击listView中的一个项目时,将提取该项目的位置,然后将该位置与具有数据的ArrayList中的位置进行比较,然后在特定字段和行中进行比较如果找到,_Id字段将被取出并以字符串形式传递给另一个活动。 You may not have understood much; 您可能了解得不多; this might help: 这可能会有所帮助:

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.

    }});  
}

This code I used to get data: 我用来获取数据的这段代码:

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; 
} 

Any feedback would be appreciated. 对于任何反馈,我们都表示感谢。

you can try 你可以试试

 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    String currentId=yourArrayList.get(position).toString();
         }
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();
}});  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从listview中的单击项传递到另一个活动时出错 - Error while passing data from clicked item in listview to another activity 单击ListView中的项目时在另一个活动中显示列表 - Showing a List in another activity when clicked on an item in ListView 加载新活动时,将选定项从ListView Android传递到另一个活动会引发NullPointerException - Passing selected item from ListView Android to another Activity throws a NullPointerException when loading the new activity 将项目从另一个活动添加到列表视图(仅添加一个项目) - Add item to listview from another activity (it only adds one item) 单击展开式列表视图中的子项时如何在另一个活动中打开列表视图 - How to open a listview in another activity when a child item in an expandable listview is clicked 通过可点击的 ListView 将 Firebase 数据从一个活动传递到另一个活动 - Passing firebase data from one activity to another via clickable ListView 在ArrayList项目上单击将数据从一个活动传递到另一个活动 - passing data from one activity to another on ArrayList Item click 单击项目时从列表视图中删除项目 - Delete item from listview when the item is clicked 将数据从列表视图传递到另一个活动的edittext - Passing data from listview to edittext of another activity 当我从另一个活动获得结果时,ListView仅更新一个视图项,如何获得所有过去的结果? - ListView is updating only one view item when I get the results from another activity, how do I get all the past results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM