0
我正在嘗試使用除系統應用程序之外的所有已安裝的應用程序填充微調控件。我遇到兩個問題:在Android微調控制器中重複的數據
1)我得到重複數據
2)當我的微調填充和我開的微調項列表,有有時會差距之間應用程序名稱。
見下面我的適配器代碼:
public class PackageAdapter extends BaseAdapter implements SpinnerAdapter {
private List<ApplicationInfo> applications;
private Context context;
private PackageManager packageManager = null;
public PackageAdapter(Context context) {
this.context = context;
// setting up the List applications with all the list of installed apps
applications = new ArrayList<ApplicationInfo>();
try {
packageManager = context.getPackageManager();
if (packageManager != null) {
applications = packageManager.getInstalledApplications(0);
}
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
@Override
public int getCount() {
return applications.size();
}
@Override
public Object getItem(int position) {
return applications.get(position);
}
@Override
public long getItemId(int position) {
return applications.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View spinView;
if (convertView == null) {
// setting up the view
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
spinView = inflater.inflate(R.layout.spin_layout, null);
} else {
spinView = convertView;
}
// getting one application from the list of apps.
final ApplicationInfo application = this.applications.get(position);
// filtering out the system apps and getting the application name
String appName = "";
if (packageManager.getLaunchIntentForPackage(application.packageName) != null) {
appName = packageManager.getApplicationLabel(application).toString();
}
TextView t1 = (TextView) spinView.findViewById(R.id.field1);
if (!appName.equals("")) {
t1.setText(appName);
}
return spinView;
}
}
任何幫助在任上述兩個的將不勝感激! TY
心不是這段代碼爲你工作?@大衛賈維斯 –
嗨@Amit Vaghela泰爲您的文章和這方面的幫助!所以我剛剛用你的代碼替換了我的適配器代碼,我的微調現在顯示所有軟件包名稱而不是應用程序名稱,我的微調控制器也被填充了系統應用程序。我的微調項目必須顯示爲應用程序名稱,因爲用戶可能不會知道應用程序的軟件包名稱,我們也不能允許用戶從此列表中選擇系統應用程序。任何想法,我們可以如何修改代碼以適應此? –
檢查我的更新答案。你的問題解決了@ DavidJarvis –