2013-04-05 169 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

相關問題