0

MainMenulist.java 在此类字符串数组中存储所有值 public String[] itemcodes; 我想访问 Main.java 的项目代码

主.java

  JSONArray json = jArray.getJSONArray("mainmenu");
        list=(ListView)findViewById(R.id.mainmenulist);
         adapter=new MainMenulist(this, json);
         list.setAdapter(adapter);
           MainMenulist.java
      public class MainMenulist extends BaseAdapter {
protected static Context Context = null;
int i;
public String editnewmainmenu,menuname;
String qrimage;
Bitmap bmp, resizedbitmap;
Bitmap[] bmps;
Activity activity = null;
private LayoutInflater inflater;

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

public MainMenulist(Context context, JSONArray imageArrayJson) {
    Context = context;
    // inflater =
    // (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // imageLoader=new ImageLoader(activity);
    inflater = LayoutInflater.from(context);
    this.mImages = new ImageView[imageArrayJson.length()];
    this.bmps = new Bitmap[imageArrayJson.length()];
    this.itemnames = new String[imageArrayJson.length()];
    this.itemcodes=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");
            itemcode=image.getString("menucode");
            itemnames[i] = itemname;
            itemcodes[i]=itemcode;

            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(final int position, View convertView, ViewGroup parent) {

    View vi = convertView;

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

final   TextView text = (TextView) vi.findViewById(R.id.menutext);
    ImageView image = (ImageView) vi.findViewById(R.id.menuimage);

    System.out.println(itemcodes[position]);
    image.setImageBitmap(bmps[position]);

    text.setText(itemnames[position]);
    text.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(itemcodes[position].equals("1"))
            {
                Intent intent = new Intent(Context, FoodMenu.class);
                System.out.println("prakash");

                Context.startActivity(intent);
            }
            else {
                Toast.makeText(Context, "This Feauture is not yet Implemented",4000).show();
            }

        }
    });       

    return vi;

}

    }

MainMenulist.java System.out.println(itemcodes[position]); 在这里我打印所有代码。没有我想在 Main.java 中打印相同的结果

4

2 回答 2

0

编写一个实现可序列化的bean,为您的数组(项目名称)编写setter和getter方法,如下所示

class Bean implements Serializable{
    String itemnames[];

    public Hashtable getItemnames() {
        return itemnames;
    }

    public void setItemnames(String itemnames[]) {
        this.itemnames= itemnames;
    }

}

并在调用活动中编写愚蠢的代码

 Bean b = new Bean();
    b.setItemnames(itemnames);

    Intent i=new Intent();
    i.setClass(A.this,B.class);
    i.putExtra("itemnames", b);
    startActivity(i);

并在调用的活动中检索如下

Bean obj = (Bean) getIntent().getSerializableExtra("itemnames");// TypeCasting 
String itemname[] = (Hashtable) obj.getItemnames();
于 2012-04-04T11:08:38.017 回答
0
There are two ways to do this:

In your code:

    public String[] itemnames,itemcodes; make that arrays as static like below

    public static String[] itemnames,itemcodes; 

And then use `Main.java` file by calling:

    System.out.println(MainMenulist.itemcodes[position]);
    System.out.println(MainMenulist.itemnames[position]);

2) Parse JSON in Main.java which you have pass to MainMenulist.java

    public MainMenulist(Context context, JSONArray imageArrayJson) 
于 2012-04-04T10:00:27.497 回答