ListView does not show correct values ​​after scrolling

In my application, I use CustomListView with an ArrayAdapter to display the time of different countries. But after 6-7 lines (depending on the screen size of the phone), the time values ​​are repeated.

According to the previous post, I wrote the following code snippet to get a solution. But the problem still exists.

Below is the code I wrote:

  public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; Order o = items.get(position); if (v == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout ll = (LinearLayout) vi.inflate(R.layout.row, null); CustomDigitalClock customDC = new CustomDigitalClock(CityList.this, o.getOrderTime()); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT); customDC.setTextColor(Color.WHITE); customDC.setTextSize(13); LayoutParams param=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); ll.addView(customDC, 2); v = ll; } if (o != null) { TextView tt = (TextView) v.findViewById(R.id.toptext); TextView bt = (TextView) v.findViewById(R.id.bottomtext); if (tt != null) { tt.setText("" + o.getOrderName()); } if (bt != null) { bt.setText("" + o.getOrderStatus()); } v.setOnCreateContextMenuListener(this); } return v; } 

Can someone help me?

+4
source share
1 answer

ListViews processes the views, which means that first the core set of list entries is pumped from XML. When you scroll down, one list entry is hidden at the top, and the bottom is displayed at the bottom. At this point, getView() is called with a nonzero argument to convertView , because the already oversized view is reused.

In your case, this means that the entire inflation / adjustment layout is skipped ( if (v == null) tree). Which is good, basically all you have to do is update the timestamp in the second section of if ( o != null ).

It should contain something similar to this, as with text comments:

 CustomAnalogClock customAC = (CustomAnalogClock) v.findViewById(R.id.yourclockid); customAC.setTime(o.getOrderTime()); 

This means that you must assign an identifier (using setId() ) for your view, adding it to the layout, and also have a setTime() method ready.

+3
source

Source: https://habr.com/ru/post/1387860/


All Articles