【问题标题】:Unable to show my ArrayList data in ListView无法在 ListView 中显示我的 ArrayList 数据
【发布时间】:2016-04-07 04:48:42
【问题描述】:

当我将数据放入 ArrayList 并添加到 ListView 时。但ListView 上没有显示数据,但我确信这些数据已被检索到。

用于测试的系统输出是正确的。(我的数据)

这是我的pseudo-code

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    import com.parse.FindCallback;
    import com.parse.ParseException;
    import com.parse.ParseObject;
    import com.parse.ParseQuery;

    import java.util.ArrayList;
    import java.util.List;

    public class query_page extends AppCompatActivity {
    public ArrayList<String> show_Location, show_Messages;
    public ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_page);
        show_Location = new ArrayList<String>();
        show_Messages = new ArrayList<String>();
        listView = (ListView) findViewById(R.id.listView);
        //Parse.enableLocalDatastore(this);
        //Parse.initialize(this,"Kfl0hkIIHec4uwhufpAc9luukPhz2H4QPEQeCgZY","S0eka3H15A58ACGKuvbnIsnKFS6gVQRz9BUCTnQw");
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Danger");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) {
                    for (ParseObject j : list) {
                        show_Location.add(j.getString("DangerLocation"));
                        show_Messages.add(j.getString("DangerMessage"));
                        System.out.println("Location: " + j.getString("DangerLocation"));
                        System.out.println("Messages: " + j.getString("DangerMessage"));
                    }
                    ArrayAdapter adapter = new ArrayAdapter(query_page.this, android.R.layout.simple_list_item_1, show_Location);
                    listView.setAdapter(adapter);

                } else {
                    System.out.println("Exception occur! ");
                }
            }
        });
    }
}

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.s1011423_term_project.query_page">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"></ListView>

</RelativeLayout>

【问题讨论】:

  • 请发布完整的代码适配器!
  • 您确定从 Parse 接收数据吗?你能发布你的活动的xml吗?顺便说一句,在 Java 中我们使用 CamelCase,没有下划线
  • @QuangDoan,这里有我的完整代码,谢谢
  • @jegesh,已经添加了 XML 文件。我是安卓新手,谢谢你的建议!
  • 请不要用现有答案更改问题的内容,并在提供解决方案后添加附加信息。这会导致答案失效。

标签: android android-layout arraylist android-listview android-adapter


【解决方案1】:

使您的适配器与 Arraylist 一起成为您类中的一个字段。

private ArrayAdapter adapter;

并且不要在 Parse 回调中初始化适配器。您不需要列表中的任何数据来设置适配器。

所以移动这些行

adapter = new ArrayAdapter(query_page.this, android.R.layout.simple_list_item_1, show_Location);
listView.setAdapter(adapter);

紧接着

listView = ...;

在 Parse 代码中 done 的末尾,调用

adapter.notifyDataSetChanged();

原因是您更改了基础数据。您也可以直接添加到适配器。

adapter.add(j.getString("DangerLocation"));

【讨论】:

  • 感谢@cricket_007,我已经修改了我的代码,但出现了错误...“java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String java.lang.Object.toString ()' 在空对象引用上
  • 这与我发布的内容无关。 j.getString("DangerLocation") 很可能返回 null,因为 DangerLocation 不是 j 中的字符串。如果这篇文章对你有帮助,你可以接受它并用你的新错误创建一个新问题
【解决方案2】:

还要确保activity_mail.xml中定义的ListView的宽度和高度不为0(在推断约束期间可能变为0) 确保它具有 match_parent 之类的值或任何大于 0 的值 android:layout_width="match_parent" android:layout_height="match_parent"

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2017-08-15
    • 2021-05-22
    相关资源
    最近更新 更多