如何在列表视图中显示已安装的应用程序列表,而不会在滚动列表视图时取消选中复选框? [英] How to show installed app list in a listview without Checkbox getting unchecked when listview is scrolled?

查看:10
本文介绍了如何在列表视图中显示已安装的应用程序列表,而不会在滚动列表视图时取消选中复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在自定义列表视图中(在 Activity 内)显示已安装的应用程序列表,而不会取消选中复选框(滚动列表视图时).

My goal is to display the list of installed apps in a custom listview (within an Activity) without checkbox getting unchecked (when listview is scrolled).

问题:滚动列表视图时,复选框的选中状态变为未选中状态.

我目前的进展:而 这个博客entry 清楚地展示了如何摆脱 listview 中的复选框被取消选中的问题,不同之处在于它使用 Model 作为自定义类,而在我的情况下它是一个系统类 [即packageList1 = packageManager.getInstalledPackages(0);]

My Progress So Far: While this blog entry clearly shows how to get rid of the problem of checkboxes in listview getting unchecked, the difference is that it uses Model as the custom class whereas in my case it is a system class [i.e. the packageList1 = packageManager.getInstalledPackages(0);]

我该如何解决这个问题?下面是我的代码尝试.我能够在带有复选框的列表视图中成功显示应用程序,只是在滚动列表视图时未选中选中的状态.

How can I resolve this? Below is my code attempt. I am able to successfully display apps in a listviw with checkboxes, just that checked status is getting unchecked when listview is scrolled.

我愿意接受任何方法作为答案,使我能够在列表视图中显示已安装的应用程序列表,而不会取消选中复选框的已检查状态

我的自定义列表适配器类:

public class Listadapter extends BaseAdapter {

private static String TAG = "focus";
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;

List<String> appNamestoBlock_local;

public Listadapter(Activity context, List<PackageInfo> packageList,
        PackageManager packageManager, List<String> appNamestoBlock) {
    super();
    this.context = context;
    this.packageList = packageList;
    this.packageManager = packageManager;
    this.appNamestoBlock_local = appNamestoBlock;

    itemChecked = new boolean[packageList.size()];
}

private String getSerializedBlockedAppNames() {
    String serializedBlockedAppNames;
    Log.v(TAG,
            "-------- List<String> list = "
                    + TextUtils.join(",", appNamestoBlock_local));
    serializedBlockedAppNames = TextUtils.join(",", appNamestoBlock_local);
    return serializedBlockedAppNames;
}

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.list_item, null);
        holder = new ViewHolder();

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

        holder.ck1.setFocusable(false);
        convertView.setTag(holder);

    } else {

        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, 40, 40);
    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);

    // Log.v(TAG,
    // "-------- checking in getView() in ListAdapter if appName is in getSerializedBlockedAppNames()");
    if (getSerializedBlockedAppNames().contains(appName)) {
        Log.v(TAG + " ListAdapter",
                "-------- YES, appName is in getSerializedBlockedAppNames(), appName = "
                        + appName);
        holder.ck1.setChecked(true);
    }

    holder.ck1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                itemChecked[position] = true;
            } else {
                itemChecked[position] = false;
            }
        }
    });

    return convertView;

}

}

在我的主要活动的 onCreate 函数中:

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

Listadapter Adapter = new Listadapter(MainActivityCircularSeekbar.this, packageList1, packageManager, blockedApps);

推荐答案

Activity

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

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.widget.ListView;

public class AppScreen extends Activity {
private ListView list;
ArrayList<Datamodel> res;
MyAdapter _adapter;

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

    list = (ListView) findViewById(R.id.list);
    List<PackageInfo> _myapps = getPackageManager().getInstalledPackages(0);
    res = new ArrayList<Datamodel>();
    for (int i = 0; i < _myapps.size(); i++) {
        PackageInfo p = _myapps.get(i);

        Datamodel _model  = new Datamodel();
        _model.setAppname(p.applicationInfo.loadLabel(getPackageManager()).toString());
        res.add(_model);
        System.out.println("ajajajja" + res.size() + res.get(i).getAppname());
    }
    _adapter = new MyAdapter(getApplicationContext(), res);
    _adapter.notifyDataSetChanged();
    list.setAdapter(_adapter);


}

}

数据模型类

public class Datamodel {

public String appname = "";
private boolean selected;

public String getAppname() {
    return appname;
}

public void setAppname(String appname) {
    this.appname = appname;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean isChecked) {
    this.selected = isChecked;
}

}

适配器类

public class MyAdapter extends BaseAdapter {
Context _ctx;
LayoutInflater inflater;
public ArrayList<Datamodel> data;

public MyAdapter(Context c, ArrayList<Datamodel> _arraylist) {
    this._ctx = c;
    this.data = _arraylist;
}

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

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    inflater = (LayoutInflater) _ctx.getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.inflator, parent, false);
    final CheckBox checks = (CheckBox) itemView.findViewById(R.id.b);
    final TextView _setappname = (TextView) itemView.findViewById(R.id.a);
    checks.setChecked(data.get(position).isSelected());

    Datamodel obj = data.get(position);
    _setappname.setText(obj.getAppname());
    checks.setTag(position);

    checks.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {                    
            int getPosition = (Integer) buttonView.getTag();
            Datamodel _obj = data.get(getPosition);
            data.get(position).setSelected(isChecked);
            String ss = _obj.getAppname();
            System.out.println("pos is" + getPosition);

        }
    });

    return itemView;
}

}

活动 XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.installedapps.AppScreen" >

<ListView 
    android:id="@+id/list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

</ListView>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2" >

<TextView
    android:id="@+id/a"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:textColor="#000"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_weight="1.7" />

<CheckBox
    android:id="@+id/b"
    android:layout_width="0dp"
     android:layout_gravity="center"
    android:gravity="center"
    android:layout_weight="0.3"
    android:layout_height="wrap_content" />

</LinearLayout>

这篇关于如何在列表视图中显示已安装的应用程序列表,而不会在滚动列表视图时取消选中复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆