带有Checkbox的Android Listview如何访问Checkbox

时间:2013-01-08 12:47:41

标签: android listview checkbox row

您好我有一个带Checkbox的自定义ListView但我不知道如何访问它们。我试过这个:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ViewGroup row = (ViewGroup)view;
        TextView tv = (TextView) row.findViewById(R.id.username);
        CheckBox check = (CheckBox) row.findViewById(R.id.chbUsers);
        check.toggle();

    }
});

但是这项工作。我到处搜索,但我找不到解决办法。这是list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" 
    android:longClickable="true">

    <!--  Bild auf der linken Seite -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/ic_launcher"/>

    </LinearLayout>

    <!-- Name des Benutzers -->
    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Alexander Janjic"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15dip"
        android:textStyle="bold"/>

    <!-- Status des Nutzers -->
    <TextView
        android:id="@+id/status"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/username"
        android:textColor="#343434"
        android:textSize="10dip"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="audiobolle an die Macht!" />

    <!-- Rechts oben (Zuletzt online) -->
    <TextView
        android:id="@+id/lastonline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/username"
        android:gravity="right"
        android:text="11.12.2012 09:14"
        android:layout_marginRight="5dip"
        android:textSize="10dip"
        android:textColor="#10bcc9"
        android:textStyle="bold"/>

     <!-- Checkbox -->
     <CheckBox 
        android:id="@+id/chbUsers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"/>

</RelativeLayout>

这是ListAdapter:

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView username = (TextView)vi.findViewById(R.id.username); // Username
        TextView status = (TextView)vi.findViewById(R.id.status); // Status
        TextView lastonline = (TextView)vi.findViewById(R.id.lastonline); // Last Online
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // Thumb Image

        HashMap<String, String> user = new HashMap<String, String>();
        user = data.get(position);

        // Alle Einträge in die ListView eintragen
        username.setText(user.get(MainActivity.KEY_USERNAME));
        status.setText(user.get(MainActivity.KEY_STATUS));
        lastonline.setText(user.get(MainActivity.KEY_LASTONLINE));
        if (!user.get(MainActivity.KEY_THUMB_URL).equals("NOURL"))
        {
            imageLoader.DisplayImage(user.get(MainActivity.KEY_THUMB_URL), thumb_image);
        }

        return vi;
    }
}

2 个答案:

答案 0 :(得分:1)

你必须在你的getView方法中添加checkbox.setOnCheckedChangeListener然后你可以访问他们在Listview中的位置的复选框......

这是方法..

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub

            }
        });

答案 1 :(得分:0)

您在getView方法

中设置了侦听器 像这样:

viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
      viewHolder.checkbox
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              Model element = (Model) viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });