我正在创建一个 Android 应用程序,其中有一个 ListView,它显示了我手机中安装的所有应用程序。
我的ListView是自定义的,它包含了一个Icon、TextView和CheckBox,icon的使用是显示应用的图标,TextView是显示应用的名称,CheckBox的使用是判断里面有什么item我选择的 ListView。
当我单击应用程序中的按钮时,如何确定在 ListView 行中选择的 CheckBox 是什么?我是 Android 新手,所以我不知道我应该采取什么方法。
这是我的代码:
public class AppInfo {
public Drawable icon;
public String applicationName;
public AppInfo(){
super();
}
public AppInfo(Drawable icon, String applicationName){
super();
this.icon = icon;
this.applicationName = applicationName;
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.widget.CheckBox;
public class AppInfoAdapter extends ArrayAdapter<AppInfo> {
Context context;
int layoutResourceId;
AppInfo data[] = null;
public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){
super(context, layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
AppInfoHolder holder= null;
if (row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AppInfoHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imgPackageIcon);
holder.txtTitle = (TextView) row.findViewById(R.id.txtApplicationName);
holder.chkSelect = (CheckBox) row.findViewById(R.id.chkSelect);
row.setTag(holder);
}
else{
holder = (AppInfoHolder)row.getTag();
}
AppInfo appinfo = data[position];
holder.txtTitle.setText(appinfo.applicationName);
holder.imgIcon.setImageDrawable(appinfo.icon);
holder.chkSelect.setChecked(true);
return row;
}
}
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;
public class AppInfoHolder {
ImageView imgIcon;
TextView txtTitle;
CheckBox chkSelect;
}
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.content.pm.PackageInfo;
public class CacheActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cache);
final ListView listApplication = (ListView)findViewById(R.id.listApplication);
ApplicationInfo applicationInfo = getApplicationInfo();
PackageManager pm = getPackageManager();
List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
pInfo.addAll(pm.getInstalledPackages(0));
AppInfo app_info[] = new AppInfo[pInfo.size()];
int counter = 0;
for(PackageInfo item: pInfo){
try{
applicationInfo = pm.getApplicationInfo(item.packageName, 1);
app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo),
String.valueOf(pm.getApplicationLabel(applicationInfo)));
System.out.println(counter);
}
catch(Exception e){
System.out.println(e.getMessage());
}
counter++;
}
AppInfoAdapter adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info);
listApplication.setAdapter(adapter);
}
}