Hi, I created a list with checkboxes in it ... but I donβt know how to get the selected checkbox. Here is the activity_main.xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="top" android:orientation="vertical" tools:context=".MygamesActivity" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="20dp" /> </LinearLayout>
another layout that has checkboxes to display in the list main.list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" /> </LinearLayout>
and this is a class that extends the arrayadapter
package com.wasiff.listview; import android.content.Context; import android.content.res.TypedArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class CheckboxAdapter extends ArrayAdapter<String> { private LayoutInflater mInflater; private String[] mStrings; private TypedArray mIcons; private int mViewResourceId; public CheckboxAdapter(Context ctx,int viewResourceId,String[] strings){ super(ctx,viewResourceId,strings); mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mStrings = strings; mViewResourceId = viewResourceId; } public int getCount(){ return mStrings.length; } public String getItem(int position){ return mStrings[position]; } public long getItemId(int position){ return 0; } public View getView(int position,View convertView,ViewGroup parent){ convertView = mInflater.inflate(mViewResourceId, null); CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1); tv.setText(mStrings[position]); return convertView; } }
and this is my mainActivity class
package com.wasiff.listview; import android.app.ListActivity; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context ctx = getApplicationContext(); Resources res = ctx.getResources(); String[] options = res.getStringArray(R.array.countrynames); setListAdapter((ListAdapter) new CheckboxAdapter(ctx,R.layout.main_list_item,options)); } @Override public boolean onCreateOptionsMenu(Menu menu) {
and finally i have all the countries saved in countries.xml in the values ββfolder
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="countrynames" translatable="false"> <item>Bhutan</item> <item>Colombia</item> <item>India</item> <item>Pakistan</item> <item>Australia</item> <item>Srilanka</item> <item>England</item> </string-array> </resources>
it shows the checkboxes in the listView, now I want to get the text of the checkboxes that have been checked and show in the toast at the click of a button (to check). I followed the orroid cookbook tutorial, but still I don't know how to set the listener