尝试从Firebase显示listview时出现java.lang.ArrayIndexOutOfBoundsException错误

时间:2017-04-02 22:36:10

标签: android listview firebase firebase-realtime-database

我正在构建一个应用程序,用户可以将数据存储到firebase数据库,并可以通过日志片段上的列表视图访问它,所以在开始时我将数据存储在输入活动的哈希映射中:

String BloodLevels = (bgET.getText().toString());
            String Carbohydrates = (carbET.getText().toString());
            String InsulinTaken = (insulinET.getText().toString());
            String DateandTime = DateFormat.getDateTimeInstance().format(new Date());

            HashMap<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("DateandTime", DateandTime);
            dataMap.put("BloodLevels", BloodLevels);
            dataMap.put("Carbohydrates", Carbohydrates);
            dataMap.put("Insulin", InsulinTaken);

            dataRef.push().setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (task.isSuccessful()){

                        progress.dismiss();

                        Toast.makeText(InputActivity.this, "Your Data Has Been Saved", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(InputActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();


                    }else{

                        progress.dismiss();

                        Toast.makeText(InputActivity.this, "Error: Your data could not be saved", Toast.LENGTH_SHORT).show();
                    }

然后我尝试在logbook片段中的listview中调用数据:

public void onActivityCreated(Bundle savedInstanceState) {
    getActivity().setTitle("Logbook");
    super.onActivityCreated(savedInstanceState);
    databaseQuery = FirebaseDatabase.getInstance().getReference().child("Entries");

    databaseQuery.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            HashMap<String, String> map = (HashMap<String, String>) dataSnapshot.getValue();
            ListView listView = (ListView) getActivity().findViewById(R.id.entriesList);
            List<HashMap<String,String>> listitems = new ArrayList<>();
            SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
                    new String[]{"First Line", "Second Line, Third Line, Fourth Line"},
                    new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});
            Iterator it = map.entrySet().iterator();
            while (it.hasNext()){
                HashMap<String, String> resultsMap = new HashMap<>();
                Map.Entry pair = (Map.Entry)it.next();
                resultsMap.put("First Line", pair.getKey().toString());
                resultsMap.put("Second Line", pair.getValue().toString());
                resultsMap.put("Third Line", pair.getKey().toString());
                resultsMap.put("Fourth Line", pair.getValue().toString());
                resultsMap.put("Fifth Line", pair.getKey().toString());
                resultsMap.put("Sixth Line", pair.getValue().toString());
                resultsMap.put("Seventh Line", pair.getKey().toString());
                resultsMap.put("Eighth Line", pair.getValue().toString());
                listitems.add(resultsMap);
            }
            listView.setAdapter(adapter);
        }

这是我在listview上使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">\

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:textColor="@color/textDark"
    android:textSize="14dp"
    android:textStyle="bold"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text2"
    android:textColor="@color/textDark"
    android:textSize="14dp"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text3"
    android:textColor="@color/textDark"
    android:textSize="14dp"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text4"
    android:textColor="@color/textDark"
    android:textSize="14dp"/>

</LinearLayout>

但是当我启动应用程序时,我收到此错误并且应用程序崩溃:

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.kenjasim.glucosehelper, PID: 10154   java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

所以我不知道如何解决这个问题,并希望能得到任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

部分错误消息没有显示太多,但我希望这是SimpleAdapter的构造函数中的错误。

SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
    new String[]{"First Line", "Second Line, Third Line, Fourth Line"},
    new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});

看起来目的是拥有一个包含4个字符串的数组,但是通过引号的排列,"Second Line, Third Line, Fourth Line"是一个字符串,这意味着数组中只有两个字符串。

尝试:

SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitems,R.layout.list_item_log,
    new String[]{"First Line", "Second Line", "Third Line", "Fourth Line"},
    new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});