我正在创建一个安卓应用程序,其中有一个 ListView,它显示了我手机上安装的所有应用程序。
我的 ListView 是自定义的,包含图标、文本视图和复选框,图标用于显示应用程序的图标,文本视图用于显示应用程序的名称,复选框用于确定我在 ListView 中选择的项目。
当我在应用程序中点击按钮时,如何确定我选择的 ListView 行中的复选框是哪一个?由于我是 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);
}
}