customlistadapter向下滚动并设置为初始值时,TextView值会发生变化

时间:2016-03-23 10:12:28

标签: android listview custom-lists

在CustomListAdapter中我有一个增量按钮和一个textview来显示它的值,当我点击增量按钮时我增加值然后将它存储到sqlite db然后我获取该值然后我将它设置为相应的textview。

但是当我向下滚动时,它的值会变为其初始值,即“0” 我之前提到了一个类似的问题,他们建议使用我使用过的视图,但是没有帮助我。 我很感激有人指导我如何做到这一点。 这是CustomListAdapter代码:

public class CustomListAdapter2 extends BaseAdapter {
  private SQLiteHandler db;
//    HashMap<Integer,Integer> quantity = new HashMap<Integer,Integer>();
private Activity activity;
private LayoutInflater inflater;
private List<com.example.malli.myapplication.model.Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private EventBus bus = EventBus.getDefault();

public CustomListAdapter2(Activity activity, List<Movie> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
    Log.d("size",String.valueOf(movieItems.size()));

    db = new SQLiteHandler(activity.getApplicationContext());
}


@Override
public int getCount() {
    return movieItems.size();
}

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)       


   {
     ViewHolderItem viewHolder = null;
    if (inflater == null) {
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row2, null);


            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();

            viewHolder = new ViewHolderItem();
            viewHolder.thumbNail = (NetworkImageView) convertView
                    .findViewById(R.id.thumbnail);
          viewHolder.title=(TextView)convertView.findViewById(R.id.title);          
          viewHolder.rating=(TextView)convertView.findViewById(R.id.rating);
          viewHolder.genre=(TextView) convertView.findViewById(R.id.genre);
      viewHolder.addbutton=(Button)convertView.findViewById(R.id.addbutton);
        viewHolder.qty = (TextView) convertView.findViewById(R.id.qty);
        convertView.setTag(viewHolder);
        }else
        {
            viewHolder = (ViewHolderItem) convertView.getTag();

        }
            final Integer uniqueKey = Integer.valueOf(position);
            final Movie m = movieItems.get(position);

            // store the holder with the view.
            //    convertView.setTag(viewHolder);
            for(int j =0;j<20;j++){

                 String val1=db.getUser(j);

              if(val1 != null) {
                   viewHolder.qty.setText(db.getUser(j));
              }
               else{
                    viewHolder.qty.setText("0");
                   }
            }

    final ViewHolderItem finalViewHolder = viewHolder;

    viewHolder.addbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String val = finalViewHolder.qty.getText().toString();
            if(val == null)
            val= String.valueOf(1);
            else {
                val = String.valueOf(Integer.valueOf(val) + 1);
            Log.d("val",String.valueOf(val));
            } 
            db.add(m.getTitle(), String.valueOf(val),    
    String.valueOf(m.getRating1(),String.valueOf(m.getRating1()));

             String val2 = db.getUser(m.getRating1());
            finalViewHolder.qty.setText(db.getUser(m.getRating1()));
                db.addtotalcost(String.valueOf(
            Integer.valueOf(val)*m.getRating1()));
            String totalcost = db.getTotalCost2();
            EventBus.getDefault().post(new  
                ChargingEvent(Integer.valueOf(totalcost)));

                }
    });
    // thumbnail image
    viewHolder.thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    // title
    viewHolder.title.setText(m.getTitle());

    // rating
    viewHolder.rating.setText("Cost: " + String.valueOf(m.getRating1()));

    // genre
    String genreStr = "";
    for (String str : m.getGenre()) {
        genreStr += str + ", ";
    }
    genreStr = genreStr.length() > 0 ? genreStr.substring(0,
            genreStr.length() - 2) : genreStr;
    viewHolder.genre.setText(genreStr);

    // release year
    //year.setText(String.valueOf(m.getYear()));

    return convertView;
   }
  }

1 个答案:

答案 0 :(得分:0)

添加此行

1e-8

之后

movieItems.set(position, m);

这将更新数据集中属性的值,因此适配器在m.setqty(val1);

呈现时查看新数据