我有一个自定义GridView。这是我的应用程序的screesnhot:
现在,当我快速向下滚动时,AYAMBROILER UTUH中editText(2)内的值会变为另一个单元格。这是截图:
为什么会发生这种情况,我该如何解决?
这是我的适配器代码:
public class CustomGridView2 extends BaseAdapter {
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
private Context context;
private String[] imageUrls;
private int count = 0;
int arrayCount[];
SharedPreferences prefs ;
SharedPreference sharedPreference;
public CustomGridView2(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
sharedPreference = new SharedPreference();
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.afterlogin_product_gridview, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.nama_produk);
holder.teaserView = (TextView) convertView.findViewById(R.id.harga);
holder.imageView = (ImageView) convertView.findViewById(R.id.img_produk);
holder.cmdMinus = (Button) convertView.findViewById(R.id.btn_min);
holder.cmdPlus = (Button) convertView.findViewById(R.id.btn_plus);
holder.qty = (TextView) convertView.findViewById(R.id.lbl_qty);
holder.layout1 = (LinearLayout) convertView.findViewById(R.id.layout1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
String satuan = newsItem.getSatuan().toString();
String harga = newsItem.getReporterName().toString();
harga = "Rp. " + harga + " / " + satuan;
holder.headlineView.setText(newsItem.getHeadline().toUpperCase());
holder.teaserView.setText(harga);
String a = newsItem.getUrl();
holder.cmdPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = Integer.parseInt( holder.qty.getText().toString());
count++;
holder.qty.setText(""+count);
}
});
holder.cmdMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = Integer.parseInt( holder.qty.getText().toString());
if(count == 0) {
holder.qty.setText("0");
}
else {
count--;
holder.qty.setText("" + count);
}
}
});
if (holder.imageView != null) {
//new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
Picasso
.with(context)
.load(a)
.fit()
.into(holder.imageView);
}
holder.qty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!holder.qty.getText().toString().matches("0"))
{
holder.layout1.setBackgroundResource(R.drawable.border_radius_gridview);
}
else
{
holder.layout1.setBackgroundResource(R.drawable.border_radius);
}
}
});
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView teaserView;
ImageView imageView;
TextView satuan,qty;
Button cmdPlus,cmdMinus;
LinearLayout layout1;
}
答案 0 :(得分:1)
这是因为视图在所有可滚动视图中重用 - recylcerview listview和gridview。因此,当重复使用这些视图时,它们的值会显示在它不应该的位置。请查看以下链接以解决您的问题。
EditText loses content on scroll in ListView
如果您有任何疑问,请告诉我。
答案 1 :(得分:1)
在ViewHolder模式中视图正在重复使用......因此,在单击加号或减号时向ListItem对象添加数量并从对象中显示它。
holder.teaserView.setText(harga);
holder.qty.setText(String.valueOf(newsItem.getQuantity()));
String a = newsItem.getUrl();
holder.cmdPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = newsItem.getQuantity();
count++;
newsItem.setQuantity(count);
holder.qty.setText(""+count);
}
});
holder.cmdMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = newsItem.getQuantity();
if(count == 0) {
holder.qty.setText("0");
}
else {
count--;
newsItem.setQuantity(count);
holder.qty.setText("" + count);
}
}
});