Search code examples
androidandroid-listviewandroid-arrayadaptertextview

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. The first TextView contains the _id. When I click on an item, an AlertDialog pops up asking if I want to delete the item. How do I extract the information from a specific TextView, in order to update the Database accordingly?

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!


Solution

  • 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

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