我在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;
}
答案 0 :(得分: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++;
}