将listadapter用于多个活动

时间:2016-07-24 08:05:51

标签: java android listview checkbox

我有一个带复选框的自定义列表视图。它工作正常。当我点击任何项目时,它的值存储到arraylist,当我再次单击值时,将从数组列表中删除。它正在工作,但当我点击复选框时,它的值不存储到arraylist。我想使用复选框setOnCheckedChangeListener来处理复选框,但问题是我只有一个适配器,我想使用此适配器而不是活动。请告诉我,我可以在活动中使用setOnCheckedChangeListener吗?如果我可以,我怎么能在活动中获得位置和textview和复选框,请告诉我一些代码或编辑我的代码,谢谢。 这是我的适配器

public class Listadapter extends BaseAdapter {
    CheckBox checkBox;
    boolean index[];
    boolean[] itemChecked;
    ResolveInfo entry;
    String[] itempkg;
    private Context mContext;
    private List<ResolveInfo> mListAppInfo;
    private PackageManager mPackManager;
    private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

    public Listadapter(Context applicationContext, List<ResolveInfo> installedApplication, PackageManager packageManager)
    {
        //super(applicationContext,textViewResourceId,installedApplication);
        super();
        this.mContext = applicationContext;
        this.mListAppInfo = installedApplication;
        index = new boolean[installedApplication.size()];
        this.mPackManager = packageManager;
        for (int i = 0; i < installedApplication.size(); i++) {
            checkList.add(false);
            itemChecked = new boolean[installedApplication.size()];
            itempkg = new String[installedApplication.size()];
        }
    }
    @Override
    public int getCount()
    {
        return mListAppInfo.size();
        //return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
    }
    @Override
    public Object getItem(int position)
    {
        // index = new boolean[mListAppInfo.size()];
        return mListAppInfo.get(position);
    }
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // get the selected entry

        final ViewHolder holder;

        //  LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            // reference to convertView
            holder.tvAppName = (TextView) convertView
                    .findViewById(R.id.textView1);
            holder.tvPkgName = (TextView) convertView
                    .findViewById(R.id.textView);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.ivAppIcon = (ImageView) convertView
                    .findViewById(R.id.imageView);

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

        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        entry = mListAppInfo.get(position);
        holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
        holder.tvAppName.setText(entry.loadLabel(mPackManager));
        holder.tvPkgName.setText(entry.activityInfo.packageName);
        holder.checkBox.setChecked(false);

        if (itemChecked[position])
            holder.checkBox.setChecked(true);
        else
            holder.checkBox.setChecked(false);

        holder.checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (holder.checkBox.isChecked()) {
                    itemChecked[position] = true;
                } else {
                    itemChecked[position] = false;
                }
            }
        });

        return convertView;
    }

    private class ViewHolder
    {
        ImageView ivAppIcon;
        TextView tvAppName;
        TextView tvPkgName;
        CheckBox checkBox;
    }
}

这是活动代码。

public class Profile5Activity extends Activity implements AdapterView.OnItemClickListener {

        ListView apps5;
        PackageManager packageManager5;
        static ArrayList<String> checkedValue5;
        Button bt5;
        ResolveInfo pi5 = new ResolveInfo();
        Context context = this;
        CheckBox cb5;
        Listadapter Adapter5 = null;
        boolean[] itemChecked5;

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

         itemChecked5 = new boolean[AllApps.getInstalledApplication(this).size()];

            checkedValue5 = new ArrayList<String>();
        }
        apps5 = (ListView) findViewById(R.id.list5);
        apps5.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        packageManager5 = getPackageManager();

        List<String> list = new ArrayList<String>();

        Adapter5 = new Listadapter(this, AllApps.getInstalledApplication(this), packageManager5);
        apps5.setAdapter(Adapter5);
        apps5.setOnItemClickListener(this);

    }
@Override
    public void onItemClick(AdapterView<?> arg0, View v, final int position, long arg3)
    {
        // TODO Auto-generated method stub
        cb5 = (CheckBox) v.findViewById(R.id.checkBox1);
        final TextView tv5 = (TextView) v.findViewById(R.id.textView);
        final TextView tvv5 = (TextView) v.findViewById(R.id.textView1);
        pi5 = (ResolveInfo) arg0.getItemAtPosition(position);
        cb5.performClick();

        if (cb5.isChecked()) {
            checkedValue5.add(tv5.getText().toString());
            itemChecked5[position] = true;
            Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();

        } else if (!cb5.isChecked()) {
            checkedValue5.remove(tv5.getText().toString());
            Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();
        }

    }

0 个答案:

没有答案