customised listview using arrayadapter class in android

Drawing from https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
    String[] GENRES = new String[] {"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"};
    private CheckBoxAdapter mCheckBoxAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listView = (ListView) findViewById(R.id.lv);

        listView.setItemsCanFocus(false);
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(this);
        mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
        listView.setAdapter(mCheckBoxAdapter);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                StringBuilder result = new StringBuilder();
                for (int i = 0; i < GENRES.length; i++) {
                    if (mCheckBoxAdapter.mCheckStates.get(i) == true) {
                        result.append(GENRES[i]);
                        result.append("\n");
                    }

                }
                Toast.makeText(MainActivity.this, result, 1000).show();
            }

        });
    }

    public void onItemClick(AdapterView parent, View view, int position, long id) {
        mCheckBoxAdapter.toggle(position);
    }

    class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener {
        LayoutInflater mInflater;
        TextView tv1, tv;
        CheckBox cb;
        String[] gen;
        private SparseBooleanArray mCheckStates;
        private SparseBooleanArray mCheckStates;

        CheckBoxAdapter(MainActivity context, String[] genres) {
            super(context, 0, genres);
            mCheckStates = new SparseBooleanArray(genres.length);
            mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            gen = genres;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return gen.length;
        }
    }
}

activity_main.xml

 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">

 <ListView
 android:id="@+id/lv"
 android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_above="@+id/button1"/>
  <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:text="Button" />

</RelativeLayout>

And the XML file for the checkboxes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="34dp"
    android:text="TextView" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="22dp"
    android:layout_marginTop="23dp" />

 </RelativeLayout>

When you click the button a toast message with list of item choosen is displayed. You can modify the above according to your requirements.

enter image description here

Leave a Comment