我有我的ListView设置,但它只显示3行数字1-3,它没有显示我的数据库中的条目。我试图找到答案,但这个ListView主题非常模糊,我可以'找到关于如何从我的数据库条目中显示文本的明确答案。 这是我的xml布局,ListView类和数据库中的Cursor条目的代码。
布局:
<?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:background="@drawable/nfbackground"
android:orientation="vertical" >
<ImageView
android:id="@id/titlebar"
android:layout_width="wrap_content"
android:layout_height="66dp"
android:src="@drawable/nftitlebar" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/searchimageview"
android:textColor="@color/black"
android:textSize="25dp"
android:textStyle="bold"
android:text=""
android:orientation="vertical" >
</ListView>
</LinearLayout>
代码:
package com.fullfrontalgames.numberfighter;
import com.fullfrontalgames.numberfighter.DBAdapter;
import com.fullfrontalgames.numberfighter.R;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class PlayAFriend extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playafriend);
ListView FriendLV = (ListView) findViewById(android.R.id.list);
DBAdapter db = new DBAdapter(this);
db = db.open();
Cursor friendslist = db.GetAllFriends();
String[] from = new String[] {"ID","USERNAME","FRIENDS"}; // your column/columns here
int[] to = new int[] {android.R.id.text1};
@SuppressWarnings("deprecation")
ListAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, friendslist, from, to);
FriendLV.setAdapter(cursorAdapter);
db.close();
}
}
代码:
public Cursor GetAllFriends()
{
Cursor cursor = db.rawQuery("select rowid _id,* from NFDB", null);
int iRow = cursor.getColumnIndex("ID");
int iName = cursor.getColumnIndex("USERNAME");
int iFriends = cursor.getColumnIndex("FRIENDS");
if (cursor.moveToFirst()) {
do {
TextView friendslist = new TextView(context);
friendslist.setId(Integer.parseInt(cursor.getString(iRow)));
friendslist.setText(cursor.getString(iName));
friendslist.setText(cursor.getString(iFriends));
} while (cursor.moveToNext());
}
return cursor;
}
答案 0 :(得分:1)
您需要为列表视图列创建一个包含2个TextView的布局
list_items.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="wrap_content"
android:padding="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
更改
String[] from = new String[] {"USERNAME","FRIENDS"}; // your column/columns here
int[] to = new int[] {textview_name, textview_friends};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_items, friendslist, from, to, 0);
FriendLV.setAdapter(cursorAdapter);
//db.close(); Close the db in onDestroy
更改
public Cursor GetAllFriends()
{
return db.rawQuery("select * from NFDB", null);
}