I created an app is working with custom ListView
. In the custom, ListView
have Checkbox
option. When I scroll up or down over the phone screen Checkbox got unchecked automatically. Do you have any solution on this case? Please help to solve this issue. Thanks!
customelist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".menu_list">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textViewItemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/buttonBarcode"
android:fontFamily="serif"
android:text="Medium Text"
android:textColor="@android:color/holo_red_dark"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkboxList"
android:layout_width="32dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginEnd="30dp"
android:background="@drawable/button_round_green"
android:textAlignment="center"
android:textColorHint="@android:color/holo_red_dark"
android:textColorLink="@android:color/holo_red_dark" />
</RelativeLayout>
</RelativeLayout>
ListAdapter.java
public class listAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] index_item;
private final String[] barcode_food;
private final String[] item_name;
private final String[] price_item;
//private TextView textView_name, textView_price;
//private Button button_index, button_barcode;
//private CheckBox checkBoxItem;
public listAdapter(Activity context, String[] index_item, String[] barcode_food, String[] item_name, String[] price_item) {
super(context, R.layout.activity_menu_list, index_item);
this.context = context;
this.index_item = index_item;
this.barcode_food = barcode_food;
this.item_name = item_name;
this.price_item = price_item;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);
TextView textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);
final CheckBox checkBoxItem = (CheckBox) rowView.findViewById(R.id.checkboxList);
textView_name.setText(item_name[position]);
// Row View got click
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBoxItem.isChecked()==true){
checkBoxItem.setChecked(false);
}
else checkBoxItem.setChecked(true);
}
});
// Return row view
return rowView;
}