2013-04-25 119 views
0

我的列表視圖隨機排序的項目,當我滾動向下或向上的位置隨機變化的項目,我嘗試過很多辦法來解決這個問題,但沒有成功ListView隨機顯示訂購商品?

我GOOGLE了,我發現太多的方法可以解決這個問題有關擴展列表視圖,但問題它不是我的代碼

請一些幫助

這是列表視圖代碼

static class ViewHolder { 
     ImageView play; 
     ImageView download; 
     TextView rtitle; 
     TextView size; 
     TextView downloads; 
     RatingBar ratingsmall; 
     ImageView ratebutton; 
     long tonid; 
     TextView voters; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     //Get the current location object 
     JSONObject r = (JSONObject) getItem(position); 
     //Inflate the view 
     if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.ringtone_bit, null); 
     holder = new ViewHolder(); 
     ImageView play = (ImageView) convertView.findViewById(R.id.play); 
     ImageView download = (ImageView) convertView.findViewById(R.id.download); 
     ImageView ratebutton = (ImageView) convertView.findViewById(R.id.ratebutton); 
     TextView rtitle = (TextView) convertView.findViewById(R.id.rtitle); 
     TextView size = (TextView) convertView.findViewById(R.id.size); 
     TextView downloads = (TextView) convertView.findViewById(R.id.downloads); 
     TextView voters = (TextView) convertView.findViewById(R.id.voters); 
     TextView personname = (TextView) convertView.findViewById(R.id.personname); 
     TextView date = (TextView) convertView.findViewById(R.id.date); 
     RatingBar ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall); 
     //setdate 
     try { 
      Date date_g = new Date(r.getLong("timestamp") * 1000); 
      date.setText(date_g.toLocaleString()); 
     } catch (JSONException e2) {} 
     //set person name 
     try { 
      String client_name = (r.getString("personname").equals("null") == true) ? "ghost" : r.getString("personname"); 
      personname.setText(client_name); 
     } catch (JSONException e2) {} 
     //set total votars and vote avarage 
     try { 
      float z = (float) r.getInt("rate"); 
      voters.setText(" (" + r.getLong("voters") + ")/" + z); 
     } catch (JSONException e2) {} 
     //set rating bar 
     try { 
      float z = (float) r.getInt("rate"); 
      ratingsmall.setRating(z); 
     } catch (JSONException e2) {} 
     //set ringtone Name as defualt device language 
     try { 
      String name = (lang.equals("English") == true) ? r.getString("en_name") : r.getString("ar_name"); 
      rtitle.setText(name); 
     } catch (JSONException e2) {} 
     //ringtone file size 
     try { 
      size.setText(r.getString("size")); 
     } catch (JSONException e2) {} 
     //set downloads 
     try { 
      downloads.setText(String.valueOf(r.getLong("downloads"))); 
     } catch (JSONException e2) {} 
     //set ringtone ID toneid 
     try { 
      holder.tonid = r.getLong("toneid"); 
      download.setTag(r.getLong("toneid")); 
      ratebutton.setTag(r.getLong("toneid")); 
     } catch (JSONException e1) {} 
     //set download stram url to play icon 
     try { 
      play.setTag(r.getString("stream_url")); 
     } catch (JSONException e) {} 
     //add play listener test Ringtone before download it 
     play.setOnClickListener(onClickListener); 
     convertView.setTag(holder); 
     } else { 
     holder = (ViewHolder) convertView.getTag(); 
     } 
     return convertView; 
    } 
+0

你不能用'convertView = null'處理案件正確!你也必須填寫所有的值。兩種情況之間的唯一區別應該是佈局通貨膨脹。 – 2013-04-25 16:44:33

回答

3

我想你是誤會是什麼ViewHolder的。 ViewHolder不是保存值,而是保存Views,所以你不必再次膨脹它們。即使您從標籤中獲得視圖,您仍然需要設置數據。

該如何正確使用視圖持有人:

if(convertView == null) 
{ 
    convertView = mInflater.inflate(R.layout.ringtone_bit, null); 
    holder = new ViewHolder(); 
    convertView.setTag(holder); 
    holder.play  = (ImageView) convertView.findViewById(R.id.play); 
    holder.download = (ImageView) convertView.findViewById(R.id.download); 
    holder.ratebutton = (ImageView) convertView.findViewById(R.id.ratebutton); 
    holder.rtitle  = (TextView) convertView.findViewById(R.id.rtitle); 
    holder.size  = (TextView) convertView.findViewById(R.id.size); 
    holder.downloads = (TextView) convertView.findViewById(R.id.downloads); 
    holder.voters  = (TextView) convertView.findViewById(R.id.voters); 
    holder.personname = (TextView) convertView.findViewById(R.id.personname); 
    holder.date   = (TextView) convertView.findViewById(R.id.date); 
    holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall); 
} else { 
    holder = (ViewHolder) convertView.getTag(); 
} 

// Fill the data 
+0

多可愛的意見+1 – 2013-04-25 16:56:39

1

您還沒有填充如果數據工作。你應該閱讀另一個使用viewHolder的例子。這次仔細檢查一下。

+0

你能否給我提供任何有用的演示鏈接,我不知道在哪裏我必須開始 – 2013-04-25 16:46:11

+1

你說得對,但你的回答更多的是評論而不是真實的答案。 – m0skit0 2013-04-25 16:55:13

+1

@ m0skit0:我不認爲答案需要包含確切的解決方案。我優先指出人們正確的方向,所以他們可以自己弄清楚。這將在一天結束時幫助他們。 – SimonSays 2013-04-25 17:03:04

0
//Get the current location object 
JSONObject r = (JSONObject) getItem(position); 

//Inflate the view 
if(convertView == null) 
{ 
    convertView = mInflater.inflate(R.layout.ringtone_bit, null); 
} 

     ImageView play  = (ImageView) convertView.findViewById(R.id.play); 
    ImageView download = (ImageView) convertView.findViewById(R.id.download); 
    ImageView ratebutton = (ImageView) convertView.findViewById(R.id.ratebutton); 
    TextView rtitle  = (TextView) convertView.findViewById(R.id.rtitle); 
    TextView size  = (TextView) convertView.findViewById(R.id.size); 
    TextView downloads = (TextView) convertView.findViewById(R.id.downloads); 
    TextView voters  = (TextView) convertView.findViewById(R.id.voters); 
    TextView personname = (TextView) convertView.findViewById(R.id.personname); 
    TextView date   = (TextView) convertView.findViewById(R.id.date); 
    RatingBar ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall); 


    //setdate 
    try { 
     Date date_g = new Date(r.getLong("timestamp")*1000); 
     date.setText(date_g.toLocaleString()) ; 

    } catch (JSONException e2) {} 


    //set person name 
    try { 
     String client_name = (r.getString("personname").equals("null") == true) ? "ghost" : r.getString("personname"); 
     personname.setText(client_name); 
    } catch (JSONException e2) {} 

    //set total votars and vote avarage 
    try { 
     float z = (float) r.getInt("rate"); 
     voters.setText(" ("+ r.getLong("voters") +")/" + z); 
    } catch (JSONException e2) {}    
    //set rating bar 
    try { 
     float z = (float) r.getInt("rate"); 
     ratingsmall.setRating(z); 
    } catch (JSONException e2) {}   
    //set ringtone Name as defualt device language 
    try { 
     String name = (lang.equals("English") == true) ? r.getString("en_name") : r.getString("ar_name"); 
     rtitle.setText(name); 
    } catch (JSONException e2) {} 

    //ringtone file size 
    try { 
     size.setText(r.getString("size")); 
    } catch (JSONException e2) {} 

    //set downloads 
    try { 
     downloads.setText(String.valueOf(r.getLong("downloads"))); 
    } catch (JSONException e2) {} 

    //set ringtone ID toneid 
    try { 
      holder.tonid = r.getLong("toneid"); 
      download.setTag(r.getLong("toneid")); 
      ratebutton.setTag(r.getLong("toneid")); 
     } catch (JSONException e1) {} 

    //set download stram url to play icon 
    try { 
     play.setTag(r.getString("stream_url")); 
    } catch (JSONException e) {} 

    //add play listener test Ringtone before download it 
    play.setOnClickListener(onClickListener); 


return convertView; 

觀點持有人是奇怪的,通常會引起混淆,這是我會建議實施getView()

+0

謝謝兄弟,上帝保佑你它爲我工作瞄準抱歉瞄準更新的Android開發,再次感謝 – 2013-04-25 16:51:16

+1

沒問題,適配器是混亂! – Eluvatar 2013-04-25 16:51:51

+0

這仍然是錯誤的。您仍在使用findViewById而不是使用ViewHolder。 – m0skit0 2013-04-25 16:52:00