gpt4 book ai didi

Android ArrayAdapter 只工作一次

转载 作者:行者123 更新时间:2023-11-30 03:53:42 24 4
gpt4 key购买 nike

我有一个带有自定义 ArrayAdapter 的 ListView ,它设置在一个 fragment 中。仅设置 ListView 中的第一行。

这可能是一般使用带有 fragment 的 ListViews 时的常见问题,因为我已经按照来自 here 的自定义 ArrayAdapters 教程进行操作。字面意思(据我所知)。

我的 fragment :

public class MyFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);

ListView myListView = (ListView) v.findViewById(R.id.custom_listview);

ArrayList<CustomRow> customList = new ArrayList<CustomRow>();
customList.add(new CustomRow("Test A", "A", "AA");
customList.add(new CustomRow("Test B", "B", "BB");
customList.add(new CustomRow("Test C", "C", "CC");

CustomAdapter customAdapter = new CustomAdapter(getActivity(), R.layout.listitem_custom, customList);
myListView.setAdapter(customAdapter);

//Inflate the layout for this fragment
return v;

}

因此,即使列表中添加了三行,也只有第一个 ListItem 显示“Test A”、“A”、“AA”。最后两行只是保留布局中定义的文本(即文本实际上从未设置为任何内容,甚至没有设置为空)

自定义数组适配器

public class CustomAdapter extends ArrayAdapter<CustomRowItem> {

Context context;
ArrayList<CustomRowItem> rowList;

public CustomAdapter(Context context, int textViewResourceId, ArrayList<CustomRowItem> rowList) {
super(context, textViewResourceId, rowList);
this.context = context;
this.rowList = rowList;
}

public static class ViewHolder {
public TextView tvText1;
public TextView tvText2;
public TextView tvText3;
}

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

ViewHolder holder;

// called when listview is not instantiated yet (first run)
if (convertView == null) {

// assigns the layout custom_row.xml as the listitem (a single row)
LayoutInflater alarmInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = alarmInflater.inflate(R.layout.listitem_custom, null);

holder = new ViewHolder();
holder.tvText1 = (TextView) convertView.findViewById(R.id.textview1);
holder.tvText2 = (TextView) convertView.findViewById(R.id.textview2);
holder.tvText3 = (TextView) convertView.findViewById(R.id.textview3);

convertView.setTag(holder);

// called when list view is already instantiated and we are updating the fields
} else {

holder = (ViewHolder) convertView.getTag();

CustomRowItem newRow = rowList.get(position);

if (newRow != null) {

holder.tvText1.setText(newRow.getText1());
holder.tvText2.setText(newRow.getText2());
holder.tvText3.setText(newRow.getText3());

}

}


return convertView;
}

有什么想法吗?

最佳答案

getView() 中的 else 语句包含的代码多于应有的代码。应该是这样的:

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

ViewHolder holder;

// called when listview is not instantiated yet (first run)
if (convertView == null) {

// assigns the layout custom_row.xml as the listitem (a single row)
LayoutInflater alarmInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = alarmInflater.inflate(R.layout.listitem_custom, null);

holder = new ViewHolder();
holder.tvText1 = (TextView) convertView.findViewById(R.id.textview1);
holder.tvText2 = (TextView) convertView.findViewById(R.id.textview2);
holder.tvText3 = (TextView) convertView.findViewById(R.id.textview3);

convertView.setTag(holder);

// called when list view is already instantiated and we are updating the fields
} else {
holder = (ViewHolder) convertView.getTag(); // <-- that's all for here
}

CustomRowItem newRow = rowList.get(position);
if (newRow != null) {
holder.tvText1.setText(newRow.getText1());
holder.tvText2.setText(newRow.getText2());
holder.tvText3.setText(newRow.getText3());
}

return convertView;
}

关于Android ArrayAdapter 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680801/

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