2015-10-04 26 views
0

我想要一個按鈕,在點擊它時創建一個新的文本視圖。我有執行以下功能的onCreate方法:顯示一個新的文本視圖,其中包含一個對sqllite數據庫的請求結果

public void request(View v) { 
    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

    String[] projection = { 
      FeedReaderContract.FeedEntry._ID, 
      FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, 
    }; 


    String sortOrder = 
      FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + " DESC"; 

    Cursor cursor = db.query(
      FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query 
      projection,        // The columns to return 
      null,        // The columns for the WHERE clause 
      null,       // The values for the WHERE clause 
      null,          // don't group the rows 
      null,         // don't filter by row groups 
      sortOrder         // The sort order 
    ); 


} 

如何在新的textview上顯示光標的內容? 我需要使用哪種方法以及如何完成上面的代碼?

回答

1

首先從遊標對象中獲取想要顯示的字符串。

String strColumnValue=cursor.getString(mCursor.getColumnIndex(COLUMN_NAME_TITLE)));

COLUMN_NAME_TITLE是表

規定現在你已經得到了字符串值的列。創建一個TextView動態地使用下面的`

LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
TextView tv=new TextView(this); 
tv.setLayoutParams(lparams); 
tv.setText(strColumnValue); 
this.layout_name.addView(tv); 

下面的代碼「LAYOUT_NAME」是你要添加的TextView在具體佈局。

相關問題