Skip to content

Instantly share code, notes, and snippets.

@jimitjaishwal
Created December 20, 2016 08:49
Show Gist options
  • Save jimitjaishwal/67c06b998796ede9fd803ac767a51e97 to your computer and use it in GitHub Desktop.
Save jimitjaishwal/67c06b998796ede9fd803ac767a51e97 to your computer and use it in GitHub Desktop.
how to limit checkbox selection in listview?
package com.android.jmitjaishwal.pannadrinks.activitys;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.AbsListView;
import android.widget.ListView;
import com.android.jmitjaishwal.pannadrinks.R;
import com.android.jmitjaishwal.pannadrinks.adapters.ListAdapter;
import com.android.jmitjaishwal.pannadrinks.models.ListModel;
import java.util.ArrayList;
import java.util.List;
public class DemoActivity extends AppCompatActivity {
private ListView listView;
private ListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
listView = (ListView) findViewById(R.id.list_view);
List<ListModel> listModels = new ArrayList<ListModel>();
listModels.add(new ListModel("Blue Moon", "Blue moon Belgian white - 5.4%"));
listModels.add(new ListModel("Raspberry", "Sea dog fruit ale - 4.6%"));
listModels.add(new ListModel("Blue Moon", "Blue moon Belgian white - 5.4%"));
listModels.add(new ListModel("Raspberry", "Sea dog fruit ale - 4.6%"));
listView.setSelection(AbsListView.CHOICE_MODE_MULTIPLE);
listAdapter = new ListAdapter(this, listModels);
listView.setAdapter(listAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/list_view_listener"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/product_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="Blue Moom"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/product_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Blue Moom"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="20dp"
android:gravity="center_vertical|end"
android:paddingLeft="10dp">
<CheckBox
android:id="@+id/icon_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/black" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.android.jmitjaishwal.pannadrinks.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.jmitjaishwal.pannadrinks.R;
import com.android.jmitjaishwal.pannadrinks.models.ListModel;
import java.util.List;
public class ListAdapter extends ArrayAdapter<ListModel> {
private ListModel listModel;
private int selectedItemCounter = 0;
public ListAdapter(Context context, List<ListModel> listModels) {
super(context, 0, listModels);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
listModel = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
}
TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);
Listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icon_right.performClick();
}
});
icon_right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedItemCounter++;
} else {
selectedItemCounter--;
}
if (selectedItemCounter >= 3) {
buttonView.setChecked(false);
selectedItemCounter--;
notifyDataSetChanged();
}
}
});
icon_right.setChecked(false);
productTitle.setText(listModel.product);
productDescription.setText(listModel.productDetails);
// icon_right.setImageResource(R.drawable.icon_save);
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment