在我更新活动中的数据后,我不知道为什么listview的输出总是处于错误的顺序。
这是我在活动中初始化适配器的方法
$rent_1[0] = equipment_1
$rent_1[0] = quantity_1
$rent_1[0] = time_1
...
我打电话
inboxTradeAdapter=new InboxTradeAdapter(InboxTradeActivity.this, R.layout.activity_inbox_trade, tradeArrayList,loginUserId);
tradeList.setAdapter(inboxTradeAdapter);
在tradeArrayList更新后
这是我的适配器
inboxTradeAdapter.notifyDataSetChanged();
我可以确认tradeArrayList订单在我的活动中是正确的,但listview以随机顺序显示交易。我找不到错误,请帮忙
答案 0 :(得分:0)
我注意到你没有使用持有人模式。始终将其用于优化。 对于手头的问题,尝试使用holder模式重构代码。您可以在线了解更多相关信息。
也没有必要覆盖getItem()
方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Trade trade = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_category, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.category_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.category_gridlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.category_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
}
您的观看者类代码:
static class ViewHolder {
private TextView tradeName;
private TextView tradeLayout;
private ImageView tradeImage;
}
答案 1 :(得分:0)
我找到了解决方案,在适配器中创建了一个视图持有者,并在调用notifyDataSetChanged()
之前创建了一个setter来设置tradeList这是我的回答
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Trade trade = tradeList.get(position);
Log.d("worked",position+": "+trade.getId());
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_trade, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.trade_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.trade_listlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.trade_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (trade != null) {
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
try {
Picasso.with(context)
.load(trade.getImageOwnOne())
.error(R.drawable.ic_menu_camera)
.resize(200, 200)
.into(holder.tradeImage);
}catch(Exception e){
holder.tradeImage.setImageResource(R.drawable.ic_menu_camera);
}
holder.tradeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, DisplayTradeActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("trade", trade);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
public void setTradeList(ArrayList<Trade> tl) {
this.tradeList = tl;
}
static class ViewHolder {
private TextView tradeName;
private CardView tradeLayout;
private ImageView tradeImage;
}