homtg
homtg

Reputation: 1999

Custom ListAdapter in Android not working as expected especially when filtered

i do have a list with imageviews in every row. In some lines i want to display those images and in somes i do not want to diaplay them so i use a custom ListAdapter:

The List Row XML has:

<ImageView
 android:id="@+id/image1"
 android:layout_height="fill_parent"
 android:src="@drawable/button1"
 android:visibility="invisible"/>

So as you can see by default all images are invisible.

In the custom SimpleAdapter i then check my condition and depending on that i set the image of this row to visible:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

if(condition == true){

   ((ImageView) view.findViewById(R.id.image1)).setVisibility(View.VISIBLE);
}

return view;

}

So i am doing it like this with 2 ImageViews and 3 TextViews in every row. The ImageViews are invisible by default and get visible based on a condition as shown above. The TextVoews are black and get another text color based on another condition.

My problem is that this works fine for small lists like 1-5 items or so but when the list gets longer this does not work as expected. The result is sometimes just wrong and when i reload the listview based on the same data i may see a different result every time. The Images are set wrong or the text colors are set wrong.

The second problem ist that this gets completely and definitely wron when i do apply a filter to the adapter. I do have an EditText field and i want to aplly the filter on every change of this EditText so i have a TextChangedListener on that EditText:

edittext1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void afterTextChanged(Editable s) {

            adapter.getFilter().filter(s);
        }
    });

From the moment i set the focus to this EditText the whole listview completely changes and it seems like the condition based formatting now is not only "sometimes wrong" but now "completely without any logic".

So is this the right way i am creating my rows and using the adapter? Why is this not working properly and why is it going completely crazy when i start to use the filter?

Thanks a lot for your help in advane.

Upvotes: 0

Views: 951

Answers (3)

homtg
homtg

Reputation: 1999

So i finally changed that SimpleAdapter i had to an ArrayAdapter and did not use the default Filter method but implemented one which is filtering the ArrayList and setting the filtered ArrayList as the Adapters List. Now it works. With the SimpleAdapter the Images have not been updated after filtering and with a filtered List i could not get the Views updated. Works now.

Upvotes: 0

b_yng
b_yng

Reputation: 14136

For the first problem:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.my_list_row, null);

        if(condition == true)
            ((ImageView) convertView.findViewById(R.id.image1)).setVisibility(View.VISIBLE);
        else
            ((ImageView) convertView.findViewById(R.id.image1)).setVisibility(View.INVISIBLE);
    }

    return convertView; 
}

As for the filter, what are you trying to do? You might want to check out inputTypes:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

Upvotes: 0

dmon
dmon

Reputation: 30168

Adapter views get recycled, so you have to account for all paths, e.g.:

if(condition == true){

   ((ImageView) view.findViewById(R.id.image1)).setVisibility(View.VISIBLE);
} else {
   ((ImageView) view.findViewById(R.id.image1)).setVisibility(View.INVISIBLE);
}

That way the ImageView will get hidden when the condition is false.

No clue about your filter problem, my hunch is that it's related to the same problem as above.

Upvotes: 1

Related Questions