开发者

ListView With Checkboxes have problem

I implement a ListView with three columns, first is Textview, second is also TextView and third is CheckBox.

all thing do fine, it show exactly in Layout file as i want.

but two problems i faced

1) The checkBox checked or unchecked state is replaced automatically when i scroll the screen, to show full List.

2) listview.setOnItemClickListener(new OnItemClickListener(){}), this function is not calling.

I want to check a checkbox on click any of the item in listView

and then has a button to submit all checked state of listview, how i can do this

listview=(ListView)findViewById(R.id.listview);
        // create the grid item mapping
        String[] from = new String[] {"col_1","col_2"};
        int[] to = new int[] { R.id.txtnameItem, R.id.txtpriceItem};

        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i <items.length; i++)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("col_1",items[i]);
            map.put("col_2",price[i]);
            fillMaps.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, fillMaps, R.layout.item, from, to);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parentView, View v, int position,long id) 
            {
                Log.d("Inside onClilck log","Yes");
                Toast.makeText(v.getContext(),""+position,Toast.LENGTH_LONG).show();
            }
        });

item.xml file is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:background="#FFFFFF"
  android:layout_height="fill_parent">
    <LinearLayout
        android:layout_margin="15dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:id="@+id/linearLayout1">
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_gravity="center"
            android:padding="5dip"
            android:paddingRight="0dip"
            android:id="@+id/linearLayout2">
            <TextView
                android:id="@+id/txtnameItem"
                android:textSize="20px"
                android:textStyle="bold"
                android:text="col_1"
                android:gravity="left"
                android:textColor="#000000"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:textStyle="bold"
            android:id="@+id/linearLayout2">
            <TextView
                android:id="@+id/txtpriceItem"
                android:gravity="left"
                android:layout_gravity="center"
                android:text="col_2"
                android:layout_weight="1"
                android:textSize="20px"
                android:textColor="#000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <CheckBox
                android:gravity="right"
                android:layout_weight="1"
                android:text=""
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="right|center"
                android:checked="false"
                android:id="@+id/chkboxItem">
            </CheckBox>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

And finally main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <!-- Header -->
    <LinearLayout android:id="@+id/header"
        android:background="#FFFFFF"
        android:padding="10dip"
        android:orientation="horizontal"
        android:paddingBottom="5dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <TextView android:id="@+id/heading"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Items"
            android:gravity="left"
            android:layout_weight="1"
            android:textColor="#CC6633"
            android:textSize="25px"
            android:textStyle="bold"
        />
        <Button
            android:text="Add To Order"
            android:id="@+id/btnOrder"
            android:layout_weight="1"
            android:textSize="15px"
            android:gravity="right"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/header"
        android:background="#FFFFFF"
        android:padding="5dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_gravity="center"
            android:padding="5dip"
            android:id="@+id/linearLayout2">
            <TextView
                android:id="@+id/txtnameItem"
                android:text="Name"
                android:gravity="left"
                android:textColor="#000000"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:padding="5dip"
            android:gravity="left"
            android:id="@+id/linearLayout2">
            <TextView
                android:id="@+id/txtpriceItem"
                android:gravity="left"
                android:text="Price"
                android:layout_weight="1"
                android:textColor="#000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_gravity="left">
            </TextView>
            <TextView
                android:id="@+id/txtorderItem"
                android:gravity="right"
                android:text="Order"
                android:layout_weight="1"
                android:textColor="#000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right">
            </TextView>
        </LinearLayout>
    </LinearLayout>

    <!-- List Divider -->
    <View android:layout_width="fill_parent"
        android:layout_height="5dip"
        android:background="#C85A17" />

    <!-- ListView (list_items) -->
    <LinearLayout android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_hei开发者_JAVA技巧ght="wrap_content">
        <ListView android:id="@+id/listview"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:layout_width="fill_parent">
        </ListView>
    </LinearLayout>
</LinearLayout>


I suggest rather than making it a simpleadpater make it a customized adapter extended from Base Adapter.

Then you change the state (on/off) of your checkbox in a row in listview.setOnItemClickListener .. and call notifyDataSetChanged() over adapter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜