1

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);
    }
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Refer this following link. This may help you to solve http://stackoverflow.com/questions/12602426/listview-with-checkbox-radiobutton-textview-and-button-not-working-correctly-in – Finder Apr 17 '14 at 12:03
  • Why are you using 3 different arraylist for handling your data , make custom class which is having all these data types , and use array list of that class – Miral Dhokiya Apr 17 '14 at 12:12
  • @M.J what r u trying to say...? –  Apr 17 '14 at 12:56
  • i am trying to say why are you handling three array list for keeping listview's row's data ,instead of using 1 arraylist having custom modal class which contains data types you want. – Miral Dhokiya Apr 17 '14 at 13:07
  • @M.J thank you for the suggestion but i am new in android development so i think right now i am not able to implement it, if u have any example then refer me a link .. –  Apr 17 '14 at 13:19

3 Answers3

1

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;
}
Sunny Garg
  • 1,073
  • 1
  • 6
  • 13
  • one more question bro i want to add item in array which is checked, is it possible...? –  Apr 17 '14 at 12:48
0

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
                 }

            }
        };
coderVishal
  • 8,369
  • 3
  • 35
  • 56
0

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;
}
Sunny Garg
  • 1,073
  • 1
  • 6
  • 13