退出后保存listview项目

时间:2014-08-08 06:33:55

标签: android

你好朋友我有listview,其中包含所有安装应用程序和一个checkbox.my问题是,当我使用复选框检查项目时,它不会永久保留,即如果我使用复选框检查某个项目并退出应用程序然后运行应该显示它应该显示以前检查过的所有项目。

这里是我的包ApkListActivity.java

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

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

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

public class ApkListActivity extends Activity {

    PackageManager packageManager;
    ListView apkList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

    /* To filter out System apps */
    for (PackageInfo pi : packageList) {
        boolean b = isSystemPackage(pi);
        if (!b) {
        packageList1.add(pi);
        }
    }

    Collections.sort(packageList1, new Comparator<PackageInfo>() {
        public int compare(PackageInfo o1, PackageInfo o2) {
        return o1.applicationInfo
            .loadLabel(getPackageManager())
            .toString()
            .compareTo(
                o2.applicationInfo.loadLabel(
                    getPackageManager()).toString());
        }
    });
    apkList = (ListView) findViewById(R.id.applist);
    apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));
    apkList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long row) {
        PackageInfo packageInfo = (PackageInfo) parent
            .getItemAtPosition(position);
        AppData appData = (AppData) getApplicationContext();
        appData.setPackageInfo(packageInfo);

        // Intent appInfo = new Intent(getApplicationContext(),
        // ApkInfo.class);
        // startActivity(appInfo);
        PackageInfo country = (PackageInfo) parent
            .getItemAtPosition(position);
        Toast.makeText(
            getApplicationContext(),
            "Clicked on Row: "
                + country.applicationInfo.loadLabel(
                    getPackageManager()).toString(),
            Toast.LENGTH_LONG).show();

        }
    });

    }

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

}

这是我的ApkAdapter.java

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
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.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ibc.android.demo.appslist.activity.R;

public class ApkAdapter extends BaseAdapter {

    List<PackageInfo> packageList = null;
    Activity context;
    Context context2;
    PackageManager packageManager;
    private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

    boolean selected = false;

    // List<ApplicationInfo> packageList = new ArrayList<ApplicationInfo>();

    public ApkAdapter(Activity context, List<PackageInfo> packageList,
        PackageManager packageManager) {
    super();
    this.context = context;
    this.packageList = packageList;
    this.packageManager = packageManager;

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

        checkList.add(selected);

    }

    }

    public int getCount() {
    return ((null != packageList) ? packageList.size() : 0);
    // return packageList.size();
    }

    public PackageInfo getItem(int position) {
    return ((null != packageList) ? packageList.get(position) : null);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (null == view) {
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.apklist_item, null);
        // holder = new ViewHolder();

    }
    PackageInfo data = packageList.get(position);
    if (null != data) {

        TextView apkName = (TextView) view.findViewById(R.id.appname);

        ImageView apkicon = (ImageView) view.findViewById(R.id.icon_image);
        TextView packageName = (TextView) view
            .findViewById(R.id.packagename);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
        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
        PackageInfo packageInfo = (PackageInfo) getItem(position);

        Drawable appIcon = packageManager
            .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
            packageInfo.applicationInfo).toString();
        ;
        apkicon.setImageDrawable(appIcon);
        apkName.setText(appName);
        packageName.setText(packageInfo.packageName);

    }

    return view;

    }

    OnCheckedChangeListener mListener = new OnCheckedChangeListener() {

    public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
        checkList.set((Integer) buttonView.getTag(), isChecked);
    }
    };

}

1 个答案:

答案 0 :(得分:1)

您可以以简单的1010101格式保存布尔值列表,然后将此整数保存在SharedPreferences中。当您的应用程序打开时,只需使用整数重新填充您的复选框。