Question

i have developed an application that have one list view and that list view's one item contain one image view one text view and one check box so my question is how to handle check box checked and unchecked event of particular list view item

Adapter class for list view

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;
    }
}

main activity

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);
    }
}
Was it helpful?

Solution

Change the code getView() call back as below,

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;
}

OTHER TIPS

The method in the getView() you must add an setOnCheckedChangeListener as follows

    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
                 }

            }
        };

Change the code to below to add the checked item in array.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top