简体   繁体   English

ListView随机订购商品?

[英]ListView ordered items randomly?

my list view is ordered items randomly , when i scroll down or up the items positions changed randomly I've tried many ways to fix this but no success 我的列表视图是随机排序的项目,当我向下滚动或向上滚动项目时,位置随机更改时,我尝试了多种方法来解决此问题,但未成功

I've googled and i found too many ways to fix this issue related to Expanded listview but its not working with my code 我已经用Google搜索过,发现了太多方法可以解决与扩展列表视图有关的问题,但不适用于我的代码

please some help 请一些帮助

this is the listview code 这是列表视图代码

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

I think you misunderstood what ViewHolder is for. 我认为您误解了ViewHolder的用途。 ViewHolder is not to hold values but to hold the Views so you don't have to inflate them again. ViewHolder不是要保存值,而是要保存Views,这样您就不必再次膨胀它们。 You still need to set the data even if you get the View from the tag. 即使您从标记中获得“视图”,仍然需要设置数据。

This how you correctly use a View holder: 这是您正确使用视图持有人的方式:

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

You are not populating the data if convertView != null . 如果convertView != null则不会填充数据。 You should read another example that uses a viewHolder. 您应该阅读另一个使用viewHolder的示例。 This time check it more carefully. 这次请仔细检查。

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

view holders are weird and usually cause confusion, this is how I'd recommend implementing getView() 视图持有人很奇怪,通常会引起混乱,这就是我建议实现getView()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM