Как установить текст для кнопки в просмотре прокрутки программно? - PullRequest
1 голос
/ 30 марта 2012

Я создал один линейный макет внутри вида прокрутки (Горизонтальный) в формате xml с номером кнопки.
У меня также есть одна таблица "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;       
 }

1 Ответ

4 голосов
/ 30 марта 2012

вам нужно использовать метод кнопки 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++;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...