如何在不滚动Android的情况下获取自定义列表视图中的所有项目名称

时间:2012-04-21 10:42:45

标签: android android-listview

在我的应用程序中,我将MySQL服务器数据库中的所有图像和名称提取到Android手机。如果模拟器屏幕为HVGA,则仅显示五个图像和名称。

LogCat中,只有五个名字被打印出来。当我滚动屏幕时,我可以在屏幕上看到多少名字只有很多名字正在打印?而不是那样,所有名称都必须在滚动之前打印。

这是我的代码:

public class VegdishesListview extends BaseAdapter {
    String qrimage;
    Bitmap bmp, resizedbitmap;
    Bitmap[] bmps;
    Activity activity = null;
    private LayoutInflater inflater;

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

    public VegdishesListview(Context context, JSONArray imageArrayJson) {
        //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.prices=new String[imageArrayJson.length()];
        this.descs=new String[imageArrayJson.length()];
        try {
            for (int i = 0; i < imageArrayJson.length(); i++) {
                JSONObject image = imageArrayJson.getJSONObject(i);
                qrimage = image.getString("itemimage");
                itemname = image.getString("itemname");
                price=image.getString("price");
                desc=image.getString("itemdesc");

                itemnames[i] = itemname;
                prices[i]=price;
                descs[i]=desc;

                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(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.vegdisheslistview, null);

        TextView text=(TextView)vi.findViewById(R.id.vegdishestext);
        ImageView image=(ImageView)vi.findViewById(R.id.vegdishesimage);
        TextView text1=(TextView)vi.findViewById(R.id.vegdishesprice);
        TextView text2=(TextView)vi.findViewById(R.id.vegdishesdesc);
        image.setImageBitmap(bmps[position]);
        text.setText(itemnames[position]);
        text1.setText(prices[position]);
        text2.setText(descs[position]);
        System.out.println(itemnames[position]);

        return vi;
    }
}

在这里,我想要打印所有项目名,System.out.println(itemnames[position]);而不是滚动。

1 个答案:

答案 0 :(得分:0)

仅为显示的项目调用方法getView,因此您应该像这样放置代码

for(String itemname: itemnames)
  System.out.println(itemname);

在构造函数的末尾(在System.out.println(map);之后) 这样做而不是从getView

打印