Gridview 根据 Android 中的 SQLITE 状态突出显示特定项目

Gridview highlight specific Item based on SQLITE status in Android

我有一个问题,我想在其中设置突出显示 Gridview 项 当来自 SQLITE 的值状态等于 0 时。我有下面的查询 但我不知道从哪里开始以及如何在 GridView.

上突出显示特定项目

输出应该是这样的,突出显示的项目基于 SQLITE 上的状态,其值为 0。有人知道我该如何实现突出显示的部分吗?对我很有用

InventoryListClass

    GridView gridView = (GridView) findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new InventoryListAdapter(this, R.layout.activity_inventory_items, list);
    gridView.setAdapter(adapter);


    try {
        Cursor cursor = sqLiteHelper.getData("SELECT id,hh_number,card_scanning_status FROM CgList");
        list.clear();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String hhNumber = cursor.getString(1);
            
            int status = cursor.getInt(2);
            if (status==0){
                //IF VALUE IS O IT IS HIGHLIGHTED 
            }
            else {
                // IF VALUE IS 1 NOT HIGHLIGHTED
            }
            list.add(new Inventory(hhNumber, id));
        }
        adapter.notifyDataSetChanged();
    }
    catch (Exception e){
        Log.d(TAG, "Error: "+ e);
    }

InventoryListadapter class 更新:我认为不是在 InventoryList class 下设置颜色,而是我认为突出显示应该在 getView 里面试过的是下面的代码 但它似乎是错误的 Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference

  @Override
public View getView(int position, View view, ViewGroup viewGroup) {
    View row = view;
    ViewHolder holder = new ViewHolder();

    if(row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, null);
        holder.txtName = (TextView) row.findViewById(R.id.txtName);
        row.setTag(holder);
    }
    else {
        holder = (ViewHolder) row.getTag();
    }
    Inventory inventory = inventoryList.get(position);
    holder.txtPrice.setText("HH no: "+ inventory.getHHnumber());


    int status = inventory.getStatus();

    if (status==0){
        view.setBackgroundColor(Color.parseColor("#FF108714"));
        //here to set color
    }
    
    return row;
}

int status = inventory.getStatus();

所以,假设你有一个 setter 状态,那么你可以设置它:

try {
    Cursor cursor = sqLiteHelper.getData("SELECT id,hh_number,card_scanning_status FROM CgList");
    list.clear();
    while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String hhNumber = cursor.getString(1);
        
        int status = cursor.getInt(2);
        Inventory inventory = new Inventory(hhNumber, id);
        inventory.setStatus(status);
        list.add(inventory);
    }
    adapter.notifyDataSetChanged();
}
catch (Exception e){
    Log.d(TAG, "Error: "+ e);
}

Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference

getView()view参数可以为空,因此您使用了row实例;但是这里你在 view 上调用了 setBackgroundColor(),而不是 row,所以你需要将其更改为:row.setBackgroundColor()

还要确保设置默认背景颜色,以防状态值不为 0,因为当视图被回收时,您会发现高亮和非高亮行之间有些混乱。

旁注:最佳实践使用 3-arg inflate() 方法:inflater.inflate(layout, viewGroup, false)

应用:

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    View row = view;
    ViewHolder holder = new ViewHolder();

    if(row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, viewGroup, false);
        row.setTag(holder);
    }
    else {
        holder = (ViewHolder) row.getTag();
    }
    
    Inventory inventory = inventoryList.get(position);
    holder.txtPrice.setText("HH no: "+ inventory.getHHnumber());
    int status = inventory.getStatus();

    //here to set color
    if (status==0){
        row.setBackgroundColor(Color.parseColor("#FF108714"));

    } else {
        // set the default color
    }
    
    return row;
}