android中如何获得listview中的checkbox的值?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中如何获得listview中的checkbox的值?相关的知识,希望对你有一定的参考价值。

方案:

自定义ListView行布局,并在布局中添加CheckBox对象,在Adaper获取行信息的时候,给每个CheckBox添加事件,在事件中修改数据集合。


代码:


结果:




参考技术A 大概情景:在做一个文件浏览器。listview显示所有的文件。如果是文件,点击打开。如果是文件夹,递归显示目录下的文件。遇到的问题:通过listview和checkbox实现批量删除。checkbox初始状态gone,用户选择批量操作后,显示checkbox,然后选择要删除的item。这里如果在xml中设置checkbox的属性android:clickable=false,会影响到文件的操作,比如会弹出打开或者递归目录的问题。如果不设置,响应事件又被checkbox截获,没法得到item的值。后来,有想到可以直接在用户做出最终操作时去遍历所有item并判断checkbox状态,但是这里不知道如果得到listview中checkbox对象。所以想大家请教一下应该如何处理。做过的努力:已经找了写例子看了,多是checkbox直接就是显示出来的,而且也是单独的demo,并没有和具体的业务连起来,不太合适。本回答被提问者采纳

如何获得Android手机的软件安装列表

参考技术A packageManager.getInstalledApplications()返回一个列表都是安装在设备上的应用程序包。如果我们把 flag GET_UNINSTALLED_PACKAGES设定,一个列表中的所有应用,包括那些设置为dont_delete_data(部分已安装的应用程序的数据目录)将返回。

你可以看到附件中的截图,我们将创建一个列表显示所有已安装的应用程序。

snippet_list_row.xml--------------------这个布局是由ListView适配器用于表示应用程序的细节。它显示应用程序图标、应用程序名称和应用程序包。
<?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" > <ImageView android:id="@+id/app_icon" android:layout_width="50dp" android:layout_height="50dp" android:padding="3dp" android:scaleType="centerCrop" /> <LinearLayout android:layout_width="fill_parent" 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></LinearLayout>

AllAppsActivity.java ##这是用于初始化和列表已安装的应用程序的主要应用程序类。从packagemanage得到应用细节列表是一个耗时的任务,我们将在AsyncTask里面做。同时,类使用自定义适配器“自定义listview applicationadapter”。 package com.javatechig.listapps;import java.util.ArrayList;import java.util.List;import android.app.AlertDialog;import android.app.ListActivity;import android.app.ProgressDialog;import android.content.ActivityNotFoundException;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.AsyncTask;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.ListView;import android.widget.Toast;public class AllAppsActivity 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(); public boolean onCreateOptionsMenu(Menu menu) MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; public boolean onOptionsItemSelected(MenuItem item) boolean result = true; switch (item.getItemId()) case R.id.menu_about: displayAboutDialog(); break; default: result = super.onOptionsItemSelected(item); break; return result; private void displayAboutDialog() final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.about_title)); builder.setMessage(getString(R.string.about_desc)); builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int id) Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stacktips.com")); startActivity(browserIntent); dialog.cancel(); ); builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int id) dialog.cancel(); ); builder.show(); @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(AllAppsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); catch (Exception e) Toast.makeText(AllAppsActivity.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(AllAppsActivity.this, R.layout.snippet_list_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(AllAppsActivity.this, null, "Loading application info..."); super.onPreExecute(); @Override protected void onProgressUpdate(Void... values) super.onProgressUpdate(values);

package com.javatechig.listapps;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.snippet_list_row, null); ApplicationInfo applicationInfo = appsList.get(position); if (null != applicationInfo) 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(applicationInfo.loadLabel(packageManager)); packageName.setText(applicationInfo.packageName); iconview.setImageDrawable(applicationInfo.loadIcon(packageManager)); return view; ;

以上是关于android中如何获得listview中的checkbox的值?的主要内容,如果未能解决你的问题,请参考以下文章

我如何获得要提升的ListView项?

如何获得 ListView GridViewColumn 来填充网格中的剩余空间?

如何获得Android手机的软件安装列表

如何使用 Flutter 从 Firestore 中的 ListView 获得无限滚动

Android-Listview中Bitmap的缓存实现有哪些方式

Android控件ListView获取item中EditText值