jeudi 24 septembre 2015

Dynamically adding views to a listview row with checkbox

I have a listview where I need to add views dynamically to the listview row.I have done that but the dynamically added view has a checkbox. So for example if i have two dynamic views added to a row of listview when I check one checkbox the other has to be unchecked.No matter the number of dynamic views, at a time only one checkbox should be selected.

This is the code I used to add dynamic views to the row of a listview and its in the getview of the Adapter.

if (commutes.get(position).types.size() != 0) {


            viewHolder.parentPanel.setVisibility(View.VISIBLE);

            viewHolder.parentPanel.removeAllViews();


            for (i = 0; i < commutes.get(position).types.size(); i++) {

                // inflate child
                View item = inflater.inflate(R.layout.childview, null);


                CheckBox chk = (CheckBox) item.findViewById(R.id.ck);


                int newId=Integer.parseInt(position+""+i);
                chk.setId(newId);
                if(mChecked.contains(newId))
                {
                    chk.setChecked(true);
                }
                else
                {
                    chk.setChecked(false);

                }



                chk.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        if (cb.isChecked()) {
                            mChecked.add(cb.getId());
                        } else {

                            mChecked.remove(mChecked.indexOf(cb.getId()));


                        }
                    }

                });



                TextView rupeeSymbol = (TextView) item.findViewById(R.id.tvRupeesSymbol);
                rupeeSymbol.setTypeface(AppUtil.getINRTypeFace());
                TextView rupees = (TextView) item.findViewById(R.id.tvRupees);
                TextView kms = (TextView) item.findViewById(R.id.tvkm);

                rupees.setText(Html.fromHtml("<b>" + commutes.get(position).types.get(i).price + "</b>" +
                        "<small>" + " for " + "</small>"));

                kms.setText(Html.fromHtml("<b>" + commutes.get(position).types.get(i).kms + "</b>" +
                        "<small>" + " (" + commutes.get(position).types.get(i).free_text + ")" + "</small>" + "<br />"));


                int viewId = position + i;
                item.setId(viewId);
                viewHolder.parentPanel.addView(item);

            }

        } else {
            viewHolder.parentPanel.setVisibility(View.GONE);

        }

For checkbox functionality below code is declared

ArrayList<Integer> mChecked=new  ArrayList<Integer>();

This is the viewholder for the adapter.

private static class ViewHolder {
        LinearLayout parentPanel;
        TextView locationSelected;
        TextView price;
        TextView carName;

    }

This is the xml of the childview

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:id="@+id/relativeLayout"
    tools:context=".CommuteActivity">

    <TextView
        android:id="@+id/tvRupeesSymbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:text="`"
        android:textColor="@color/text_dark_grey"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_toRightOf="@+id/tvRupeesSymbol"
        android:gravity="center"
        android:id="@+id/tvRupees"
        android:layout_marginLeft="5dp"
        android:layout_gravity="center_vertical"
        android:text="ddddddd"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:id="@+id/tvkm"
        android:layout_toRightOf="@+id/tvRupees"
        android:gravity="center_vertical"
        android:layout_gravity="center"
        />


    <CheckBox
        android:id="@+id/ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:src="@drawable/tick"/>
</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire