如何使用listview中的复选框(每个列表中有多行)并扩展Listactivity?

时间:2011-05-11 02:50:52

标签: android listview checkbox listactivity

这是我的程序,它扩展了ListActivity,在每个列表中我都有多行,图片和复选框,所以我试试这个

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    List<Map<String, Object>> resourceNames =new ArrayList<Map<String, Object>>();

    Map<String, Object> data1,data2;

    data1 = new HashMap<String, Object>();
    data2 = new HashMap<String, Object>();

      data1.put("line1", "adidas Chelsea Tee-Womens" );
      data1.put("line2", "$ 129.0" );
      data1.put("img", R.drawable.q1 );
      resourceNames.add(data1);
      data2.put("line1", "Chelsea Track Top-Womens" );
      data2.put("line2", "$ 399.0" );
      data2.put("img", R.drawable.q2 );
 resourceNames.add(data2);
SimpleAdapter notes = new SimpleAdapter(
        this,
        resourceNames,
        R.layout.row,
        new String[] {"line1","line2", "img" },
        new int[] { R.id.text1, R.id.text2, R.id.img } );
    setListAdapter(notes);
}

我想知道如何从每个列表中的每个复选框中获取值

这是我的row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<CheckBox 

android:id="@+id/checkBox1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></CheckBox>
<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/icon" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="2"
        android:gravity="center_vertical"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:id="@+id/text2"
        android:singleLine="true"
        android:ellipsize="marquee"
    />
</LinearLayout>

感谢。

1 个答案:

答案 0 :(得分:1)

在这里查看http://www.mousetech.com/blog/?p=74Android custom ListView unable to click on items

总结已经汇总的博客,如果列表项包含可聚焦项,则该项将获得点击事件。所以将复选框上的focusable属性设置为false以防止发生这种情况。

相关问题