如何在android中的列表视图项中处理复选框事件

时间:2014-04-17 12:00:56

标签: java android listview android-listview android-adapter

我开发了一个具有一个列表视图的应用程序,该列表视图的一个项目包含一个图像视图一个文本视图和一个复选框,所以我的问题是如何处理特定列表视图项目的复选框已选中和未选中事件

列表视图的适配器类

package com.rk.test_facebook;

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.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ListAdapter extends BaseAdapter {

    private ArrayList<String> imageUrl;
    private final ArrayList<String> name;
    private final ArrayList<String> birthday;
    private Activity activity;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public ListAdapter(Activity activity, ArrayList<String> imageUrl, ArrayList<String> birthday, ArrayList<String> name) {
        this.activity = activity;
        this.imageUrl = imageUrl;
        this.name = name;
        this.birthday = birthday;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return name.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 view = convertView;
        if (convertView == null)
            view = inflater.inflate(R.layout.listelement, null);
        TextView text = (TextView) view.findViewById(R.id.text);

        ImageView image = (ImageView) view.findViewById(R.id.image);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
        text.setText((name.get(position)));
        imageLoader.DisplayImage(imageUrl.get(position), image);
        return view;
    }
}

主要活动

package com.rk.test_facebook;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

import Handler.DatabaseHandler;
import Property.FriendsProperty;

/**
 * Created by Intex on 15/04/2014.
 */
public class FriedsList extends Activity {
    private ListView listView;

    public static ArrayList<String> arrayListName = new ArrayList<String>();
    public static ArrayList<String> arrayListBirthday = new ArrayList<String>();
    public static ArrayList<String> arrayListImage = new ArrayList<String>();
    private ListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendslist);

        listView = (ListView) findViewById(R.id.ListView);
        DatabaseHandler databaseHandler = new DatabaseHandler(this);
        ArrayList<FriendsProperty> properties = databaseHandler.DislpayFriends();

        for (FriendsProperty friendsProperty : properties) {
            arrayListName.add(friendsProperty.getName());
            arrayListImage.add(friendsProperty.getImage());
            arrayListBirthday.add(friendsProperty.getBirthday());
        }
        databaseHandler.close();
        adapter = new ListAdapter(this, arrayListImage, arrayListBirthday, arrayListName);
        listView.setAdapter(adapter);
    }
}

3 个答案:

答案 0 :(得分:0)

更改代码getView()回调如下,

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null)
        view = inflater.inflate(R.layout.listelement, null);
    TextView text = (TextView) view.findViewById(R.id.text);

    ImageView image = (ImageView) view.findViewById(R.id.image);
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        // it is check
                    } else {
                        // it is unchecked
                    }
                }
            });



   text.setText((name.get(position)));
   imageLoader.DisplayImage(imageUrl.get(position), image);
   return view;
}

答案 1 :(得分:0)

getView()中的方法必须添加setOnCheckedChangeListener,如下所示

    checkBox.setOnCheckedChangeListener(starCheckedChangeListener);
}

private  OnCheckedChangeListener starCheckedChangeListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                   //DO WHEN CHECKED
                }
                else
                {
                    //Do when not checked
                 }

            }
        };

答案 2 :(得分:0)

将代码更改为以下,以在数组中添加选中的项目。

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null)
    view = inflater.inflate(R.layout.listelement, null);
    TextView text = (TextView) view.findViewById(R.id.text);

    ImageView image = (ImageView) view.findViewById(R.id.image);
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);

   checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    selectedItemArrayList.add(dataArrayList.get(position));
                } else {
                    selectedItemArrayList.remove(dataArrayList.get(position));
                }
            }
        });



   text.setText((name.get(position)));
    imageLoader.DisplayImage(imageUrl.get(position), image);
   return view;
}