2013-04-05 103 views
1

我有我的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; 
     } 

回答

1

您需要创建2的TextView布局为您的ListView列

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

    } 
+0

我编辑我的答案。 – 2013-04-05 01:14:14

+0

非常感谢你,我现在可以看到我的作品,我可以从这里完成剩下的工作! – Cranosaur 2013-04-05 01:16:35

相关问题