Advertisemen
This example show how to implement ListView with CheckBox, modified from the post "Custom ListView with ImageView".At the beginning, I tried to implement OnCheckedChangeListener for the CheckBox; to handle the check state. But, it will be called when the ListView item scroll-out from the screen, and clear the check state unexpectedly.
Finally, I implement OnClickListener. It work as expected.
/res/layout/row.xml
<?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="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/rowCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/rowImageView"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp" />
<TextView
android:id="@+id/rowTextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidcheckboxlistview.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Button
android:id="@+id/lookup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Look up"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.blogspot.android_er.androidcheckboxlistview;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public class Item {
boolean checked;
Drawable ItemDrawable;
String ItemString;
Item(Drawable drawable, String t, boolean b){
ItemDrawable = drawable;
ItemString = t;
checked = b;
}
public boolean isChecked(){
return checked;
}
}
static class ViewHolder {
CheckBox checkBox;
ImageView icon;
TextView text;
}
public class ItemsListAdapter extends BaseAdapter {
private Context context;
private List<Item> list;
ItemsListAdapter(Context c, List<Item> l) {
context = c;
list = l;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public boolean isChecked(int position) {
return list.get(position).checked;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
ViewHolder viewHolder = new ViewHolder();
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.row, null);
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.rowCheckBox);
viewHolder.icon = (ImageView) rowView.findViewById(R.id.rowImageView);
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.icon.setImageDrawable(list.get(position).ItemDrawable);
viewHolder.checkBox.setChecked(list.get(position).checked);
final String itemStr = list.get(position).ItemString;
viewHolder.text.setText(itemStr);
viewHolder.checkBox.setTag(position);
/*
viewHolder.checkBox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
list.get(position).checked = b;
Toast.makeText(getApplicationContext(),
itemStr + "onCheckedChanged\nchecked: " + b,
Toast.LENGTH_LONG).show();
}
});
*/
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean newState = !list.get(position).isChecked();
list.get(position).checked = newState;
Toast.makeText(getApplicationContext(),
itemStr + "setOnClickListener\nchecked: " + newState,
Toast.LENGTH_LONG).show();
}
});
viewHolder.checkBox.setChecked(isChecked(position));
return rowView;
}
}
Button btnLookup;
List<Item> items;
ListView listView;
ItemsListAdapter myItemsListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listview);
btnLookup = (Button)findViewById(R.id.lookup);
initItems();
myItemsListAdapter = new ItemsListAdapter(this, items);
listView.setAdapter(myItemsListAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
((Item)(parent.getItemAtPosition(position))).ItemString,
Toast.LENGTH_LONG).show();
}});
btnLookup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = "Check items:\n";
for (int i=0; i<items.size(); i++){
if (items.get(i).isChecked()){
str += i + "\n";
}
}
/*
int cnt = myItemsListAdapter.getCount();
for (int i=0; i<cnt; i++){
if(myItemsListAdapter.isChecked(i)){
str += i + "\n";
}
}
*/
Toast.makeText(MainActivity.this,
str,
Toast.LENGTH_LONG).show();
}
});
}
private void initItems(){
items = new ArrayList<Item>();
TypedArray arrayDrawable = getResources().obtainTypedArray(R.array.resicon);
TypedArray arrayText = getResources().obtainTypedArray(R.array.restext);
for(int i=0; i<arrayDrawable.length(); i++){
Drawable d = arrayDrawable.getDrawable(i);
String s = arrayText.getString(i);
boolean b = false;
Item item = new Item(d, s, b);
items.add(item);
}
arrayDrawable.recycle();
arrayText.recycle();
}
}
/res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="resicon">
<item>@android:drawable/ic_dialog_alert</item>
<item>@android:drawable/ic_dialog_dialer</item>
<item>@android:drawable/ic_dialog_email</item>
<item>@android:drawable/ic_dialog_info</item>
<item>@android:drawable/ic_dialog_map</item>
<item>@android:drawable/ic_menu_camera</item>
<item>@android:drawable/ic_menu_compass</item>
<item>@android:drawable/ic_menu_gallery</item>
<item>@android:drawable/ic_menu_my_calendar</item>
<item>@android:drawable/ic_menu_myplaces</item>
<item>@android:drawable/ic_menu_slideshow</item>
<item>@android:drawable/ic_menu_today</item>
</array>
<array name="restext">
<item>"alert"</item>
<item>"dialer"</item>
<item>"email"</item>
<item>"info"</item>
<item>"map"</item>
<item>"camera"</item>
<item>"compass"</item>
<item>"gallery"</item>
<item>"calendar"</item>
<item>"myplaces"</item>
<item>"slideshow"</item>
<item>"today"</item>
</array>
</resources>
Advertisemen