无法在 ListView 中显示我的 ArrayList 数据
Unable to show my ArrayList data in ListView
当我将数据放入 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>
使您的适配器与 Arraylist 一起成为 class 中的一个字段。
private ArrayAdapter adapter;
并且不要在解析回调中初始化适配器。您不需要列表中的任何数据来设置适配器。
所以移动这些行
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"));
还要确保activity_mail.xml中定义的ListView的宽高不为0(infer constraints时可能会变成0)
确保它具有 match_parent 之类的值或任何大于 0 的值
android:layout_width="match_parent"
android:layout_height="match_parent"
当我将数据放入 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>
使您的适配器与 Arraylist 一起成为 class 中的一个字段。
private ArrayAdapter adapter;
并且不要在解析回调中初始化适配器。您不需要列表中的任何数据来设置适配器。
所以移动这些行
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"));
还要确保activity_mail.xml中定义的ListView的宽高不为0(infer constraints时可能会变成0) 确保它具有 match_parent 之类的值或任何大于 0 的值 android:layout_width="match_parent" android:layout_height="match_parent"