2014-01-23 50 views
0

我試圖添加兩個textViews列出item.that數據從列表中獲取。當列表打印時,它顯示正確。但在列表中查看它不正確打印。誰能幫我?如何將兩個textView添加到自定義適配器?

這是自定義適配器類。

@Override 
    public View getView(int arg0, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     final String text1 = listData.get(0); 
     final String text2 = listData.get(1); 


     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.ratings_list, null); 
     } 

     TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.textView1); 
     lblListHeader1.setText(text1); 

     TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.textView2); 
     lblListHeader2.setText(text2); 

     return convertView; 
    } 

這是活動代碼。

public void ListDrwaer() { 

     try { 
      JSONObject jsonResponse = new JSONObject(strJson1); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("ratings"); 

      for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       String restName = jsonChildNode.optString("rest_name"); 
       listData = new ArrayList<String>(); 
       if (restName.equalsIgnoreCase(name)) { 
        String userName = jsonChildNode.optString("user_name"); 
        String rate = jsonChildNode.optString("rate"); 
        String ratOut = "Rate : " + rate; 

        listData.add(userName); 
        listData.add(ratOut); 
        Log.d("Data", userName + rate); 

       } 
      } 

     } catch (JSONException e) { 
      Toast.makeText(getApplicationContext(), "Error..." + e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

     RatingsAdapter adapter = new RatingsAdapter(getApplicationContext(), 
       listData); 
     listView.setAdapter(adapter); 
    } 

我想添加用戶名,低於它的速率。

這應該是輸出列表。

01-23 11:59:09.102: D/Data(4873): omali 3.5 
01-23 11:59:09.102: D/Data(4873): sunil 2 
01-23 11:59:09.102: D/Data(4873): [email protected] 1.5 
01-23 11:59:09.102: D/Data(4873): [email protected] 0.5 
+0

您提供什麼樣的細節還不是很清楚,你可以請張貼你的xml文件。 – NarendraSoni

+0

@anuruddhika試試我的答案 –

+0

看看我的回答,我認爲所有其他人都忘記了您在每次迭代中創建新列表的事實。此外,你避免了一個雙重大小的清單,所有其他方法將會更簡單和更安全 –

回答

2

首先,要構建列表的方式,它永遠不會工作,因爲您正在刪除它並在每次迭代中創建一個新的,所以當您創建適配器時,在列表中您只有最後一個項目。

我會做:

ArrayList<Pair<String,String>> listData = new ArrayList<Pair<String,String>>(); //added 


public void ListDrwaer() { 

     try { 
      JSONObject jsonResponse = new JSONObject(strJson1); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("ratings"); 

      for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       String restName = jsonChildNode.optString("rest_name"); 
       //removed listData = new ArrayList<String>(); 
       if (restName.equalsIgnoreCase(name)) { 
        String userName = jsonChildNode.optString("user_name"); 
        String rate = jsonChildNode.optString("rate"); 
        String ratOut = "Rate : " + rate; 


        listData.add(new Pair<String,String>(userName,ratOut));//added 
        //removed listData.add(userName); 
        //removed listData.add(ratOut); 
        Log.d("Data", userName + rate); 

       } 
      } 

     } catch (JSONException e) { 
      Toast.makeText(getApplicationContext(), "Error..." + e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

     RatingsAdapter adapter = new RatingsAdapter(getApplicationContext(), 
       listData); 
     listView.setAdapter(adapter); 
    } 

然後,在自定義適配器類,您檢索的數據簡單地通過

@Override 
    public View getView(int arg0, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

      //only this 3 lines change 
      Pair<String,String> item= listData.get(arg0); 
     final String text1 = item.first; 
     final String text2 = item.second; 


     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.ratings_list, null); 
     } 

     TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.textView1); 
     lblListHeader1.setText(text1); 

     TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.textView2); 
     lblListHeader2.setText(text2); 

     return convertView; 
    } 
+0

謝謝dear.it的工作,我明白了。 – anuruddhika

0

你總是打印了錯誤的數據,而不管項目的位置

final String text1 = listData.get(0); 
final String text2 = listData.get(1); 

所以最好採取用戶名ratOut的不同列表和顯示數據

final String text1 = userNameListData.get(arg0); 
final String text2 = ratOutListData.get(arg0); 

這裏爲arg0是位置

0

變化:

final String text1 = listData.get(arg0*2); 
    final String text2 = listData.get(arg0*2+1); 

,並覆蓋:

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return listData.size()/2; 
    } 

當您添加兩個在一個arraylist 0,1文本屬於第一項和2, 3屬於第二等。

此外,列表視圖項目的大小將是arraylist大小的一半。

試試這個。

也改變這一點:

listData = new ArrayList<String>();//<--out side for loop 

for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       String restName = jsonChildNode.optString("rest_name"); 
       if (restName.equalsIgnoreCase(name)) { 
        String userName = jsonChildNode.optString("user_name"); 
        String rate = jsonChildNode.optString("rate"); 
        String ratOut = "Rate : " + rate; 

        listData.add(userName); 
        listData.add(ratOut); 
        Log.d("Data", userName + rate); 

       } 
      } 
+0

是的,我嘗試過。但它只顯示列表ne的最後一個用戶數據。我想顯示列表中的所有用戶數據 – anuruddhika

+0

檢查我的編輯,把listData = new ArrayList ()out side for循環。對於每次迭代,您將創建一個新的數組列表並僅向其添加一個項目。 –

0

替換此:

final String text1 = listData.get(0); 
final String text2 = listData.get(1); 

有:

final String text1 = listData.get(2*arg0); 
final String text2 = listData.get((2*arg0)+1); 
相關問題