FORUMS
Remove All Ads from XDA

Cab and row with checkbox

1 posts
Thanks Meter: 0
 
By Masterpc96, Junior Member on 2nd May 2017, 10:30 PM
Post Reply Email Thread
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

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();
}
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes