Android Listview Amongst Checkbox

Posted on

This representative present how to implement ListView amongst CheckBox, modified from the post “Custom ListView amongst ImageView“.

At the beginning, I tried to implement OnCheckedChangeListener for the CheckBox; to grip the banking company check state. But, it volition last called when the ListView especial scroll-out from the screen, as well as clear the banking company check field unexpectedly.

Finally, I implement OnClickListener. It move every bit 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="https://rootandroid.org//"         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;  populace storey MainActivity extends AppCompatActivity {      populace storey Item {         boolean checked;         Drawable ItemDrawable;         String ItemString;         Item(Drawable drawable, String t, boolean b){             ItemDrawable = drawable;             ItemString = t;             checked = b;         }          populace boolean isChecked(){             supply checked;         }     }      static storey ViewHolder {         CheckBox checkBox;         ImageView icon;         TextView text;     }      populace storey ItemsListAdapter extends BaseAdapter {          someone Context context;         someone List<Item> list;          ItemsListAdapter(Context c, List<Item> l) {             context = c;             listing = l;         }          @Override         populace int getCount() {             supply list.size();         }          @Override         populace Object getItem(int position) {             supply list.get(position);         }          @Override         populace long getItemId(int position) {             supply position;         }          populace boolean isChecked(int position) {             supply list.get(position).checked;         }          @Override         populace View getView(final int position, View convertView, ViewGroup parent) {             View rowView = convertView;              // reuse views             ViewHolder viewHolder = novel 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);              terminal String itemStr = list.get(position).ItemString;             viewHolder.text.setText(itemStr);              viewHolder.checkBox.setTag(position);              /*             viewHolder.checkBox.setOnCheckedChangeListener(                     novel CompoundButton.OnCheckedChangeListener() {                 @Override                 populace 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                 populace 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));              supply 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 = novel ItemsListAdapter(this, items);         listView.setAdapter(myItemsListAdapter);          listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){              @Override             populace 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             populace 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();              }         });     }      someone void initItems(){         items = novel 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 second = arrayText.getString(i);             boolean b = false;             Item especial = novel 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>