gpt4 book ai didi

android - 尝试从 Firebase 显示 ListView 时出现 java.lang.ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-11-30 00:37:29 25 4
gpt4 key购买 nike

我正在构建一个应用程序,用户可以在其中将数据存储到 firebase 数据库中,并可以通过日志 fragment 上的 ListView 访问它,因此一开始我将数据存储在输入 Activity 的 HashMap 中:

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();
}

然后我尝试在日志 fragment 中调用 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

所以我不知道如何解决这个问题,如果能得到任何帮助,我将不胜感激。谢谢

最佳答案

部分错误消息并没有透露太多,但我认为这是您的 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});

关于android - 尝试从 Firebase 显示 ListView 时出现 java.lang.ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174274/

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