android - 在 Firebase 的列表视图中显示数据 - IndexOutOfBoundsException
问题描述
我正在尝试从 Firebase 获取信息并使用信息类在 ListView 上显示它。在 CustomList 中显示此错误
进程:com.legiontechsolutions.antigravity,PID:21862 java.lang.IndexOutOfBoundsException
public class CustomList extends ArrayAdapter {
private static Activity context;
private static ArrayList<Information> arrayList;
TextView name, amount, date;
public CustomList(@NonNull Activity context, ArrayList<Information> arrayList) {
super(context, R.layout.list_item);
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Log.e(".............", "" + getItem(position));
Information information = (Information) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
name = (TextView) convertView.findViewById(R.id.name);
amount = (TextView) convertView.findViewById(R.id.email);
date = (TextView) convertView.findViewById(R.id.creadits);
name.setText(information.name);
amount.setText(" " + information.email);
date.setText(information.mobile + " ");
return convertView;
}
}
我在这里设置这个适配器
try {
adapter = new CustomList(getActivity(), arrayList);
} catch (IndexOutOfBoundsException e) {
Log.e("Exception is:", "" + e);
}
listViewCustomers.setAdapter(adapter);
解决方案
你不能像这样使用 ArrayAdapter。Default Array Adapter 和 R.layout.simple_list_item_1 在 ListView 上显示一个属性。如果你想在 ListView 上显示多个属性,你应该使用自定义数组适配器。
R.layout.simple_list_item_1 有一个 TextView,但您的信息模型有 5 个属性。如果您在默认适配器上使用您的信息列表,它会显示在 ListView information.toString() 中。
请检查以下链接:
https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView
推荐阅读
- azure - 将 C# Teams 消息扩展 (Bot) 部署到 Azure 的其他步骤?
- javascript - 在 React 中将 shouldcomponentupdate 转换为备忘录
- css - 如何将右框组件移动/对齐到更靠近左侧的位置?
- linux - 在 Linux shell 中对文本块进行排序?
- html - 将 HttpOnly JWT 令牌从父应用程序传递到嵌入式 iFrame?
- apache-kafka - 使用 KafkaJS 创建主题时出错
- ckeditor - 导入带格式的 RTF 文本
- javascript - 如何在 JS 中删除正确的对象属性值
- angular - 无法以编程方式禁用角度材料日期选择器
- r - 如何将数字格式化为百分比并限制小数位数