我有一个自定义适配器类,它扩展了ArrayAdapter
。我在我的活动中将适配器设置为listview
。在我的objects
列表中,我有8个对象。在listview
显示加载时的5个对象(当我滚动listview
时,它还有3个数据要显示)。滚动listview
后,最后3个对象显示为与前3个对象相同。这是我试过的代码。
适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
final MenuItem listItem = objects.get(position);
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + --i);
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
活动
listViweMenu.destroyDrawingCache();
listViweMenu.setVisibility(ListView.INVISIBLE);
listViweMenu.setVisibility(ListView.VISIBLE);
menuListAdapter.notifyDataSetChanged();
List<uk.co.bapos.android.baposwaiter.data.models.menu.MenuItem> itemsList
= MenuItemController.fetchAllCategoryItems(this, String.valueOf(new ArrayList<>(menuItemData.entrySet()).get(position).getValue()));
listDataMenu.clear();
listDataMenu.addAll(itemsList);
我该如何解决这个问题?
答案 0 :(得分:2)
您正在调用getTag(),setTag()并将数据设置在错误的位置。试试这个:
ArrayAdapter adapter = myAdapter();//pass arraylist or data to adapter here
listview.setAdapter(adapter);
获取所有id的视图后,使用setTag(),如果view不为null,请使用getTag()返回所有id。只有在将数据设置到视图之后。
活动:
{{1}}
答案 1 :(得分:1)
试试这个。
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_list_item, parent);
//initializing your view elements
convertView.setTag(holder);
}
else{
holder = (Holder) convertView.getTag();
}
// do other stuff after if-else, setting the values to view elements
答案 2 :(得分:-1)
@Override public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
final MenuItem listItem = objects.get(position);
holder = new Holder(); LayoutInflater inflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView)convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder); }
else
{
holder = (Holder) convertView.getTag();
}
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice()))); // Check & Set if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener()
{
@Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set if (holder.buttonMinus != null)
{
holder.buttonMinus.setOnClickListener(new View.OnClickListener()
{
@Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + --i);
}
} }); }
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
return convertView;
}