如何列出所有图标安装的应用程序和Android中复选框 [英] How to list all installed applications with icon and check box in android

查看:120
本文介绍了如何列出所有图标安装的应用程序和Android中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算器我试图开发出可以显示所有的列表安装的应用程序与应用程序的图标,名称和放大器;复选框,在这里我的如下code

Hi stackoverflow I'm trying to develop an application which can display a list of all installed application with their Icon, Name & Check Box, here follows my code

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/app_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="3dp"
    android:scaleType="centerCrop" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingLeft="5dp" >

    <TextView
        android:id="@+id/app_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/app_paackage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" />
</LinearLayout>

<CheckBox
    android:id="@+id/cb_app"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

public class MainActivity extends ListActivity {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;

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

    packageManager = getPackageManager();

    new LoadApplications().execute();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);
    try {
        Intent intent = packageManager
                .getLaunchIntentForPackage(app.packageName);

        if (null != intent) {
            startActivity(intent);
        }
    } catch (ActivityNotFoundException e) {
        Toast.makeText(MainActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
    for (ApplicationInfo info : list) {
        try {
            if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                applist.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return applist;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        listadaptor = new ApplicationAdapter(MainActivity.this,
                R.layout.row, applist);

        return null;
    }

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

    @Override
    protected void onPostExecute(Void result) {
        setListAdapter(listadaptor);
        progress.dismiss();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(MainActivity.this, null,
                "Loading application info...");
        super.onPreExecute();
    }

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

ApplicationAdapter.java

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;

public ApplicationAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
    super(context, textViewResourceId, appsList);
    this.context = context;
    this.appsList = appsList;
    packageManager = context.getPackageManager();
}

@Override
public int getCount() {
    return ((null != appsList) ? appsList.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
    return ((null != appsList) ? appsList.get(position) : null);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.row, null);
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        TextView appName = (TextView) view.findViewById(R.id.app_name);
        TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

        appName.setText(data.loadLabel(packageManager));
        packageName.setText(data.packageName);
        iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
}

问题是,每当我勾出一个复选框键,滚动滚动窗口其他框复选框正在。请帮我解决了这个谜。

Problem is whenever I tick a Check Box and scroll other boxes in the scrolled window Check Box are being selected. Please help me to solve this riddle.

感谢。

推荐答案

之所以出现这种情况是因为你的意见被重新使用,但你不重置在 getView复选框的状态

The reason why this happens is because your views are being re-used but you do not reset the state of the checkbox in the getView.

我建议你做这样的事情:

I suggest you do something like this:

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

public ApplicationAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
    super(context, textViewResourceId, appsList);
    this.context = context;
    this.appsList = appsList;
    packageManager = context.getPackageManager();

    for (int i = 0; i < appsList.size(); i++) {
        checkList.add(false);
    }
}

@Override
public int getCount() {
    return ((null != appsList) ? appsList.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
    return ((null != appsList) ? appsList.get(position) : null);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.row, null);
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        TextView appName = (TextView) view.findViewById(R.id.app_name);
        TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

        CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_app);
        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

        appName.setText(data.loadLabel(packageManager));
        packageName.setText(data.packageName);
        iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
}

OnCheckedChangeListener mListener = new OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
         checkList.set((Integer)buttonView.getTag(),isChecked); // get the tag so we know the row and store the status 
     }
};
}

基本上,具有存储每个复选框的状态(对应于您的数据)一个数组列表。因此,例如:当你在复选框位置 1 点击 checkList.get(1)真正

由于意见正在被重新使用,你需要将每个复选框的状态存储相对于它的位置。即复选框在位置 X 检查与否。然后,当你做 getView()的位置 X 您使用存储的状态设置正确的检查值。

Since views are being re-used you need to store the state of each checkbox with respect to its position. i.e checkbox at position x is checked or not. Then when you do getView() for position x you use the stored state to set the correct checked value.

更新初始化值的清单。

这篇关于如何列出所有图标安装的应用程序和Android中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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