gpt4 book ai didi

java - 显示循环读取的第一个图像

转载 作者:行者123 更新时间:2023-11-30 10:59:09 25 4
gpt4 key购买 nike

我想显示用户输入的字母对应的图像。但问题是,循环会重复显示已读取的第一张图像。

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_list, null, true);
ImageView imageView = (ImageView)rowView.findViewById(R.id.img);

if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list, parent, false);
}

for (int loop = 0; loop < ALPHA.length; loop++) {
for (int j = 0; j < alphaSplit.length; j++) {
if (alphaSplit[j] == ALPHA[loop]) {
imageView.setImageResource(ICONS[loop]);
}
}
}
return rowView;
}

这已经是我修改后的 GET View

public View getView(int position, View convertView, ViewGroup parent) {

Holder holder;

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

holder = new Holder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img);

convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}

for (int loop = 0; loop < alphaSplit.length; loop++) {
for (int j = 0; j < ALPHA.length; j++) {
if (alphaSplit[loop] == ALPHA[j]) {
holder.imageView.setImageResource(ICONS[j]);
}
}
}
return convertView;
}

但它会显示循环获取的最后一个相应图像

最佳答案

你把整个 getView 的想法搞错了。

首先,不要一遍又一遍地定义LayoutInflater。为您的适配器创建一个构造函数,并让它获取 Context 作为参数。

LayoutInflater inflater;
public YourAdapter(Context context) {
inflater = LayoutInflater.from(context);
}

其次,明智地使用 convertView,将静态容器设置为它的标签:

static class Holder{
ImageView imageView;
}

public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;

if (convertView == null) {
convertView = inflater.inflate(...); //I'd recommend setting it (*layout, null);

holder = new Holder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img);

convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}

for (int loop = 0; loop < ALPHA.length; loop++) {
for (int j = 0; j < alphaSplit.length; j++) {
if (alphaSplit[j] == ALPHA[loop]) {
holder.imageView.setImageResource(ICONS[loop]);
}
}
}

return convertView;
}

请看,检查 convertView == null 仅用于定义和扩充第一个可见 View 。然后你想更新相同 View 中的数据,这样它就会产生滚动的错觉(而不是每次都实际膨胀新 View )。

关于java - 显示循环读取的第一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com