0

i am writing a code to show a multiple selection list (one having check boxes) and when the user checks an item i want to retrieve user's name and id from database table and display it in toast. Here's the code:

Activity code

public class Add_To_Circle extends Activity {
ListView lv;
ListAdapter adapter;
@Override
    public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.add_to_circle);
               // Get listview
                lv = (ListView)findViewById(R.id.list);
                lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
... //all the other code,and then in another class in postexecute there is adapter:

protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            //pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                     adapter = new SimpleAdapter(
                             Add_To_Circle.this, productsList,
                             android.R.layout.simple_list_item_multiple_choice, new String[] { TAG_PID,
                                    TAG_NAME},
                            new int[] { R.id.pid, R.id.name });

                    // updating listview
                    lv.setAdapter(adapter);
                }
            });

        }
}

Code of XML file

<RelativeLayout 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">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </ListView>

</RelativeLayout>

the output: a blank listview with only checkboxes as in the attched image. What am i doing wrong? i have followed same steps as in tutorials on web still no luck :((

  • you can use custom adapter that has one textview and checkbox for each row – micky Oct 27 '14 at 13:03
  • is there no way to get this code to running? P.S when i write R.layout.list_item in place of android.R.layout.simple_list_item_multiple_choice the list view has all the values then but there are no checkboxes. can u tell the possible reason? – user3347803 Oct 27 '14 at 13:15
  • thatsy I am saying instead of using simple adapter you can use custom adapter that has one textview and checkbox – micky Oct 27 '14 at 13:18

2 Answers2

0

you can use custom adapter that has one textview and checkbox.Here are the links,

http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html

http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html

and if you want to use simple array adapter,

refer this link,

Selecting multiple items in ListView

Thanks

Community
  • 1
  • 1
micky
  • 508
  • 5
  • 17
0

Ok, will try and give an answer but I have had to change the Adapter for the answer, so that part is untested.

Lets go bottom up, this is the XML layout for each single element in the list:

view_adapter_item_checklist.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Using CheckedTextView removes the need to handle the checkbox checked/unchecked handling ourselves.

Now the ListView, in my code it is placed inside a LinearLayout, hence the 0dp height. The important part is choiceMode:

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:choiceMode="multipleChoice"
    android:drawSelectorOnTop="false" />

The adapter is really rather simple. It takes a list of Strings and inflate list rows based on the checktext XML above. The only task is to set the text of the view, actually:

SimpleChecklistAdapter:

public class SimpleChecklistAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final List<String> values;

    public SimpleChecklistAdapter(Context context, List<String> values) {
        super(context, R.layout.view_adapter_item_checklist, values);
        this.context = context;
        this.values = values;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.view_adapter_item_checklist,
                parent, false);

        CheckedTextView text = (CheckedTextView)rowView.findViewById(R.id.item);
        text.setText(values.get(position));


        return rowView;
    }
}

All there is left is to use the new fancy checklist adapter instead of SimpleAdapter in your code

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33