自定义Listview,第一行不正确

时间:2016-09-28 12:52:56

标签: java android listview

所以我的自定义ListView左侧为TextView,右侧为ImageView。它显示了可以检查的产品。因此,当产品最受欢迎时,它会显示亮星,而不显示暗星。

Everthing对第一行的期望很好。当第一行被选为收藏时,一切正常。但是当它被选为不喜欢的时候,当任何其他产品也有光明星时,它仍然有一颗明星。为了将第一个星级颜色设置为黑色,我需要设置所有不喜欢的产品。

我试着调试它,但我的日志显示一切都很好......我找不到任何合理的答案为什么会发生这种情况,所以我来到这里。我发布了一些代码。

适配器

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;
}

活动

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);

目前,日志。当他们表明:

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

然后第一个位置仍然很轻,因为第二个和第三个位置。我需要设置第二个,黑暗到暗,以便设置第一个黑暗。 :(

3 个答案:

答案 0 :(得分:3)

任何原因isfav()你使用的是setImageResource而在其他部分是setBackgroundResource()

答案 1 :(得分:0)

你应该使用

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");
}

答案 2 :(得分:0)

从适配器中删除ref to Activity并将context.getLayoutInflater();更改为LayoutInflater.from(parent.getContext());rowView = layoutInflater.inflate(R.layout.product_with_fav, parent, false);现在指定要添加到父级,但父级为null,适配器本身添加到父rowView。

替换此private static class ViewHolder

使用新的Android API 22 getResources().getDrawable()现已弃用。使用ContextCompat.getDrawable(getActivity(), R.drawable.name);

你会选择使用setImageResource或setBackgroundResource。

创建适配器,然后在数据不正确时转到列表视图。适配器:

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();
}

更改数据时,请使用:

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));
}