4

I refered to these links link , link , link , link

but my problem is not getting solved. i know there are many question regarding this issue but my problem is

i have 4 class A,B,C,D

From class A i goto Class B where all installed apps listed in listview. in Class B i send Packagelist to Class c (BaseAdapter class) and then which apps user have selected will pass to Class A.

So my problem is where or how can i use POJO class to pass installed Apps to baseadapter class and then how i can get all checked value(string app name) and pass it to the Class A. and also how i can maintain checked status of check box.

any help plz.

custom Adapter class

import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class AppListAdapter extends BaseAdapter {

    List<PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;
    int checkBoxCounter = 0;
    int checkBoxInitialized = 0;

    public AppListAdapter(Activity context, List<PackageInfo> packageList,
            PackageManager packageManager) {
        super();
        this.context = context;
        this.packageList = packageList;
        this.packageManager = packageManager;
        itemChecked = new boolean[packageList.size()];
    }

    private class ViewHolder {
        TextView apkName;
        CheckBox ck1;
    }

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

    public Object getItem(int position) {
        return packageList.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    //  final ViewHolder holder;
        checkBoxCounter = 0;
        checkBoxInitialized = 0;

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.apklist_item, null);
            final ViewHolder holder = new ViewHolder();

            holder.apkName = (TextView) convertView
                    .findViewById(R.id.ApkList_tvappname);
            holder.ck1 = (CheckBox) convertView
                    .findViewById(R.id.ApkList_checkBox);

            holder.ck1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                            AppsSelected element = (AppsSelected) holder.ck1
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                            if (checkBoxCounter <= checkBoxInitialized) {
                                // increment counter, when we scroll the List it
                                // execute onCheckedChanged everytime so by
                                // using this stuff we can maintain the state
                                checkBoxCounter++;
                            } else {
                                element = (AppsSelected) holder.ck1.getTag();
                                element.setSelected(buttonView.isChecked());

                                if (element.isSelected())
                                    Toast.makeText(
                                            context,
                                            "You selected " + element.getName(),
                                            Toast.LENGTH_LONG).show();
                                else
                                    Toast.makeText(
                                            context,
                                            "Not selected " + element.getName(),
                                            Toast.LENGTH_LONG).show();
                            }

                        }
                    });

            convertView.setTag(holder);
            holder.ck1.setTag(packageList.get(position));

        } else {
        ((ViewHolder) convertView.getTag()).ck1.setTag(packageList.get(position));
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        PackageInfo packageInfo = (PackageInfo) getItem(position);

        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 40, 40);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);


        return convertView;

    }

}
Community
  • 1
  • 1
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55

1 Answers1

4

You can refer to my answer here, what you can do is,store check box state in a boolean array then you wont face this issue.....

Android checkbox multiselected issue

Community
  • 1
  • 1
Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
  • i have done that way also but then how can i get all checked value in one arraylist and then pass it to the my Class A. ?? – Wasim K. Memon Sep 14 '13 at 08:40
  • 1
    i have solved this issue. check out my tutorial for easy solution. http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html – Wasim K. Memon Apr 24 '14 at 17:04