2015-12-20 12 views
0

我登錄以下字段作爲我的輸出:顯示模式只在人口稠密的ListView

I/System.out: getLongitude 4.745929 
I/System.out: getStreet Laat 199 
I/System.out: getLatitude 52.630753 
I/System.out: getCity Alkmaar 
I/System.out: getZipCode 1811 EG 
I/System.out: getName SUBWAY® Alkmaar 
I/System.out: getFacebookID 180894875290925 

但是我要輸出到我這裏ListView下面你可以看到它目前看起來像在我的屏幕截圖ListView

鏈接截圖:http://i.imgur.com/YfWMjkG.png

這裏下面你可以看到我曾嘗試:

AllStores.java:

  Iterator it = subprises.body().iterator(); 
      List<Store> subprisesList = new ArrayList<>(); 
      int i = 0; 
      while(it.hasNext()) { 
       i++; 
       Store store = (Store) it.next(); 
       System.out.println("getLongitude "+store.getLongitude()); 
       System.out.println("getStreet "+store.getStreet()); 
       System.out.println("getLatitude "+store.getLatitude()); 
       System.out.println("getCity "+store.getCity()); 
       System.out.println("getZipCode "+store.getZipCode()); 
       System.out.println("getName "+store.getName()); 
       System.out.println("getFacebookID "+store.getFacebookID()); 
       System.out.println("next store"); 
       subprisesList.add(store); 
      } 
      StoreArrayAdapter stringArrayAdapter = new StoreArrayAdapter(
        getApplicationContext(), 
        R.layout.listview_item_row, 
        subprisesList); 
      lv.setAdapter(stringArrayAdapter); 

編輯:我已經改變了我StoreArrayAdapter類

StoreArrayAdapter class which lives inside AllStores.java: 
    private class StoreArrayAdapter extends ArrayAdapter<Store> { 
     public StoreArrayAdapter(Context context, int textViewResourceId, List<Store> stores) { 
      super(context, textViewResourceId, stores); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View storeView = convertView; 
      if (convertView == null) { 
       // inflate your list view here 
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       storeView = inflater.inflate(R.layout.listview_item_row, parent, false); 
      } 
      Store store = getItem(position); 
      // use findViewById() to get the TextViews 
      TextView name = (TextView)storeView.findViewById(R.id.txtName); 
      // call setText using the values from store 
      name.setText(store.getName()); 

      return convertView; 
     } 
    } 

而且這個時候,我想告訴你,我的看法和我的模型稱爲Store

activity_all_stores.xml:

鏈接代碼:http://pastebin.com/6eiUyPKU

listview_item_row.xml:

鏈接代碼:http://pastebin.com/CGSVf2Uq

Store.java:

鏈接代碼:​​3210

回答

1

您在圖片中看到文字的原因是因爲它來自默認的Store.toString()方法。你可以開始看到的東西通過重寫toString()方法更有趣的你Store類:

@Override 
public String toString() { 
    return getName(); // or define how you want 
} 

但最終你可能需要定義一個擁有獨立TextView S代表每個字段列表視圖佈局。那麼您需要在您的StoreArrayAdapter子類中覆蓋getView()

@Override 
    public View getView(int position, View view, ViewGroup parent) { 

     if (view == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.listview_item_row, parent, false); 
     } 
     Store store = getItem(position); 
     // use findViewById() to get the TextViews 
     TextView name = (TextView) view.findViewById(R.id.txtName); 
     // call setText using the values from store 
     name.setText(store.getName()); 

     return view; 
    } 
+0

我編輯了我的代碼。但我仍然看到相同的結果。另外我還添加了三個名爲'activity_all_stores.xml','listview_item_row.xml'和'Store.java'的文件,以防你想看到它們。你可以再看看我的代碼,請:) :)? – superkytoz

+0

我唯一可能看到的錯誤是你將佈局擴充到'storeView'中,但是返回'convertView',它將爲null。這應該會給你一個'NullPointerException',與以前不一樣。但無論如何,嘗試我更新的代碼。我將'convertView'重命名爲'view',因爲「convertView」令人困惑。它真的意味着「如果沒有任何回收利用的情況下,可以回收視圖或者爲null」。一定還有其他的東西不對,但我還看不到它。 –

+0

你的代碼確實有效,它是我的棄牌。我有無意識地使用了另一個'ArrayAdapter'變量。當我重構我的代碼時,我發現了這個錯誤。無論如何感謝您的解決方案:)! – superkytoz

相關問題