Tomek
Tomek

Reputation: 386

Custom Listview, first row is incorrect

So I do have my custom ListView with TextView on the left and ImageView on the right side. It shows products that can be checked favourite. So when the product is favourite it shows light star and when not it shows dark star.

Everthing works fine expect of the first row. When first row is selected as favourite everything works fine. But when it is selected non-favourite, then it still has a light star when any other product has also light star. In order to set firsts star colour to dark, I need to set all the products non-favourite.

I tried to debug it, but my logs show that everything is fine... I can't find any reasonable answer why this happens to me so I came here. I post some of code.

Adapter

public class ProductsWithFavAdapter extends ArrayAdapter<Products>{
private Activity context;
private List<Products> products;
public ProductsWithFavAdapter(Activity context, List<Products> products) {
    super(context, R.layout.product_with_fav, products);
    this.context = context;
    this.products = products;
}

private class ViewHolder {
    public TextView productName;
    public ImageView imageFav;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    View rowView = convertView;
    if(rowView == null) {
        LayoutInflater layoutInflater = context.getLayoutInflater();
        rowView = layoutInflater.inflate(R.layout.product_with_fav, null, true);
        viewHolder = new ViewHolder();
        viewHolder.productName = (TextView) rowView.findViewById(R.id.textView1);
        viewHolder.imageFav = (ImageView) rowView.findViewById(R.id.imageView2);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();
    }
    Products product = products.get(position);

    if(product.isFav()) {
        viewHolder.imageFav.setImageResource(R.drawable.fav_light);
        Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET LIGHT");
    } else {
        viewHolder.imageFav.setBackgroundResource(R.drawable.fav_dark);
        Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET DARK");
    }

    viewHolder.productName.setText(product.getName());

    return rowView;
}

Activity

products.clear();
            favProductsTriggered = !favProductsTriggered;

            String searchProductText = searchProduct.getText().toString();
            boolean searchingProduct = false;

            if(!searchProductText.equals("") && !searchProductText.isEmpty()) {
                searchingProduct = true;
            }

            ProductsDbAdapter adapterProducts = new ProductsDbAdapter(getActivity());
            adapterProducts.open();
            final Cursor cursorProducts = adapterProducts.getAllProducts();

            if(cursorProducts != null && cursorProducts.moveToFirst()) {
                do {
                    int id = cursorProducts.getInt(ProductsDbAdapter.ID_COLUMN);
                    String name = cursorProducts.getString(ProductsDbAdapter.NAME_COLUMN);
                    boolean fav = cursorProducts.getInt(2) != 0;
                    products.add(new Products(id, name, 0, 0, fav));
                    Log.d("wwFAV " + Integer.toString(cursorProducts.getPosition()), Integer.toString(cursorProducts.getInt(2)));
                } while(cursorProducts.moveToNext());
            }

            ProductsWithFavAdapter listProductsAdapter = new ProductsWithFavAdapter(getActivity(), products);
            lvProducts = (ListView) ll.findViewById(R.id.listView3);
            lvProducts.setAdapter(listProductsAdapter);

And for now, logs. When they show that:

AdaptFAV 0 bułka pszenna: 0 SET DARK
AdaptFAV 1 płatki owsiane: 1 SET LIGHT
AdaptFAV 2 bułka jęczmienna: 1 SET LIGHT

then first position is still LIGHT because second and third are. I need to set second and dark to dark in order to set first dark. :(

Upvotes: 0

Views: 96

Answers (3)

Yanbaev
Yanbaev

Reputation: 1

Remove ref to Activity from adapter and change context.getLayoutInflater(); to LayoutInflater.from(parent.getContext()); and rowView = layoutInflater.inflate(R.layout.product_with_fav, parent, false); Now you specify that you want to add to the parent, but the parent is null, adapter itself add to the parent rowView.

Replace this private static class ViewHolder.

With new android API 22 getResources().getDrawable() is now deprecated. Use ContextCompat.getDrawable(getActivity(), R.drawable.name);

And what will you choose to use setImageResource or setBackgroundResource.

Creating the adapter and then take to the listview when the data is not correct. The adapter:

public ProductsWithFavAdapter(Context context, List<Products> products) {
    super(context, R.layout.product_with_fav, products);
    this.products = new ArrayList<>(products);
}
public void setProducts(List<Products> newProducts) {
    this.products.clear();
    this.products.addAll(newProducts);
    notifyDataSetChanged();
}

When you change the data, use:

lvProducts = (ListView) ll.findViewById(R.id.listView3);
ListAdapter adapter = lvProducts.getAdapter();
if (adapter instanceof ProductsWithFavAdapter) {
    ((ProductsWithFavAdapter) adapter).setProducts(newProducts);
} else {
    lvProducts.setAdapter(new ProductsWithFavAdapter(this, newProducts));
}

Upvotes: 0

Rajesh Panchal
Rajesh Panchal

Reputation: 1170

You should use either

if(product.isFav()) {
    viewHolder.imageFav.setImageResource(R.drawable.fav_light);
    Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET LIGHT");
} else {
    viewHolder.imageFav.setImageResource(R.drawable.fav_dark);
    Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET DARK");
}

OR

if(product.isFav()) {
    viewHolder.imageFav.setBackgroundResource(R.drawable.fav_light);
    Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET LIGHT");
} else {
    viewHolder.imageFav.setBackgroundResource(R.drawable.fav_dark);
    Log.d("AdaptFAV " + Integer.toString(position) + " " + product.getName(), Integer.toString(product.isFav() ? 1 : 0) + " SET DARK");
}

Upvotes: 0

Dharmita bhatt
Dharmita bhatt

Reputation: 475

Any reason isfav() you are using setImageResource and in else part as setBackgroundResource()

Upvotes: 3

Related Questions