I am implementing the checkbox with listview for every Iten of listview.The Problem i am getting is when I am clicking on any single check box and when i scroll then some ot
Override this methods in your adapter:
override fun getItemId(position: Int) = position.toLong()
override fun getItemViewType(position: Int) = position
Just set focusable Property = false. make onClickListener on CheckBox in the adapter.
{ <CheckBox
android:id="@+id/cb_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:button="@drawable/custom_checkbox"
android:checked="true"
android:focusable="false"
android:padding="7dip" />}
Use two way data binding to handle checkbox state https://developer.android.com/topic/libraries/data-binding/two-way#kotlin
<CheckBox
android:id="@+id/rememberMeCheckBox"
android:checked="@={viewmodel.rememberMe}"
/>
class LoginViewModel : BaseObservable { // val data = ...
@Bindable
fun getRememberMe(): Boolean {
return data.rememberMe
}
fun setRememberMe(value: Boolean) {
// Avoids infinite loops.
if (data.rememberMe != value) {
data.rememberMe = value
// React to the change.
saveData()
// Notify observers of a new value.
notifyPropertyChanged(BR.remember_me)
}
}
}
This is happening because of view recycling. What you need to do is maintain an array of say booleans and every time a checkbox is checked toggle the corresponding boolean. Then in your getview check the corresponding boolean array position and set the state of the check box.
Along these lines: I have modified it so it displays correctly. Have not checked what the rest of your code is upto. ^_^
public class CustomListViewAdapter extends ArrayAdapter<Client> {
ViewHolder holder = null;
private LayoutInflater inflator = null;
private ArrayList<Client> orderList = null;
ArrayList<View> allViews;
boolean[] checkedStates; //********** NEW ********
public CustomListViewAdapter(Context context, int resource,
List<Client> objects) {
super(context, resource, objects);
orderList = (ArrayList<Client>) objects;
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
allViews = new ArrayList<View>();
checkedStates = new boolean[objects.size()]; //********** NEW ********
}
@Override //********** NEW ******** position argument is now final
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflator.inflate(R.layout.listview_add_order, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewAddOrder);
holder.spinnerAddorder = (Spinner) convertView.findViewById(R.id.spinnerAddOrder);
holder.checkAddorder = (CheckBox) convertView.findViewById(R.id.checkAddOrder);
holder.checkAddorder.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.checkAddorder.setTag(holder);
}
allViews.add(position, holder.spinnerAddorder);
holder.checkAddorder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
checkedStates[position] = !checkedStates[position]; //********** NEW ********
}
});
holder.chechAddorder.setChecked(checkedStates[position]); //********** NEW ********
List<String> list = new ArrayList<String>();
list.add("Select Quantity");
list.add("1");
list.add("2");
list.add("3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getContext(),R.layout.spinner_add_order_style, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
holder.spinnerAddorder.setAdapter(dataAdapter);
if(checkedStates[position]) { //********** NEWEST ******** assuming true is checked and false is unchecked
ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
checkPosition.spinnerAddorder.setVisibility(View.VISIBLE);
} else {
ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE);
}
Client order = orderList.get(position);
holder.txtViewName.setText(order.getFirstName());
return convertView;
}
private static class ViewHolder {
public TextView txtViewName = null;
public Spinner spinnerAddorder = null;
public CheckBox checkAddorder = null;
}
}