1

我有我的 ListView 设置,但它只显示 3 行,编号为 1-3,并且没有显示我的数据库中的条目。我试图找到答案,但这个 ListView 主题非常模糊,我找不到关于如何显示我的数据库条目中的文本的明确答案。这是我的 xml 布局、ListView 类和数据库中的光标条目中的代码。

布局:

<?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;  
        }
4

1 回答 1

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);

    }
于 2013-04-05T00:51:27.997 回答