我有一个ApplicationInfo类型的列表和另一个String类型的ArrayList。我想在listView中列出这两个。我可以将ArrayAdapter(ApplicationInfo)和ArrayAdapter(String)扩展到ApplicationAdapter.class吗?这是我当前的代码,仅显示ApplicationInfo。
ApplicationAdapter.class
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
public ImageView addview;
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 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);
addview = (ImageView) view.findViewById(R.id.add_icon);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
addview.setImageResource(R.drawable.ads);
}
return view;
}
}
AllAppsActivity.class
public class AllAppsActivity extends ListActivity implements Serializable {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;
public ImageView addview;
public ArrayList<String> addblock_list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mboolean = false;
addblock_list = new ArrayList<String>(100);
for(int i = 0; i < 100; i= i + 1) {
addblock_list.add("addblock");
}
}
@Override
protected void onResume() {
packageManager = getPackageManager();
new LoadApplications().execute();
super.onResume();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
addview = (ImageView) v.findViewById(R.id.add_icon);
ApplicationInfo app = applist.get(position);
if(addblock_list.get(position).equals(app.packageName)) {
addview.setImageResource(R.drawable.ads);
addblock_list.set(position, "addblock");
Toast.makeText(AllAppsActivity.this, "removed "+app.packageName,Toast.LENGTH_SHORT).show();
} else {
addview.setImageResource(R.drawable.adsblock);
addblock_list.set(position, app.packageName);
Toast.makeText(AllAppsActivity.this, "added "+app.packageName,Toast.LENGTH_SHORT).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);
}
}
}
我检查了互联网,我想我必须使用类似的东西:
我在AllAppsActivity Class
中更改了我的代码 @Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent( packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
HashMap<Integer,Object> yourHash = new HashMap<Integer,Object>();
yourHash.put(1,applist);
yourHash.put(2,addblock_list);
listadaptor = new ApplicationAdapter(AllAppsActivity.this,
R.layout.snippet_list_row,yourHash);
return null;
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
我的ApplicationAdapter.class
public class ApplicationAdapter extends ArrayAdapter<HashMap<Integer,Object>>{
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
public ImageView addview;
private ArrayList<String> addblock_list;
private HashMap<Integer,Object> yourHash;
public ApplicationAdapter(Context context, int textViewResourceId,
HashMap<Integer,Object> yourHash) {
// TODO Auto-generated constructor stub
super(context,textViewResourceId);
this.context = context;
this.yourHash = yourHash;
packageManager = context.getPackageManager();
List<ApplicationInfo> appsList = (List<ApplicationInfo> ) yourHash.get(1);
ArrayList<String> addblock_list=( ArrayList<String> ) yourHash.get(2);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}..........
我是android和java的新手。
请通过告诉我如何做到这一点来启发我,给我发送代码。在此先感谢。
答案 0 :(得分:1)
hashmap是一种容器类型,可以保存不同的对象,按标记排序。 你可以在静态类上设置hashmap,从任何地方访问它,或者通过参数将它传递给你的适配器(推荐)。
看看官方文档:
http://developer.android.com/reference/java/util/HashMap.html
示例实现:
// create an arrayList to store values
35
ArrayList<String> listOne = new ArrayList<String>();
36
listOne.add("Blue");
37
listOne.add("Black");
38
listOne.add("Brown");
39
40
// create list two and store values
41
ArrayList<String> listTwo = new ArrayList<String>();
42
listTwo.add("Pink");
43
listTwo.add("Purple");
44
45
// put values into map
46
multiMap.put("B color", listOne);
47
multiMap.put("P color", listTwo);
在这里你可以找到解决不同类型问题的类似问题:
Hashmap holding different data types as values for instance Integer, String and Object