2012-04-21 31 views
-1

在我的應用程序中,我將所有圖像和名稱從MySQL服務器數據庫提取到Android移動設備。如果仿真器屏幕是HVGA,則只顯示五個圖像和名稱。在LogCat也只有五個名字正在打印。當我滾動屏幕時,屏幕上可以看到多少個名字,只有很多名字正在打印。相反,所有的名字必須在滾動前打印。如何在滾動Android中的自定義列表視圖之前打印所有名稱

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; 
    } 

這是我的自定義列表視圖XML佈局。如何根據需要更改我的代碼?

+0

發佈您正在打印的java代碼,然後 – kishu27 2012-04-21 07:40:20

回答

0

listview是爲顯示大數據列表而創建的,但其效率與移動設備資源相關。

它重新使用視圖使其高效。

您提供給列表的適配器必須具有getView方法。 獨立地爲listview的每個位置提供視圖。

它只會被調用,只有屏幕可見的視圖。 我相信你在getView中記下名字。

當您滾動getView方法時,再次調用所有要在屏幕上繪製的視圖。

如果您希望一次打印所有數據,請在設置適配器之前執行此操作。 但這並不明智。事實上只加載那些需要顯示的內容。

+0

ya ..正確 – Vinoth 2012-04-21 08:29:39

+0

我可以更改我的代碼。請參閱我的代碼告訴我解決方案 – Vinoth 2012-04-21 09:11:00

+0

@Javanator您的回答與我的有何不同? – 2012-04-21 10:59:07

0

這是默認的ListView操作,它在Android ListView中回收視圖。換句話說,只需要考慮,項目數可以在屏幕上放置的時間,所以你的情況,你都能夠看到,只是因爲這個屏幕上的那些五行......

對於更多,請參閱Android文檔中的AdapterView

相關問題