2012-03-30 3 views
1

ボタンの数でxmlのスクロールビュー(水平)内に1つの線形レイアウトを作成しました。
私はまた、データベースに1つのテーブル "TABLE_SUBJECT"を持っています。プログラムでスクロールビューのボタンにテキストを設定するにはどうすればいいですか?

件名をデータベースからボタンに設定する必要がありますが、その方法はわかりません。私はたくさんのものを検索しましたが、役に立たないものは何も見つかりませんでした。

ヒントや参考情報を入力してください。

ここに私のコードだ:

Main.xml

<HorizontalScrollView android:layout_width="fill_parent" 
android:id="@+id/horizontalScrollView1" 
    android:background="@color/myMaroonColor" 
    android:layout_height="wrap_content"> 
    <LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="62dp" > 
    <!-- android:background="@drawable/qaapti"--> 

     <Button android:textSize="15px" android:id="@+id/button9" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/math" android:text="@string/HomePage_Math" android:background="@color/myMaroonColor" android:layout_width="54dp" android:layout_height="wrap_content" android:clickable="true"></Button> 
     <Button android:textSize="15px" android:id="@+id/button11" android:gravity="center|bottom" android:layout_width="54dp" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/stat" android:text="@string/HomePage_Statstics" android:background="@color/myMaroonColor" android:layout_height="wrap_content"></Button> 
     <Button android:textSize="15px" android:id="@+id/button10" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/biology" android:text="@string/HomePage_Biology" android:background="@color/myMaroonColor" android:layout_width="55dp" android:layout_height="wrap_content"></Button> 
     <Button></Button> 
       - 
       - 
       - 
       - 
     <Button></Button> 
     <Button android:textSize="15px" android:id="@+id/button7" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/physics" android:text="@string/HomePage_Physics" android:background="@color/myMaroonColor" android:layout_width="50dp" android:layout_height="wrap_content"></Button> 
     <Button android:textSize="15px" android:id="@+id/button6" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:text="@string/HomePage_Computer" android:background="@color/myMaroonColor" android:layout_width="58dp" android:layout_height="46dp" android:drawableTop="@drawable/computer"></Button>     
     </LinearLayout> 
</HorizontalScrollView> 

MySQLiteHelper.java

public List<ObjectiveWiseQuestion>getAllSubject() 
{ 
    List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>(); 
    String selectQuery= "SELECT * FROM " + TABLE_SUBJECT; 
    SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) 
     { 
      do { 
       ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion(); 
       owq.setSubjectName(cursor.getString(1)); 
       LocwiseProfileList.add(owq); 
       } 
      while(cursor.moveToNext()); 
      db.close(); 
     } 
     return LocwiseProfileList;  
} 
+0

はあなたがrefrenceためのコードを提供することはできますか? – Jitendra

+0

データベースからテキストを取得する方法を尋ねていますか? – Tony

+1

@トニー:いいえ、私はプログラムでボタンにテキストを設定する方法を尋ねています。 –

答えて

4

あなたはボタンのメソッドのsetText()を使用する必要があります。

btn.setText("whatever_your_text"); 
をあなたのシナリオでは

更新

、あなたが動的にそれを行うことができますどのようにこの:

List<ObjectiveWiseQuestion> tmp = getAllSubject(); 
    int b = 1; //set the starting value as per your layout 
    for (ObjectiveWiseQuestion i : tmp) { 
     int btId = getApplicationContext().getResources() 
          .getIdentifier("button" + b, "id", getPackageName()); 

     Button btn = (Button) findViewById(btId); 
     btn.setText(i.getSubjectName() + b); 

     b++; 
    } 
+0

それはプログラムでボタンにテキストを設定する方法ですか? –

+0

はい............しかし、あなたの場合は、最初にループ内の各ボタンを識別し、setTextを使って名前を変更する必要があります – waqaslam

+0

k ...試してみます... –

関連する問題