嗨,我正在做示例項目,因爲我有像listitem這樣的問題顯示一些垃圾值。不知道如何解決。誰能幫我嗎。Listview顯示垃圾值
這是我的活動:
public class MainActivity extends Activity {
ListView app_List;
private ArrayList<AppInfo> infoList;
private ArrayAdapter<AppInfo> adpt;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
app_List = (ListView)findViewById(R.id.listView1);
infoList = getListOfUserInstalledApps();
}
private ArrayList<AppInfo> getListOfUserInstalledApps() {
// TODO Auto-generated method stub
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
System.out.println("No. of applications installed on the device: "+apps.size());
ArrayList<AppInfo> infoList1 = new ArrayList<AppInfo>();
for(int i=0;i<apps.size();i++) {
PackageInfo p = apps.get(i);
/*if ((!getSysPackages) && (p.versionName == null)) {
continue ; }*/
AppInfo info = new AppInfo();
//info.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
info.pname = p.packageName;
System.out.println("application name: "+info.pname);
//info.pname = p.packageName;
infoList1.add(info);
}
return infoList1;
}
static class AppInfo {
//private String appname = "";
private String pname = "";
//private String versionName = "";
//private int versionCode = 0;
//private Drawable icon;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
adpt = new ArrayAdapter<AppInfo>(this,android.R.layout.simple_list_item_1, infoList);
app_List.setAdapter(adpt);
}
}
指屏幕截圖
http://i.stack.imgur.com/w4Axi.png
*你有兩種方法可以解決您的問題* - 或第三更簡單的方法,重寫'的toString()'方法爲'AppInfo'類返回目標值(在* PNAME的*我假設)。 – Luksprog
感謝@Luksprog你是對的:) – Blackbelt
當心! @toString()並不意味着爲用戶界面提供文本。它應該提供有關對象的可讀信息。 toString用於記錄和調試 – Mirco