英文:
Custom adapter for ListView ignores set values
问题
我将为您提供代码的中文翻译:
// 我打算创建一个包含标签和图标的ListView。
这是我所做的事情:
// 我创建了一个名为“cardList”的空白ListView。
// 我创建了一个自定义视图,扩展了“ArrayAdapter<String>”。
// 然后我将“cardList”设置为该自定义视图:
cardList = findViewById(R.id.cards_list);
CustomView customView = new CustomView(this, names, images);
cardList.setAdapter(customView);
其中
ListView cardList;
String[] names = {"Name1", "Name2", "Name3"};
Integer[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
// CustomView类:
package ...
import ...
public class CustomView extends ArrayAdapter<String> {
private String[] names;
private Integer[] images;
private Activity context;
CustomView(Activity context, String[] names, Integer[] images) {
super(context, R.layout.card_row, R.id.cardName, names);
this.context = context;
this.names = names;
this.images = images;
}
@SuppressLint("InflateParams")
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View r = convertView;
ViewHolder viewHolder;
if (r == null) {
LayoutInflater layoutInflater = context.getLayoutInflater();
r = layoutInflater.inflate(R.layout.card_row, null, true);
viewHolder = new ViewHolder(r);
r.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) r.getTag();
}
viewHolder.name.setText(/*跳过:names[position]*/"一些文本");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);
return super.getView(position, convertView, parent);
}
class ViewHolder {
TextView name;
ImageView image;
ViewHolder(View v) {
name = v.findViewById(R.id.cardName);
image = v.findViewById(R.id.symbolImg);
}
}
}
现在,我故意将这行代码 viewHolder.name.setText(/*跳过:names[position]*/"一些文本");
编写成这样。结果是我的ListView显示了数组中的名称,尽管方法setText("一些文本")
应该更改它,而且没有图标和自定义字体。所以,问题出现在这一部分:
viewHolder.name.setText(/*跳过:names[position]*/"一些文本");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);
调试显示应用程序经过此部分,但应在其中设置的所有内容都被忽略了。我是否漏掉了什么?
<details>
<summary>英文:</summary>
I'm going to create a `ListView` of labels and icons.
Here's what I've done:
<br>I've created a blank `ListView` called `cardList`.
<br>I've created a custom view that extends `ArrayAdapter<String>`
<br>And I've set the `cardList` to that custom view:
cardList = findViewById(R.id.cards_list);
CustomView customView = new CustomView(this, names, images);
cardList.setAdapter(customView);
where
ListView cardList;
String[] names = {"Name1", "Name2", "Name3"};
Integer[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
The `CustomView` class:
package ...
import ...
public class CustomView extends ArrayAdapter<String> {
private String[] names;
private Integer[] images;
private Activity context;
CustomView(Activity context, String[] names, Integer[] images) {
super(context, R.layout.card_row, R.id.cardName, names);
this.context = context;
this.names = names;
this.images = images;
}
@SuppressLint("InflateParams")
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View r = convertView;
ViewHolder viewHolder;
if (r == null) {
LayoutInflater layoutInflater = context.getLayoutInflater();
r = layoutInflater.inflate(R.layout.card_row, null, true);
viewHolder = new ViewHolder(r);
r.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) r.getTag();
}
viewHolder.name.setText(/*skipping: names[position]*/"Some text");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);
return super.getView(position, convertView, parent);
}
class ViewHolder {
TextView name;
ImageView image;
ViewHolder(View v) {
name = v.findViewById(R.id.cardName);
image = v.findViewById(R.id.symbolImg);
}
}
}
Now, on purpose I wrote the line `viewHolder.name.setText(/*skipping: names[position]*/"Some text");` like this. The result is that my `ListView` is displayed with names from the array although the method `setText("Some text")` should change that, and there are no icons and custom font. So, the problem is with this section:
viewHolder.name.setText(/skipping: names[position]/"Some text");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);
Debugging has shown that the application goes through this section, but all the things that should be set inside of it are ignored. Am I missing something?
</details>
# 答案1
**得分**: 0
以下是要翻译的内容:
你的自定义视图被忽略了,因为你在 `getView` 中返回了父视图,而不是你自己的视图。
请使用:
```java
return r;
而不是:
return super.getView(position, convertView, parent);
英文:
Your custom view is ignored as you return parent view from getView
instead of your one
Use
return r;
Instead of
return super.getView(position, convertView, parent);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论