ListView随机订购商品?

时间:2013-04-25 16:40:39

标签: java android listview

我的列表视图是随机排序的项目,当我向下或向上滚动项目位置随机变化我尝试了很多方法来解决这个问题但没有成功

我用谷歌搜索过,我找到了太多方法来修复与扩展列表视图相关的问题,但它没有使用我的代码

请一些帮助

这是listview代码

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

3 个答案:

答案 0 :(得分:3)

我认为你误解了ViewHolder的用途。 ViewHolder不是保存值而是保持视图,因此您不必再次对它们进行充气。即使从标记中获取视图,您仍然需要设置数据。

这是您如何正确使用View持有人:

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

答案 1 :(得分:1)

如果convertView != null,您不会填充数据。您应该阅读另一个使用viewHolder的示例。这次仔细检查一下。

答案 2 :(得分: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()

的方式