自定义listview来自数据库我想点击listview意味着它打印特定的itemname

时间:2012-04-02 06:44:44

标签: android android-layout

我从database.now获取图像和文本作为listview,点击特定文本或图像我应该打印相同的文本。 例如:如果我点击食物菜单的列表视图,那么它应该打印食物菜单。如果我点击目录的列表视图,它应该打印目录。

enter code here
JSONArray json = jArray.getJSONArray("mainmenu");
        list=(ListView)findViewById(R.id.mainmenulist);
         adapter=new MainMenulist(this, json);
         list.setAdapter(adapter);

Main menu listview
  public class MainMenulist extends BaseAdapter {
protected static Context Context = null;
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) {
    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()];

    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(final 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]);
    text.setOnClickListener(new OnClickListener() {

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

    return vi;

}

  }

我可以获取menulistview.java中的所有项目请告诉我如何获取特定列表视图点击意味着特定项目打印

2 个答案:

答案 0 :(得分:4)

检查以下代码

text.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (itemnames[position].equals("Food Menu")) {
            Intent intent = new Intent(Context, FoodMenu.class);
            System.out.println("prakash");
            System.out.println(v.getText().toString());
            Context.startActivity(intent);
        }
    }
});

认为您想按照自己所在地的建议打印姓名 System.out.println声明

答案 1 :(得分:0)

使用以下链接显示示例应用以及如何使用自定义适配器。包括点击事件

http://www.codeproject.com/Articles/183608/Android-Lists-ListActivity-and-ListView-II-Custom

尝试以下::

text.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Context, FoodMenu.class);
            System.out.println(itemnames[position]);
            System.out.println(text.getText().toString());
            Context.startActivity(intent);
            }
        }
    });