• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cab and row with checkbox

Masterpc96

Lurker
Hello everybody. I created a contextual action bar with list view with my own adapter. It works perfectly. Now I want to add a check box to select the row, when user click anywhere. I added checkbox to row layout (it contains textview, textview and checkbox) and setEnable(false) because I will programaticaly set clicked/unclicked. But I have a problem how to add a listener to each row in an adapter which will be checking if the row was clicked and set checkbox clicked and change the object value click to true(I only need help with adding listener). Could anybody help me :)
Here is my adapter code
Java:
package com.example.micha.shoppinglist;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Michał on 26.04.2017.
*/

public class RowAdapter extends ArrayAdapter<ProductList>{
    private Context context;
    private int resource;
    private ArrayList<ProductList> data = null;
    private RowBeanHolder holder = null;
    private boolean longPressed=false;


    public RowAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<ProductList> data) {
        super(context, resource, data);
        this.context=context;
        this.resource=resource;
        this.data=(ArrayList<ProductList>)data;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {
        View row = convertView;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);

            holder = new RowBeanHolder();
            holder.txtTitle=(TextView) row.findViewById(R.id.name);
            holder.indicator=(TextView) row.findViewById(R.id.indicator);
            holder.checkBox=(CheckBox) row.findViewById(R.id.checkBoxDelete);
            row.setTag(holder);
        }
        else
        {
            holder = (RowBeanHolder)row.getTag();
        }

        if(longPressed){
            showCheckbox();
        }
        else{
            hideCheckbox();
            uncheck();
        }

        ProductList object = data.get(position);
        holder.txtTitle.setText(object.getName());
        holder.indicator.setText(object.getToBuy() + "/" + object.getBought());

        return row;
    }

    static class RowBeanHolder {
        TextView txtTitle;
        TextView indicator;
        CheckBox checkBox;
    }

    public void showCheckbox() {
        holder.checkBox.setVisibility(View.VISIBLE);
        longPressed=true;
        notifyDataSetChanged();
    }

    public void hideCheckbox() {
        holder.checkBox.setVisibility(View.GONE);
        longPressed=false;
        notifyDataSetChanged();
    }

    public void uncheck(){
        holder.checkBox.setChecked(false);
        notifyDataSetChanged();
    }
}
 
Back
Top Bottom