获取选中的客户项目ListViewCheckBox并将其通过按钮传递给新活动

时间:2019-04-12 11:42:24

标签: android

我的目标是在自定义列表视图中显示已安装应用程序的列表     (活动内)没有复选框,而我想通过所有     检查一项活动到另一项活动的项目。我应该如何通过它们?     如何通过选择提示项(ImageView和TextView)     带有按钮的新Activity中的listViewCheckbox。并申请     您可以从新活动中打开它     请帮助我。对不起,这是我第一次输入错误     存货不足

这是我的代码:

class AppInfo
    import android.graphics.drawable.Drawable;


    import android.os.Parcel;
    import android.os.Parcelable;

    import java.io.Serializable;

    class AppInfo implements Parcelable {
      Drawable icon;
            public String applicationName;
            Boolean Selected;
            public AppInfo(){
                super();
            }
            public AppInfo(Drawable icon, String applicationName){
                super();
             this.icon = icon;
                this.applicationName = applicationName;
            }


        protected AppInfo(Parcel in) {
            applicationName = in.readString();
            byte tmpSelected = in.readByte();
            Selected = tmpSelected == 0 ? null : tmpSelected == 1;
        }

        public static final Creator<AppInfo> CREATOR = new Creator<AppInfo>() {
            @Override
            public AppInfo createFromParcel(Parcel in) {
                return new AppInfo(in);
            }

            @Override
            public AppInfo[] newArray(int size) {
                return new AppInfo[size];
            }
        };

        public Drawable getIcon() {
        return icon;
      }

       public void setIcon(Drawable icon) {
          this.icon = icon;
        }

        public String getApplicationName() {
            return applicationName;
        }

        public void setApplicationName(String applicationName) {
            this.applicationName = applicationName;
        }

        public Boolean getSelected() {
            return Selected;
        }

        public void setSelected(Boolean selected) {
            Selected = selected;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(applicationName);
            dest.writeByte((byte) (Selected == null ? 0 : Selected ? 1 : 
          2));
        }
    }

这里是我的类适配器:

public class AppInfoAdapter extends ArrayAdapter<AppInfo>  
     implements 
     CompoundButton.OnCheckedChangeListener
    {
     SparseBooleanArray mCheckStates;


     AppInfo appinfo;
    Context context;
    int layoutResourceId;
    AppInfo  data[] = null;

      public AppInfoAdapter(Context context, int layoutResourceId, 
     AppInfo[]data)
       {
         super(context,layoutResourceId,data);
         this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        mCheckStates = new SparseBooleanArray(data.length);
    }


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

    @Nullable
    @Override
    public AppInfo getItem(int position) {

        return super.getItem(position);
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup 
                   parent){

        View row = convertView;
        AppInfoHolder holder= null;

        if (row == null){

            LayoutInflater inflater = 
         ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new AppInfoHolder();

            holder.imgIcon = (ImageView) 
                  row.findViewById(R.id.imageView1);
            holder.txtTitle = (TextView) 
           row.findViewById(R.id.textView1);
            holder.chkSelect = (CheckBox) 
             row.findViewById(R.id.checkBox1);

            row.setTag(holder);

        }
        else{
            holder = (AppInfoHolder)row.getTag();
        }


       appinfo = data[position];
        holder.txtTitle.setText(appinfo.applicationName);
      holder.imgIcon.setImageDrawable(appinfo.icon);
        holder.chkSelect.getText();

        holder.chkSelect.setTag(position);//done position sue chaq raw
        holder.chkSelect.getTag();

       holder.chkSelect.setChecked(mCheckStates.get(position, false));
        holder.chkSelect.setOnCheckedChangeListener(this);
        return row;

      }


      public boolean isChecked(int position) {

        return mCheckStates.get(position, false);
      }

       public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

      }

       public void toggle(int position) {
        setChecked(position, !isChecked(position));

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

        mCheckStates.put((Integer) buttonView.getTag(), isChecked);

    }


}

Class Main Activity按钮从新活动发送以下内容:

Button Send = (Button) findViewById(R.id.btnsend);
 Send.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {


            SparseBooleanArray checked = 
        listApplication.getCheckedItemPositions();
           ArrayList<AppInfo> selectedItems = new ArrayList<AppInfo>();
            for (int j = 0; j < adapter.mCheckStates.size(); j++) {
             if (adapter.mCheckStates.get(j) == true) {
                     selectedItems.add(adapter.getItem(j));
                }
           AppInfo[] outputStrArr = new AppInfo[selectedItems.size()];
             for (int i = 0; i < selectedItems.size(); i++) {
              outputStrArr[i] = selectedItems.get(i);

             }

         Intent intent = new Intent(getApplicationContext(), 
       NextActivity.class);
        intent.putExtra("list",output);
        startActivity(intent);
         }});

           ApplicationInfo applicationInfo = getApplicationInfo();
          final PackageManager pm = getPackageManager();
          final List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
          pInfo.addAll(pm.getInstalledPackages(0));
          app_info = new AppInfo[pInfo.size()];

        int counter = 0;
        for (PackageInfo item : pInfo) {
            try {

                applicationInfo = 
              pm.getApplicationInfo(item.packageName, 1);

                app_info[counter] = new 
              AppInfo(pm.getApplicationIcon(applicationInfo),

            String.valueOf(pm.getApplicationLabel(applicationInfo)));

                System.out.println(counter);

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

            counter++;
        }

        adapter = new AppInfoAdapter(this,
                R.layout.installed_app_list, app_info);
        listApplication.setAdapter(adapter);
               }}

下一个活动的结束语此代码:     公共类NextActivity扩展了AppCompatActivity {

    Context context;
    Bundle pInfo ;
    AppInfo[] outputStrArr;
    ArrayList<AppInfo> select;
    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        ListView lv=findViewById(R.id.lv);
         AppInfo[] result = (AppInfo[]) 
         getIntent().getParcelableArrayExtra("list");
        listadaptor = new AppInfoAdapter(NextActivity.this, 
         R.layout.row, result);
        lv.setAdapter(listadaptor);}}

1 个答案:

答案 0 :(得分:0)

Custom ListView

当您单击复选框时,将存储包名称命名为任何数组或模型类

将通行数据添加到第二个活动

Start app using Package name