【问题标题】:put listView child in a hashmap将 listView 子项放入哈希图中
【发布时间】:2016-01-27 08:57:15
【问题描述】:

我正在尝试使用 for 循环从 listView 中获取值并将它们放入哈希图中。但是logcat一直抛出这个错误。我不知道为什么。请提供任何帮助!列表视图有 @android:id/list 并且我在我的活动中扩展了 ListActivity 这是我获取这些值的代码

listStudent=getListView();
studentMarksList=new ArrayList<HashMap<String,String>>();
String student,obtained_value,max_value;
for (int i=0;i<listStudent.getCount();i++) {
    View view=listStudent.getChildAt(i);
    studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls);
    obtainedTxt=(EditText) view.findViewById(R.id.obtained);
    maxTxt=(TextView) view.findViewById(R.id.max);
    student=studentIdTxt.getText().toString();
    obtained_value=obtainedTxt.getText().toString();
    max_value=maxTxt.getText().toString();
    //updating the new mark list array
    HashMap<String,String>studentMark=new HashMap<String,String>();
    studentMark.put(TAG_STUDENT_ID,student);
    studentMark.put(TAG_MARKS_OBTAINED,obtained_value);
    studentMark.put(TAG_MARKS_MAX,max_value);
    studentMarksList.add(studentMark);
}

这是我的列表的 xml 布局

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="106dp" />

这里是列表项的 xml

<?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:id="@+id/student_name_ls"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#1e5401"
    android:textSize="16sp"
    android:textStyle="bold" />

<!-- Marks label -->
 <EditText
    android:id="@+id/obtained"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textColor="#acacac"
    android:inputType="numberDecimal"
    android:numeric="decimal" />
 <TextView
    android:id="@+id/max"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textColor="#0b0b0b"
    android:enabled="false"
    android:editable="false" />
 <TextView
    android:id="@+id/student_id_ls"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textColor="#060606"
    android:visibility="gone" />
   </LinearLayout>

问题出在这一行“studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls);”抛出 nullpointerexception 任何帮助请

【问题讨论】:

  • 不能直接从数据源获取吗?你能发布你初始化ListView的代码吗?

标签: java android xml listview


【解决方案1】:

你应该使用

listStudent.getChildCount()

这是因为列表视图中的视图数等于您看到的视图数。它不等于列表视图中的项目数。所以如果你使用 getCount() 你最终会得到一个 indexOutOfBoundsException 因为没有更多的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2017-10-14
    相关资源
    最近更新 更多