2012-02-21 100 views
0

我正在廣泛地學習有關獲取者和安裝者的知識,但我似乎沒有按照自己的方式。 我有一個名爲Apps_Info的類,其中包含我的setters和getters,我有我的主要活動FavouriteApps,它有一個使用Apps_Info類的列表。
我想從FavouriteApps列表中獲取包的名稱,但我仍然收到null。
請有人可以告訴該怎麼辦?下面是這個命令的代碼:類Apps_Info和FavouriteApps活動安裝者和獲得者的信息

public class Apps_Info { 
    private Bitmap bIcon; 
    private String sName; 
    private String sPacks_Name; 

    public Apps_Info(Bitmap icon, String name, String Packs_Name) { 
     bIcon = icon; 
     sName = name; 
     sPacks_Name = Packs_Name; 
    } 

    public void setIcon(Bitmap icon) { 
     bIcon=icon; 
    } 

    public Bitmap getIcon() { 
     return bIcon; 
    } 

    public void setName(String name) { 
     sName=name; 
    } 

    public String getName() { 
     return sName; 
    } 

    public void setPacks_Name(String Packs_Name) { 
     this.sPacks_Name=Packs_Name; 
    } 

    public String getPacks_Name() { 
     return sPacks_Name; 
    } 
} 

FavouriteApps活動代碼(部分)

String packname, packsname, apps_names; 
Bitmap app_icon; 
Resources res = getResources(); 
List<Apps_Info> ListApps_Info = new ArrayList<Apps_Info>(); 
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.browser_app), "Browser", "com.browser")); 
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.clock_app), "Alarm Clock", "com.alarm.clock")); 
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.threegplus), "3G Secure Connection", "threeg.secureconnect")); 

mGridView.setAdapter(new Apps_Info_Adapter(this, ListApps_Info)); 
Apps_Info packinfo = new Apps_Info(app_icon, apps_names, packname); 
packsname = packinfo.getPacks_Name(); 
apps_names = packinfo.getName(); 
Log.i("The Pack_Name is " + packsname, "Pack Name"); 
+0

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html – Jave 2012-02-21 10:46:31

回答

1

你應該這樣做。

for (int i = 0; i < ListApps_Info.size(); i++) { 
    Apps_Info packinfo = ListApps_Info.item(i); 
    packsname = packinfo.getPacks_Name(); 
    apps_names = packinfo.getName(); 
    Log.i("The Pack_Name is " + packsname, "Pack Name"); 
} 
+0

Sergey謝謝你回答我的問題的答案,但一個小問題是,從ListApps_Info下拉列表中沒有item(),所以我使用get()但我沒有得到我需要的全部結果。任何建議? – 2012-02-21 11:14:29

1

這是因爲你在初始化代碼中packname變量。

String packname,packsname,apps_names; 

當你這樣做:

Apps_Info packinfo=new Apps_Info(app_icon, apps_names,packname); 
packsname=packinfo.getPacks_Name(); 

你吸氣時做得很好,這是正常的,如果返回null。

+0

但我想初始化它已經在列表中 ListApps_Info.add(新Apps_Info(BitmapFactory.decodeResource(res,R.drawable.browser_app),「Browser」,「com.browser」)); – 2012-02-21 10:54:11

+0

不知道這個編譯。至少IDEA會大聲抱怨使用非單調變量 – 2012-02-21 10:56:26

+0

因此,您可能會嘗試執行此操作: for(Apps_Info current:ListApps_Info){ packsname = current.getPacks_Name(); apps_names = current.getName(); (「Pack_Name是」+ packsname,「Pack Name」); Log.i } – Joand 2012-02-21 10:56:35