4

In my application I am using a CustomListView with an ArrayAdapter to show different countries time. But after 6 to 7 rows(depending on the phone screen size) the time values are repeating.

According to some previous post I have written the following code snippet to get the solution. But the problem is still there.

Following is the code I have written:

 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 anybody help me?

jyotiprakash
  • 2,086
  • 1
  • 20
  • 26
  • *What* problem? Describe it *in detail*. – Ondrej Tucny Dec 24 '11 at 10:39
  • What happens if you change all your `WRAP_CONTENT` to `FILL_PARENT` ? – TryTryAgain Dec 24 '11 at 10:40
  • Already it's FILL_PARENT in xml file. The problem is something different. While I am adding cities, it's showing correct time for all those cities still the phone screen is full. Once the listview exceeds the screen, means scrollbar starts the new child starts showing wrong time. – jyotiprakash Dec 24 '11 at 10:45

1 Answers1

3

ListViews recycle views, which means at first a base set of list entries is inflated from XML. When you scroll down now, one list entry gets hidden at the top, and a new one gets shown at the bottom. At this moment getView() is called with a non-null argument convertView, because an already inflated view is reused.

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

It should contain something similar to this, like you did with the textviews too:

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

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

  • I am not using any analog clock. And I have to show my digital clock within a textview created inside the list's convertView. :( – jyotiprakash Dec 24 '11 at 11:15
  • Ops, mixed up the lines. Didn't see that your analogclock has no effects *(it's usually best to strip out unneccessary/irrelevant lines out of questions because of this)*. The same applies to the digital clock as well. You have to update the time inside the second if-section as described with the analog example here. –  Dec 24 '11 at 11:19
  • CustomDigitalClock customDC = (CustomDigitalClock) v.findViewById(R.id.time); customDC.setTime(o.getOrderTime()); – jyotiprakash Dec 24 '11 at 11:32
  • after writing this to the (o != null) if section now my app is crashing, dn't know what to do now – jyotiprakash Dec 24 '11 at 11:33
  • Most likely you didn't assign any id to your CustomDigitalClock. Which means that `findViewById()` returns null. That results in a crash due to a NullPointerException in the second line. You have to assign an ID to it first in the inflation block by using `setId()`. –  Dec 24 '11 at 11:35
  • can you provide me the exact code snippet about setId(). This is my first android project, and I am totally confused with these things now. – jyotiprakash Dec 24 '11 at 11:47
  • If you use the id above (`R.id.time`), it should be something like: `customDC.setId(getResources().getIdentifier("time", "id", getPackageName));` –  Dec 24 '11 at 11:50