2011-12-07 85 views
0

我想從sqlite檢索數據。用戶可以有超過1個分數,我想要顯示它。它可以顯示用戶是否只有一個分數,但是如果用戶有多個分數,它會顯示空白屏幕。從包含多個數據的數據庫檢索數據

這是我的代碼

ScoreDetailActivity.java

public class ScoreDetailActivity extends Activity { 

    protected TextView userName; 
    protected TextView score; 
    protected int user_Id; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scoredetail); 

    user_Id = getIntent().getIntExtra("USER_ID", 0); 
    SQLiteDatabase db = (new DatabaseUsername(this)).getWritableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT alm._id, alm.nama, alm.jekel, sc._id, sc.score, sc.userId FROM almag alm LEFT OUTER JOIN score sc ON alm._id = sc.userId WHERE alm._id = ?", 
          new String[]{""+user_Id}); 

    if (cursor.getCount() == 1) 
    { 
      cursor.moveToFirst(); 

      userName = (TextView) findViewById(R.id.userName); 
      userName.setText(cursor.getString(cursor.getColumnIndex("nama"))); 

      score = (TextView) findViewById(R.id.score); 
      score.setText(cursor.getString(cursor.getColumnIndex("score"))); 


    } 

}} 

scoredetail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <TextView 
      android:id="@+id/userName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <TextView 
      android:id="@+id/score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

我希望有人可以提供幫助。謝謝

回答

2
cursor.getCount() == 1 

意味着只有遊標有一行時才執行該代碼。在任何情況下,如果你也希望能夠顯示多個結果,你應該重新考慮你的佈局(也許listview會更合適)。

+0

謝謝@fedepaol。你有任何教程,你推薦這可能是我的佈局參考? – kumisku

+0

只是尋找listview/listactivity和simplecursoradapter。如果你不需要複雜的元素佈局,它應該是綽綽有餘。 – fedepaol