添加或删除已存储链表中的一项数据

yuchu 发布于 2013/04/09 14:36
阅读 155
收藏 1
package com.example.myapp;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.Toast;


public class MainActivity extends Activity {

public Context mcontext = this;
static public List<ResolveInfo> packs = null;
    static public MyAdapter myAdapter = null;
    static public ResolveInfo info;
    static public List<Map<String, Object>> listItems;

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

GridView gridview = (GridView) findViewById(R.id.gridView); 
packs = getData();
   myAdapter = new MyAdapter(packs, this);
   gridview.setAdapter((ListAdapter) myAdapter); 

gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ResolveInfo appInfo = (ResolveInfo)arg0.getItemAtPosition(arg2);
Intent mainIntent = mcontext.getPackageManager()
.getLaunchIntentForPackage(appInfo.activityInfo.packageName);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


try {
// launcher the package
mcontext.startActivity(mainIntent);
} catch (ActivityNotFoundException noFound) {
Toast.makeText(mcontext, "Package not found!", Toast.LENGTH_SHORT).show();
}
}

});   
}
    public List<ResolveInfo> getData(){
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
   PackageManager pm = getPackageManager();
   
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
List<ResolveInfo> packs = pm.queryIntentActivities(mainIntent, 0);

for(ResolveInfo info : packs){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("icon", info.loadIcon(pm));
   map.put("name", info.loadLabel(pm));

listItems.add(map); //添加到列表
}
return packs;
}
}

如何添加或删除packs里面的一项数据?
加载中
返回顶部
顶部