简体   繁体   English

Android:使用ListAdapter从TextView获取特定文本

[英]Android: Getting a specific text from a TextView with a ListAdapter

I have a ListView , with three TextViews in each row, that is populated from a Database. 我有一个ListView ,每行有三个TextViews ,这是从数据库填充的。 The first TextView contains the _id . 第一个TextView包含_id When I click on an item, an AlertDialog pops up asking if I want to delete the item. 当我单击一个项目时, AlertDialog弹出一个AlertDialog ,询问我是否要删除该项目。 How do I extract the information from a specific TextView , in order to update the Database accordingly? 如何从特定的TextView提取信息,以便相应地更新数据库?

Here is the code snippet I have so far: 这是我到目前为止的代码片段:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage("Delete this reminder?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //I need the _id to remove the selected item.
                    db.deleteContact();
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();
      }
});

Thanks! 谢谢!

Easiest would be if you got the information from the database that is driving the adapter. 最简单的方法是从驱动适配器的数据库中获取信息。 You already have the position of the clicked item and the data from your database. 您已经有了单击项的位置以及数据库中的数据。 Another possibility is in the onClick method to do 另一种可能性是在onClick方法中做

TextView tv = (TextView) view.findViewById(R.id.yourFirstTextView); 
String id = tv.getText().toString(); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM