简体   繁体   中英

How do I get the item from each row of a listview?

I wanna get the data in the clicked row in listView.

This is my code , I want to access to "ID" Value in each row od list View.

How do I get the item from each row of a listview?

How do I get the item from each row of a listview?

public class ActivityBookMark extends Activity {

private SQLiteDatabase database;
private ListView lv;
private int ID;
public ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.book_mark);
    lv = (ListView) findViewById(R.id.BookMarkListView);
    Animation animation = AnimationUtils.loadAnimation(this,
            R.anim.translate);
    lv.setLayoutAnimation(new LayoutAnimationController(animation));
    ArrayList<String> data = geData();
    adapter = new ArrayAdapter<String>(this, R.layout.bookmark_item,
            R.id.TXTbookmarkItem, data);
    lv.setAdapter(adapter);
}

public ArrayList<String> geData() {
    // TODO Auto-generated method stub
    database = SQLiteDatabase.openDatabase(DBHelper.DB_PATH
            + DBHelper.DB_NAME, null, 0);
    Cursor localCursor = database
            .rawQuery(
                    "SELECT `rowid`,* FROM `TABLE1` WHERE 1=1 AND `FAV` LIKE '%TRUE%' ORDER BY `rowid` ASC LIMIT 0, 50000",
                    null);
    ArrayList<String> result = new ArrayList<String>();
    int TITLE = localCursor.getColumnIndex("TITLE");
  //↓ i want to access ID value onto OnClickUI method 
    ID = localCursor.getColumnIndex("ID");
    for (localCursor.moveToFirst(); !localCursor.isAfterLast(); localCursor
            .moveToNext()) {
        result.add(localCursor.getString(TITLE));
    }
    return result;
}

public void OnClickUI(View v) {
    switch (v.getId()) {
    case R.id.DelFromBookMark: {
       //Code to retrive ID of each Row
 .... .

 Toast.makeText(ActivityBookMark.this,v.getViewTreeObserver()+"",      Toast.LENGTH_SHORT).show();
        database.execSQL("UPDATE TABLE1 SET FAV='FALSE' WHERE ID=" + ID
                + "");
        List<String> newItems = geData();
        adapter.clear();
        adapter.addAll(newItems);
        adapter.notifyDataSetChanged();
        Toast.makeText(getApplicationContext(),
                "از لیست نشان شده ها حذف شد!", Toast.LENGTH_SHORT).show();

    }

        break;

    default:
        break;
    }
}

}

You should implement onItemclicklistner

   lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            //in this method you get position and view of the item clicked the list


      }
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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