简体   繁体   中英

Uniquely identify an item's element in ListView

can you help me how to find a certain item's textView. For example I have a listView with some items and every item is made of 3 textViews (name, price, description). Now I want to change the value of the "price" textView with and updateView(int index) method. How do I identify the correct textView in the correct item?

Let's say i have 5 items (product1, ..., product5). now i want to update the price of the 3rd product (product3). I call updateView(3) My updateView(int index) method so far

private void updateView(int index){
    // thats how I get the row
    View v = lvProduct.getChildAt(index); 
    // now I want to find the tvPrice in that row
    // but I'm not sure how to go about this
}

any tips would be great :D

The way I normally approach this would be to do the following:

  • Create a model for your list item (now days people prefer to use RecyclerView) - the model of course will have the fields/variables representing what you want to be displayed (like 3 Strings).
  • Now, to actually keep track of the item a user clicked, I can simply get the id of that item ( column of the table storing this item in the database) by calling a custom method in my adapter like getItem(int position) which returns an actual item.
  • That getItem method will return me an item with a getId() getter method.
  • Notice now that when you are creating your list items to pass to your custom adapter, your list item model MUST expect an (id) value in its constructor because that is how you will be able to access the actual value in your table to edit.
  • So then you query your table using your clicked item's id and update it and simply update the view automatically through your adapter.

I hope this helps!

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