首页 > 解决方案 > 在 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);

标签: androidlistviewarraylistfirebase-realtime-database

解决方案


你不能像这样使用 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


推荐阅读