简体   繁体   English

如何将列表视图项添加到整个其他列表视图

[英]How to add a listview item to a whole other listview

So in my android app, I have two scrollable tabs which each contain Listview using a Fragment. 因此,在我的Android应用程序中,我有两个可滚动选项卡,每个选项卡都包含使用Fragment的Listview。 One a list of apps and the other is blank. 一个列表为应用程序,另一个为空白。 What I am aiming to do is add a plus button in place of where my checkbox is and duplicate that item in the other listview which is blank. 我的目的是在我的复选框所在的位置添加一个加号按钮,并在另一个空白的listview中复制该项目。

在此处输入图片说明

在此处输入图片说明

I have done research on this, but I have not found any successful examples on how to implement this. 我已经对此进行了研究,但是没有找到任何成功实施此事的成功例子。

Android - Add an item from one ListView to another ListView? Android-将项目从一个ListView添加到另一个ListView?

Here is my fragment that returns the apps 这是我返回应用程序的片段

package com.spicycurryman.getdisciplined10.app;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ibc.android.demo.appslist.app.ApkAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



public class InstalledAppActivity extends Fragment
        implements OnItemClickListener {

    PackageManager packageManager;
    ListView apkList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.user_installed, container, false);
        packageManager = getActivity().getPackageManager();


        /*To filter out System apps*/

        apkList = (ListView) rootView.findViewById(R.id.applist);

        new LoadApplications(getActivity().getApplicationContext()).execute();


        return rootView;
    }

    /**
     * Return whether the given PackageInfo represents a system package or not.
     * User-installed packages (Market or otherwise) should not be denoted as
     * system packages.
     *
     * @param pkgInfo
     * @return boolean
     */
    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                : false;
    }

    private boolean isSystemPackage1(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) ? false
                : true;
    }


// Don't need in Fragment
/*@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.block, menu);
   // super.onCreateOptionsMenu(menu,inflater);
}*/

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }


    private class LoadApplications extends AsyncTask<Void, Void, Void> {

        Context mContext;

        private ProgressDialog pDialog;
        List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

        public LoadApplications(Context context){
            Context mContext = context;
        }




        @Override
        protected Void doInBackground(Void... params) {

            List<PackageInfo> packageList = packageManager
                    .getInstalledPackages(PackageManager.GET_PERMISSIONS);


          /*  List<ApplicationInfo> list = mContext.getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);


            for(int n = 0;n<list.size();n++){
                if ((list.get(n).flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP))
            }*/


            for(PackageInfo pi : packageList) {
                boolean b = isSystemPackage(pi);
                boolean c = isSystemPackage1(pi);

                if(!b || !c ) {
                    packageList1.add(pi);
                }
            }

            //sort by application name

            final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);

            Collections.sort(packageList1, new Comparator<PackageInfo>() {
                @Override
                public int compare(PackageInfo lhs, PackageInfo rhs) {
                    return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
                }
            });

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(InstalledAppActivity.this.getActivity());
            pDialog.setMessage("Loading your apps...");
            pDialog.show();

        }

        @Override
        protected void onPostExecute(Void result) {

            apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));

            if (pDialog.isShowing()){
                pDialog.dismiss();

            }


            super.onPostExecute(result);
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

}

And here is my adapter class: 这是我的适配器类:

package com.ibc.android.demo.appslist.app;

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

import com.spicycurryman.getdisciplined10.app.R;

import java.util.List;

public class ApkAdapter extends BaseAdapter {


    List<PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;

    public ApkAdapter(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;

        LayoutInflater inflater = context.getLayoutInflater();

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

            holder.apkName = (TextView) convertView
                    .findViewById(R.id.appname);
            holder.ck1 = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

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

        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        // 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, 75, 75);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);
        holder.ck1.setChecked(false);

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

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

        return convertView;

    }




}

How would I go about achieving this? 我将如何实现这一目标?

So what you are doing is, you are populating a listview of installed apps using package manager. 因此,您要做的就是使用软件包管理器填充已安装应用程序的列表视图。 Add a plus button in place of the checkbox. 添加一个加号按钮代替该复选框。 Now get installed apps similar to this - 现在获取与此类似的已安装应用-

    List<PackageInfo> packageList1 = packageManager.getInstalledPackages(0);

    final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);

    Collections.sort(packageList1, new Comparator<PackageInfo>() {
        @Override
        public int compare(PackageInfo lhs, PackageInfo rhs) {
            return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
        }
    });

    for (int i = 0; i < packageList1.size(); i++) {

        PackageInfo PackInfo = packageList1.get(i);
        if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {

            //Add to adapter
        }
    }
}

Now, create a public string array in the activity which is holding the tabs. 现在,在保存标签的活动中创建一个公共字符串数组。 When you click on the plus button add the packagename to the array. 当您单击加号按钮时,将包名称添加到阵列。

Now on the other tab use the same adapter, but here before adding it to the adapter check if it is found in the string array using the index. 现在,在另一个选项卡上,使用相同的适配器,但是在这里,在将其添加到适配器之前,请使用索引检查是否在字符串数组中找到了它。 Something like - 就像是 -

List<PackageInfo> packageList1 = packageManager.getInstalledPackages(0);

final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);

Collections.sort(packageList1, new Comparator<PackageInfo>() {
    @Override
    public int compare(PackageInfo lhs, PackageInfo rhs) {
        return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
    }
});

for (int i = 0; i < packageList1.size(); i++) {

    PackageInfo PackInfo = packageList1.get(i);
    if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
    if (Mainactivity.array contains String PackageName= PackInfo.packageName)
    {
                //Add to adapter
    }
            }
}
    }

To persist the selected apps, add their package name to shared_preferences. 要保留选定的应用程序,请将其程序包名称添加到shared_preferences。

edit : 编辑:

In your fragment's LoadApplications class, you retrieve a list of installed apps. 在片段的LoadApplications类中,检索已安装应用程序的列表。 On the second fragment use the same code, but just add one more condition 在第二个片段上使用相同的代码,但只添加一个条件

    for(PackageInfo pi : packageList) {
            boolean b = isSystemPackage(pi);
            boolean c = isSystemPackage1(pi);

            if(!b || !c ) {

    if (array contains packagename){
    packageList1.add(pi);
            }
        }
List<UserApps> packageListBlocked = new ArrayList<UserApps>();
holder.ck1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (holder.ck1.isChecked())
            itemChecked[position] = true;
            packageListBlocked.add(packageList.get(position));
        else
            itemChecked[position] = false;
    }
});

ArrayList can hold duplicate data, so be cautious while adding. ArrayList可以保存重复的数据,因此添加时请谨慎。

Now you have all checkedItems in packageListBlocked pass it on to next tab and set data, make sure you call adapter.notifyDataSetChanged(); 现在,您在packageListBlocked中具有所有checkedItems, packageListBlocked传递到下一个选项卡并设置数据,确保您调用adapter.notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM