2012-03-29 80 views
0

另一類這個是我的基本適配器類 有所有我想要的PR如何獲得字符串數組對象值在安卓

enter code here 
public class MainMenulist extends BaseAdapter { 
int i; 
    String qrimage; 
    Bitmap bmp, resizedbitmap; 
    Bitmap[] bmps; 
    Activity activity = null; 
    private LayoutInflater inflater; 

    private ImageView[] mImages; 
    String[] itemimage; 
    TextView[] tv; 
    String itemname; 
    public String[] itemnames; 
    HashMap<String, String> map = new HashMap<String, String>(); 

    public MainMenulist(Context context, JSONArray imageArrayJson) { 

    inflater=LayoutInflater.from(context); 
    this.mImages = new ImageView[imageArrayJson.length()]; 
    this.bmps = new Bitmap[imageArrayJson.length()]; 
    this.itemnames = new String[imageArrayJson.length()]; 

    try { 

     for (i = 0; i < imageArrayJson.length(); i++) { 
     JSONObject image = imageArrayJson.getJSONObject(i); 
     qrimage = image.getString("menuimage"); 
     itemname = image.getString("menuname"); 
     itemnames[i] = itemname; 


     byte[] qrimageBytes = Base64.decode(qrimage.getBytes()); 

     bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0, 
              qrimageBytes.length); 
     int width = 100; 
     int height = 100; 
     resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height, 
                true); 
     bmps[i] = bmp; 

     mImages[i] = new ImageView(context); 
     mImages[i].setImageBitmap(resizedbitmap); 

     mImages[i].setScaleType(ImageView.ScaleType.FIT_START); 


     // tv[i].setText(itemname); 
     } 
     System.out.println(itemnames[i]); 
     System.out.println(map); 

    } catch (Exception e) { 
     // TODO: handle exception 
    } 
    } 

    public int getCount() { 
    return mImages.length; 
    } 

    public Object getItem(int position) { 
    return position; 
    } 

    public long getItemId(int position) { 
    return position; 
    } 



    public View getView(int position, View convertView, ViewGroup parent) { 


    View vi=convertView; 

    vi = inflater.inflate(R.layout.mainmenulistview, null); 


    TextView text=(TextView)vi.findViewById(R.id.menutext); 
    ImageView image=(ImageView)vi.findViewById(R.id.menuimage);  
    image.setImageBitmap(bmps[position]); 

    text.setText(itemnames[position]); 



    return vi; 


    } 


} 

的fooditemnames這是另一個類ManagerHandset.java該類itemNames的我要打印itemNames的[]在上述class.i想在ManagerHandset.java.please itemNames的幫我

回答

0

編寫類MyMain喜歡 -

class MyMain extends Application(){....}

當應用程序啓動時,該類將被調用。現在,你在那類喜歡 -

class MyMain extends Application(){ 
public static Object itemNames[]; 
} 

maitain您itemnames[]的靜態實例現在,使用MyMain.itemNames = this.itemNames;無論你有itemNames值實例itemNames。現在,您的整個應用程序都可以使用您的itemNames。快樂編碼!

+0

上面的代碼執行一個listview。我想點擊第一項意味着它去到另一個頁面。第二個項目意味着它進入另一個頁面..我怎麼能給我請告訴我 – Vinoth 2012-03-29 15:41:12

+0

然後,你應該已經提供'onItemClick()'人。無論如何,我的答案中的代碼適用於應用程序中的任何屏幕,因爲它的應用程序範圍。所以你可以在你的應用中的任何地方使用它。確保你的類MyMain具有公共可見性,並且你的Object [] itemNames具有公共和靜態修飾符。 – Rajkiran 2012-03-29 15:44:43

相關問題