javaandroidandroid-adapterandroid-gridview

Gridview highlight specific Item based on SQLITE status in Android


I have a problem in which I want to set highlight Gridview item when the value status from SQLITE is equal to 0. I have the query below but yet I don't have the idea where to start and how can I highlight the specific item on GridView.

The output should something like this, the highlighted item was based on the status on SQLITE which the value is 0. Is somebody knows how can I implement the highlighted part?. It is very useful for me

enter image description here

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 Updated: I think instead of setting color under InventoryList class I think highlight should be inside of getView what I tried is the below code but it seems it error 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;
}

Solution

  • int status = inventory.getStatus();

    So, assuming you've a setter to the status, then you can set it:

    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

    The view parameter of getView() is nullable, and therefore you used the row instance; but here you called setBackgroundColor() on the view, not the row, so you need to change this to: row.setBackgroundColor() instead.

    Also make sure to set the default background color in case the status value is not 0, because when the views are recycled you will find some mess among the highlighed and non-highlighted rows.

    Side note: fore best practice use the 3-arg inflate() method: inflater.inflate(layout, viewGroup, false)

    Applying that:

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