Android編程實現(xiàn)在adapter中進(jìn)行數(shù)據(jù)操作的方法
本文實例講述了Android編程實現(xiàn)在adapter中進(jìn)行數(shù)據(jù)操作的方法。分享給大家供大家參考,具體如下:
package com.cvte.apkclassify;
import java.util.ArrayList;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ItemAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private PackageManager pManager ;
private ArrayList<PackageInfo> apps;
private ArrayList<ApkBean> allApkBeans;
private ApkBean bean = new ApkBean();
private DBUtils utils ;
private ApplicationInfo appinfo;
private static int ll = 1;
public ItemAdapter(Context c) {
super();
this.context = c;
pManager = context.getPackageManager();
apps = (ArrayList<PackageInfo>) GetAllAppInfo.getAllapps(context);
utils = new DBUtils(context);
allApkBeans = (ArrayList<ApkBean>) utils.getAllRecords();
for (int i = 0; i < apps.size(); i++) {
bean = new ApkBean();
bean.setApkname(pManager.getApplicationLabel(apps.get(i).applicationInfo).toString());
bean.setKind(0);
bean.setPackagename(apps.get(i).applicationInfo.packageName);
utils.insert(bean);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return apps.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return apps.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder = null;
inflater = LayoutInflater.from(context);
if (view==null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listitem, null);
holder.apkimage = (ImageView)view.findViewById(R.id.apkimage);
holder.apkname =(TextView)view.findViewById(R.id.apkname);
holder.result = (TextView)view.findViewById(R.id.result);
view.setTag(holder);
}else{
holder=(ViewHolder)view.getTag();
}
if (allApkBeans==null||allApkBeans.size()==0) {
PackageInfo pinfo = apps.get(position);
String name = pManager.getApplicationLabel(pinfo.applicationInfo).toString();
holder.apkimage.setBackgroundDrawable(pManager.getApplicationIcon(pinfo.applicationInfo));
holder.apkname.setText(name);
holder.result.setText("未分類");
}else{
ApkBean bean = allApkBeans.get(position);
holder.apkimage.setBackgroundDrawable( getIconFromPackageName(bean.getPackagename()));
holder.apkname.setText(bean.getApkname());
if (bean.getKind()!=0) {
holder.result.setText("已經(jīng)分類");
}else{
holder.result.setText("未分類");
}
}
return view;
}
class ViewHolder{
public ImageView apkimage;
public TextView apkname;
public TextView result;
}
private Drawable getIconFromPackageName(String name){
Drawable image = null ;
try {
appinfo = pManager.getApplicationInfo(name, PackageManager.GET_META_DATA);
image = pManager.getApplicationIcon(appinfo);
} catch (NameNotFoundException e) {
Log.i("no packagename","no packagename");
}
return image;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android自定義Adapter的ListView的思路及代碼
- android開發(fā)中ListView與Adapter使用要點介紹
- 舉例講解Android應(yīng)用中SimpleAdapter簡單適配器的使用
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android控件系列之相冊Gallery&Adapter適配器入門&控件縮放動畫入門
- Android中的Adapter簡單介紹
- Android中BaseAdapter用法示例
- Android開發(fā)中ListView自定義adapter的封裝
- Android listview與adapter詳解及實例代碼
- Adapter模式實戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行
- Android Adapter的幾個常用方法
相關(guān)文章
Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)
這篇文章主要介紹了Android 中之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android11文件管理權(quán)限申請詳細(xì)介紹
大家好,本篇文章主要講的是Android11文件管理權(quán)限申請詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android使用SQLiteDatabase增刪改查(CRUD)的操作教程
在 Android 中使用 SQLiteDatabase 進(jìn)行增刪改查操作是開發(fā)中的核心技能之一,下面我將詳細(xì)說明如何使用 SQLiteDatabase 進(jìn)行 CRUD 操作,需要的朋友可以參考下2025-04-04
Flutter網(wǎng)絡(luò)請求庫DIO的基本使用
這篇文章主要介紹了Flutter網(wǎng)絡(luò)請求庫DIO的基本使用,幫助大家更好的理解和學(xué)習(xí)使用Flutter,感興趣的朋友可以了解下2021-04-04
一文了解Android?ViewModelScope?如何自動取消協(xié)程
這篇文章主要介紹了一文了解Android?ViewModelScope?如何自動取消協(xié)程,文章圍繞主題站展開詳細(xì)的內(nèi)容介紹,具有一定參考價值,感興趣的小伙伴可以參考一下2022-07-07

