在Firebase的列表视图上显示数据-IndexOutOfBoundsException

时间:2018-10-26 17:14:44

标签: android listview arraylist firebase-realtime-database

我正在尝试从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);

1 个答案:

答案 0 :(得分:0)

您不能像这样使用ArrayAdapter。默认阵列适配器和R.layout.simple_list_item_1在ListView上显示一个属性。如果要在ListView上显示多个属性,则应使用“自定义数组适配器”。

R.layout.simple_list_item_1具有一个TextView,但是您的Information模型具有5个属性。如果您使用默认适配器上的“信息”列表,它将显示在ListView information.toString()中。

请检查下面的链接:

https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView