2013-04-25 111 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