1. 程式人生 > >Android開發之listView使用(手機應用列表顯示)

Android開發之listView使用(手機應用列表顯示)

public class MainActivity extends Activity implements OnItemLongClickListener {

	private ListView lv_main;
	private List<AppInfo> data;
	private AppAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//初始化成員變數
		lv_main = (ListView) findViewById(R.id.lv_main);
		data = getAllAppInfos();
		adapter = new AppAdapter();
		//顯示列表
		lv_main.setAdapter(adapter);
		
		//給ListView設定item的點選監聽
		lv_main.setOnItemClickListener(new OnItemClickListener() {
			/**
			 * parent : ListView
			 * view : 當前行的item檢視物件
			 * position : 當前行的下標
			 */
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//提示當前行的應用名稱
				String appName = data.get(position).getAppName();
				//提示
				Toast.makeText(MainActivity.this, appName, 0).show();
			}
		});
		
		//給LitView設定Item的長按監聽
		lv_main.setOnItemLongClickListener(this);
	}
	
	
	class  AppAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		//返回帶資料當前行的Item檢視物件
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			//1. 如果convertView是null, 載入item的佈局檔案
			if(convertView==null) {
				Log.e("TAG", "getView() load layout");
				convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
			}
			//2. 得到當前行資料物件
			AppInfo appInfo = data.get(position);
			//3. 得到當前行需要更新的子View物件
			ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon);
			TextView textView = (TextView) convertView.findViewById(R.id.tv_item_name);
			//4. 給檢視設定資料
			imageView.setImageDrawable(appInfo.getIcon());
			textView.setText(appInfo.getAppName());
			
			//返回convertView
			return convertView;
		}
		
	}
	
	
	/*
	 * 得到手機中所有應用資訊的列表
	 * AppInfo
	 *  Drawable icon  圖片物件
	 *  String appName
	 *  String packageName
	 */
	protected List<AppInfo> getAllAppInfos() {

		List<AppInfo> list = new ArrayList<AppInfo>();
		// 得到應用的packgeManager
		PackageManager packageManager = getPackageManager();
		// 建立一個主介面的intent
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		// 得到包含應用資訊的列表
		List<ResolveInfo> ResolveInfos = packageManager.queryIntentActivities(
				intent, 0);
		// 遍歷
		for (ResolveInfo ri : ResolveInfos) {
			// 得到包名
			String packageName = ri.activityInfo.packageName;
			// 得到圖示
			Drawable icon = ri.loadIcon(packageManager);
			// 得到應用名稱
			String appName = ri.loadLabel(packageManager).toString();
			// 封裝應用資訊物件
			AppInfo appInfo = new AppInfo(icon, appName, packageName);
			// 新增到list
			list.add(appInfo);
		}
		return list;
	}


	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		//刪除當前行
			//刪除當前行的資料
		data.remove(position);
			//更新列表
		//lv_main.setAdapter(adapter);//顯示列表, 不會使用快取的item的檢視物件
		adapter.notifyDataSetChanged();//通知更新列表, 使用所有快取的item的檢視物件
		
		return true;
	}
}

2.AppInfo檔案

public class AppInfo {

	private Drawable icon;// 應用圖示
	private String appName;// 應用名稱
	private String packageName;// 包名

	public AppInfo(Drawable icon, String appName, String packageName) {
		super();
		this.icon = icon;
		this.appName = appName;
		this.packageName = packageName;
	}

	public AppInfo() {
		super();
	}

	public Drawable getIcon() {
		return icon;
	}

	public void setIcon(Drawable icon) {
		this.icon = icon;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	@Override
	public String toString() {
		return "AppInfo [icon=" + icon + ", appName=" + appName
				+ ", packageName=" + packageName + "]";
	}

}

3.activity_main檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#c610ef"
        android:gravity="center"
        android:padding="5dp"
        android:text="所有應用"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>


4.item_main檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv_item_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_item_icon"
        android:layout_centerVertical="true"
        android:text="應用名稱"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"/>

</RelativeLayout>