我坚持了几个小时,我试图为我的 ListView 中的第一个列表项设置一个图像,但其他列表项上的一些 ImageViews 也在发生变化。
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList<ListItem> myItems = new ArrayList<ListItem>();
Player[] players;
public MyAdapter() {
DataBase banco = new DataBase(ScoreBoard.this);
players = banco.consultaJogadores();
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; players[i] != null; i++) {
ListItem listItem = new ListItem();
listItem.caption = players[i].getName();
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.topplayer, null);
holder.textView = (TextView) convertView.findViewById(R.id.topPlayerTextView);
holder.imageview = (ImageView) convertView.findViewById(R.id.topPlayerImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(position + " " + players[position].getName() + " " + players[position].getScore());
if (position == 0) {
holder.imageview.setImageResource(R.drawable.ball);
}
return convertView;
}
}
class ViewHolder {
TextView textView;
ImageView imageview;
}
class ListItem {
String caption;
}
我想从列表中的第一个玩家更改图像。索引为 0、1、4、6、8、9、11、13 和 14 的玩家得到更新,它不遵循逻辑顺序。当我滚动时,图像再次改变。
谢谢