How to list all installed applications with icon and check box in android

无人久伴 提交于 2019-12-07 03:19:26

The reason why this happens is because your views are being re-used but you do not reset the state of the checkbox in the getView.

I suggest you do something like this:

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

public ApplicationAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
    super(context, textViewResourceId, appsList);
    this.context = context;
    this.appsList = appsList;
    packageManager = context.getPackageManager();

    for (int i = 0; i < appsList.size(); i++) {
        checkList.add(false);
    }
}

@Override
public int getCount() {
    return ((null != appsList) ? appsList.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
    return ((null != appsList) ? appsList.get(position) : null);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.row, null);
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        TextView appName = (TextView) view.findViewById(R.id.app_name);
        TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

        CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_app);
        checkBox.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
        checkBox.setChecked(checkList.get(position); // set the status as we stored it        
        checkBox.setOnCheckedChangeListener(mListener); // set the listener

        appName.setText(data.loadLabel(packageManager));
        packageName.setText(data.packageName);
        iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
}

OnCheckedChangeListener mListener = new OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
         checkList.set((Integer)buttonView.getTag(),isChecked); // get the tag so we know the row and store the status 
     }
};
}

Basically, have a arraylist which stores the status of each checkbox (that corresponds to your data). So for example: When you checkbox at position 1 is clicked, checkList.get(1) is true.

Since views are being re-used you need to store the state of each checkbox with respect to its position. i.e checkbox at position x is checked or not. Then when you do getView() for position x you use the stored state to set the correct checked value.

Update Initialize the checkList with false values.

you should maintain already visited items list in sqlite

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                        if ( isChecked )
                        {
                            // perform logic
                            checkList.set((Integer) buttonView.getTag(), isChecked); // get the tag so we know the row and store the status
                            int position = (int) buttonView.getTag();
                            Toast.makeText(context,String.valueOf(position),Toast.LENGTH_LONG).show();
                            boolean result= help.insertContact(String.valueOf(position));
                            System.out.println(result);
                        }

                }
            });
        if (count == 0){
            count++;
            ids = new ArrayList<String>();
            ids = help.getAllCotacts();
            if (ids.size() > 0) {
                for (int i = 0; i < ids.size(); i++) {
                    checkBox.setChecked(checkList.set(Integer.parseInt(ids.get(i)),true));
                }

                Toast.makeText(context, String.valueOf(ids.get(0)), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "No values in DB", Toast.LENGTH_LONG).show();
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!