2012-03-30 3 views
1

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> 

당신이 버튼의 방법의 setText()를 사용합니다

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

MySQLiteHelper.java :

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 ... 시도해 보겠습니다 ... –

관련 문제