Android 列表视图异常
我正在从事一个项目,从远程数据库加载一些数据,并在Android 列表视图异常,android,listview,Android,Listview,我正在从事一个项目,从远程数据库加载一些数据,并在列表视图中显示它们。数据加载正确,但问题是在Listview(simpledapter)中显示它们。该类扩展了ListActivity 代码: list_note.xml: user_notes.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi
列表视图中显示它们。数据加载正确,但问题是在Listview
(simpledapter
)中显示它们。该类扩展了ListActivity
代码:
list_note.xml:
user_notes.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
如果有人能给我一个解决方案来修复这个异常,我将不胜感激。
提前感谢…只需将ListAdapter更改为Simple Adapter,并将hashmap对象添加到hashmap arraylist for循环外
protected void onPostExecute(String file_url){
progressDialog.dismiss();
try{
if(jsonObject.getInt("success") == 1){
jsonArray = jsonObject.getJSONArray("notes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
userData.addNote(tempNote);
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("id", o.getString("note_id"));
hashMap.put("description", o.getString("description"));
Log.d(TAG, userNotes.get(0).toString());
}
userNotes.add(hashMap);
}
Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}
catch (JSONException e){
e.printStackTrace();
}
runOnUiThread(new Runnable(){
@Override
public void run() {
SimpleAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id, description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
setListAdapter(listAdapter);
}
});
}
受保护的void onPostExecute(字符串文件\u url){
progressDialog.disclose();
试一试{
if(jsonObject.getInt(“成功”)==1){
jsonArray=jsonObject.getJSONArray(“注释”);
for(int i=0;i
只需将ListAdapter更改为Simple Adapter,并将hashmap对象添加到hashmap arraylist for循环外
protected void onPostExecute(String file_url){
progressDialog.dismiss();
try{
if(jsonObject.getInt("success") == 1){
jsonArray = jsonObject.getJSONArray("notes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
userData.addNote(tempNote);
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("id", o.getString("note_id"));
hashMap.put("description", o.getString("description"));
Log.d(TAG, userNotes.get(0).toString());
}
userNotes.add(hashMap);
}
Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}
catch (JSONException e){
e.printStackTrace();
}
runOnUiThread(new Runnable(){
@Override
public void run() {
SimpleAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id, description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
setListAdapter(listAdapter);
}
});
}
受保护的void onPostExecute(字符串文件\u url){
progressDialog.disclose();
试一试{
if(jsonObject.getInt(“成功”)==1){
jsonArray=jsonObject.getJSONArray(“注释”);
for(int i=0;i
您在代码中所做的是
ListAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id, description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
将此更改为
ListAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id", "description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
您已经创建了新字符串[]{“id,description”}
,应该是
新字符串[]{“id”,“description”}
您在代码中所做的是
ListAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id, description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
将此更改为
ListAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"id", "description"},
new int[] {R.id.list_note_id, R.id.list_note_description});
您已经创建了新字符串[]{“id,description”}
,应该是
新字符串[]{“id”,“description”}
请列出列表注释的布局。请列出列表注释的布局。id说将ListAdapter更改为SimpleAdapter不会有任何区别。它只是一个接口和该接口的具体实现。顺便说一句,hashMap应该添加到for循环中的ArrayList中。无论如何,我将尝试您的解决方案:)我添加了两个xml文件,请检查:)在您的列表中_note.xml文件是否使用了Relativelayout或LinearLayout?我想您错过了这几行ListView lv=getListView();。。。。。。。。因此,您无法在listview中获取数据…所以请尝试一下。我在其他地方获得了它,这很好,问题已经解决,但谢谢!)我认为将ListAdapter更改为SimpleAdapter不会有任何区别。它只是一个接口和该接口的具体实现。顺便说一句,hashMap应该添加到for循环中的ArrayList中。无论如何,我将尝试您的解决方案:)我添加了两个xml文件,请检查:)在您的列表中_note.xml文件是否使用了Relativelayout或LinearLayout?我想您错过了这几行ListView lv=getListView();。。。。。。。。因此,您无法在listview中获取数据…所以请尝试一下。我在其他地方获得了它,这很好,问题已经解决,但谢谢!)哈哈,怎么可能我没看到呢?:)谢谢你,伙计!!!你可能帮我省了几个小时!:)哈哈,怎么可能我没看到呢?:)谢谢你,伙计!!!你可能帮我省了几个小时!:)