在我的自定义Listview中,当我点击列表的任何行时,复选框没有被点击。请提示是否有?

时间:2014-02-10 14:18:12

标签: android-listview android-checkbox

这是我的MainActivity.java

public class MainActivity extends Activity 
{
    ListView list;
    final String[] values={"Eminem","Cold Play","Mike Shinoda","Switchfoot","Will.I.A.M"};
    final String[] sub={"Rap God","Paradise","Believe","Stars","B**m"};
    Integer[] imageId = {
        R.drawable.eminem,
        R.drawable.coldplay,
        R.drawable.mike,
        R.drawable.switchfoot,
        R.drawable.will
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyArrayAdapter adapter = new
            MyArrayAdapter(MainActivity.this,values,sub,imageId);
        list=(ListView)findViewById(R.id.listView1);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        // list.setAdapter(adapter);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +values[+ position], Toast.LENGTH_SHORT).show();
            }
        });

    }
}

MyArrayAdapter.java

public class MyArrayAdapter extends ArrayAdapter<String> {
    private CheckBox ch;
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    private String[] sub;
    //private final String[] sub;

    public MyArrayAdapter(Activity context, String[] web,String[] sub,Integer[] imageId) 
    {
        super(context, R.layout.layout, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;
        this.sub=sub;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.layout, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
        TextView tx=(TextView)rowView.findViewById(R.id.textView2);
        ch=(CheckBox) rowView.findViewById(R.id.check); 
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
        txtTitle.setText(web[position]);
        tx.setText(sub[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:choiceMode="singleChoice"
        android:dividerHeight="0.5px"
        android:divider="#318CE7" />

</RelativeLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:padding="5dp" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox 
        android:id="@+id/check" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />
</RelativeLayout>

代码工作正常,只是CheckBox被检查为一个单独的视图(不是整行),并帮助我如何捕获已检查的数据。

0 个答案:

没有答案