1. Are you ready for the Galaxy S20? Here is everything we know so far!

Listview updating item issues

Discussion in 'Android Development' started by frappydan, Apr 24, 2021.

  1. frappydan

    frappydan Lurker
    Thread Starter

    Have a listview with custom rows. 1 imageview and several textviews. My arrayAdapter works perfectly populating the list. When I click on an item I start an activity (for result) to view/modify the item. If it was modified I get a reference to each of the views in the item using findViewById(bla) and I update them using setText, setImageResourceID, etc... This works while the view I just updated is in sight. However, when scrolled out of view the Icon on my listview item goes into green spaz-mode. when scrolled back into view.

    I should probably mention that I'm sing a holder process to smooth scroll my listview. Not sure if this is the problem or if it's unrelated. Been trying to find articles on how to clear my listview after an edit and reload my existing data using the same source and arrayadapter. Not having any luck finding out how to do this. Any ideas out there? Went from a 10 year windows programmer to android 3 months ago. Finding the transition unsettling so far but determined. Help?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Cao Van Thanh

    Cao Van Thanh Well-Known Member

    Hi
    1. you need share your record off screen to show the behaviour.
    2. you need share you listview setup code
     
  3. frappydan

    frappydan Lurker
    Thread Starter

    This is after updating the item in another activity and returning a result. The code for attempting to update the listview item is in the below onActivityResult text. What I want to do (but can't find help on) is to simply update the original data source and then update the listview item directly. I'm starting to think this isn't possible. As a matter of fact, if I scroll down to a listview item that's below and try to edit it, the proper data is passed on to the activity called, but the item is never updated in the listview at all. Guessing the listview doesn't call the actual item that I edited.

    The listview and adapter work perfectly when starting it all up. Scrolling and all are great. It's after I try to update an item that things go south.

    To repeat myself.
    1. Click on item is listview
    2. Edit the data in the activity called (for result)
    3. Update my original data used to populate the listview
    4. Update what's displayed in the listview to reflect my changes

    The third item in the list below is fine until it's scrolled off the screen. Tried this with a holder-arrayAdapter and then with a standard arrayAdapter,
    Still stuck on windows listviews so this is rather new to me how the adapters work.
    I also have tried notifying the adapter of changes but I've read that that only works if you insert or delete from the original data.

    at this point I'd settle for clearing the listview and loading the data again. setting arrayAdapter is suppose to be a bad thing to do, I hear. Lots more open help out there when I was a windows programmer.

    issue1.jpg



    Code (Java):
    1.     @Override
    2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    3.         super.onActivityResult(requestCode, resultCode, data);
    4.         if (requestCode == 501 && resultCode == RESULT_OK) {
    5.  
    6.             DecimalFormat f = new DecimalFormat("000,000");
    7.  
    8.             int locid = data.getIntExtra("locationid", 1);
    9.             int id = data.getIntExtra("id", -1);
    10.             int pos = data.getIntExtra("position", -1);
    11.             int mil = data.getIntExtra("mileage", 0);
    12.             double cos = data.getDoubleExtra("cost", 0);
    13.             String typ = data.getStringExtra("type");
    14.             String tit = data.getStringExtra("title");
    15.             String des = data.getStringExtra("description");
    16.             String dat = data.getStringExtra("date");
    17.  
    18.             ImageView icon = (ImageView) v.findViewById(R.id.rowIcon);
    19.             TextView cat = (TextView) v.findViewById(R.id.rowCategory);
    20.             TextView desc = (TextView) v.findViewById(R.id.rowDescription);
    21.             TextView mileage = (TextView) v.findViewById(R.id.rowMileage);
    22.             TextView date = (TextView) v.findViewById(R.id.rowDate);
    23.             TextView cost = (TextView) v.findViewById(R.id.rowCost);
    24.             TextView loc = (TextView) v.findViewById(R.id.rowLocation);
    25.  
    26.             LocalDateTime lt = convert.DateTimeFromString(dat);
    27.  
    28.             icon.setImageResource(getIconIndex(typ));
    29.             cat.setText(typ);
    30.             items[ItemPosition].setCategory(typ);
    31.  
    32.             desc.setText(des);
    33.             items[ItemPosition].setDescription(des);
    34.  
    35.             date.setText(dat);
    36.             items[ItemPosition].setEntryDate(dat);
    37.  
    38.             mileage.setText(f.format(mil));
    39.             items[ItemPosition].setMileage(mil);
    40.  
    41.             date.setText(convert.DateAndTimeValuesAsString(lt));
    42.             items[ItemPosition].setEntryDate(dat);
    43.  
    44.             loc.setText(String.valueOf(locid));
    45.             items[ItemPosition].setLocationID(1);
    46.  
    47.             cost.setText(convert.CurrencyAsText(cos));
    48.             items[ItemPosition].setCost(cos);
    49.         }
    50.     }
    51.  
     
  4. Cao Van Thanh

    Cao Van Thanh Well-Known Member

    Long time I not use listview, I use recycle view. But they have same behaviour, you have to call adapter.notifyDataSetChanged() after the list modified to make list rerender.
     
  5. Cao Van Thanh

    Cao Van Thanh Well-Known Member

    If this not work, you have to make sure that you don't create new Adapter after call listView.setAdapter(adapter);
    Only use 1 adapter instance which linked to listView.
     
  6. frappydan

    frappydan Lurker
    Thread Starter

    I've solved both issues. Actually found that the String value that I was using to select my icon in my arrayadapter was appearantly getting a string with an extra space in it from the return data. This caused my ImgID variable that I was using to set the image to default to ic_launcher (which I set as default icon for testing). I changed the cat == "VALUE" to a cat.contains("VALUE") to avoid this issue.

    Code (Java):
    1.         int imgID = R.drawable.ic_launcher_background;
    2.         String cat = data.getCategory().toUpperCase();
    3.         if (cat.contains ("MAINTENANCE")) {
    4.             imgID = R.drawable.icon_engine;
    5.         } else if (cat.contains ("DOCUMENT")) {
    6.             imgID = R.drawable.icon_docs;
    7.         } else if (cat.contains ("FUEL")) {
    8.             imgID = R.drawable.icon_fuel;
    9.         } else if (cat.contains ("DRIVING")) {
    10.             imgID = R.drawable.icon_steeringwheel;
    11.         } else if (cat.contains ("BUSINESS")) {
    12.             imgID = R.drawable.icon_taxi;
    13.         }
    14.  
    As for updating the Listview Item I stripped most of my code out of the above code and simply created a new itemData using the returned data.

    Code (Java):
    1.     @Override
    2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    3.         super.onActivityResult(requestCode, resultCode, data);
    4.         if (requestCode == 501 && resultCode == RESULT_OK) {
    5.  
    6.             DecimalFormat f = new DecimalFormat("000,000");
    7.  
    8.             int locid = data.getIntExtra("locationid", 1);
    9.             int id = data.getIntExtra("id", -1);
    10.             int pos = data.getIntExtra("position", -1);
    11.             int mil = data.getIntExtra("mileage", 0);
    12.             double cos = data.getDoubleExtra("cost", 0);
    13.             String typ = data.getStringExtra("type");
    14.             String tit = data.getStringExtra("title");
    15.             String des = data.getStringExtra("description");
    16.             String dat = data.getStringExtra("date");
    17.  
    18.             LocalDateTime lt = convert.DateTimeFromString(dat);
    19.  
    20.             items[ItemPosition] = new itemData(id, des, mil, cos, "", typ, 1, 1, lt);
    21.             mAdapter.notifyDataSetChanged();
    22.         }
    23.     }
    24.  
    My ItemPosition variable stores the value of the item clicked on in the listview onclicklistener so that when a result is returned I can get the actual item that was clicked to edit. mAdapter.notifyDataSetChanged() does update the entire view after the original data has been modified. My issues were self-inflicted with the string comparison and not properly updating the original data. Live and learn, right?
     
  7. Cao Van Thanh

    Cao Van Thanh Well-Known Member

    You should compare String using stringA.equals(stringB) instead of use stringA.contains(stringB).
    At the time I begin learn Android, I got very much issues too.
     
Loading...

Share This Page

Loading...