2013-06-18 26 views
5

Hy vọng đây không phải là một câu hỏi tồi, tuy nhiên tôi đã tìm kiếm qua S.O. và không thể tìm ra câu trả lời.Cách tạo số lần xem văn bản thay đổi trong android

Tôi đang tạo một ứng dụng Android chủ yếu là đồng hồ báo thức. Tôi muốn hoạt động chính hiển thị tất cả các báo thức đã được tạo cùng với một số thông tin về báo thức. Câu hỏi của tôi về việc này là cách tạo một số lượt xem văn bản nhất định dựa trên số lượng báo thức đã được tạo. Ví dụ: nếu người dùng đã tạo (và không bị xóa) 5 báo thức, làm cách nào để hiển thị 5 lần xem văn bản thay vì chỉ một số lượt xem văn bản được mã hóa cứng? Sau đây là nguyên mẫu được mã hóa khủng khiếp của tôi để kiểm tra chức năng (ngoài việc gác máy này).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Launch" > 

    <ScrollView 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout android:orientation="vertical" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"> 

      <TextView 
       android:orientation = "vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="75dp" 
       android:text="Time"/> 
        <View 
        android:layout_width="fill_parent" 
        android:layout_height="0.2dp" 
        android:id="@+id/separator" 
        android:visibility="visible" 
        android:background="@android:color/darker_gray"/> 

      <TextView 
       android:orientation = "vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="75dp" 
       android:text="Time"/> 
        <View 
        android:layout_width="fill_parent" 
        android:layout_height="0.2dp" 
        android:id="@+id/separator" 
        android:visibility="visible" 
        android:background="@android:color/darker_gray"/> 

      <TextView 
       android:orientation = "vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="75dp" 
       android:text="Time"/> 
        <View 
        android:layout_width="fill_parent" 
        android:layout_height="0.2dp" 
        android:id="@+id/separator" 
        android:visibility="visible" 
        android:background="@android:color/darker_gray"/> 


     </LinearLayout> 

    </ScrollView> 


</RelativeLayout> 

Như bạn thấy, chỉ có ba chế độ xem văn bản được mã hóa cứng thay vì số lần xem văn bản do người dùng tạo.

Một lần nữa, tôi hy vọng điều này không quá sâu sắc về câu hỏi hoặc câu hỏi có câu trả lời được đăng ở nơi khác nhưng tôi đã tìm kiếm và không thể tìm thấy bất kỳ điều gì.

Cảm ơn trước!

+0

Bạn sẽ cần phải làm điều này programatically, không chỉ thông qua xml. –

+0

bạn có thể sử dụng listview và cursoradapter cho rằng thay vì scrollview.I có nghĩa là nếu bạn lưu báo động trong db sau đó bạn có thể sử dụng một listview với cursoradapter cho báo động hiển thị –

+0

Xem nếu câu trả lời của tôi giúp bạn ra ngoài. Vì bạn mới làm quen với SO, hãy xem [tour] (http://stackoverflow.com/about). –

Trả lời

4

Bạn có thể làm điều này lập trình:

int size = numAlarms; // total number of TextViews to add 

TextView[] tv = new TextView[size]; 
TextView temp; 

for (int i = 0; i < size; i++) 
{ 
    temp = new TextView(this); 

    temp.setText("Alarm: " + i); //arbitrary task 

    // add the textview to the linearlayout 
    myLinearLayout.addView(temp); 

    tv[i] = temp; 
} 
+0

Vì vậy, để chắc chắn Im hiểu đúng, khi sử dụng giải pháp có lập trình bạn đề xuất, tôi cũng cần phải thêm bố cục tuyến tính lập trình cũng như trước đó trong mã hoặc tôi có thể thêm nó vào tham chiếu xml bằng cách nào đó không? Hoặc tôi cho rằng tôi có thể lấy bố cục tuyến tính xml của mình bằng: 'LinearLayout linearLayout = (LinearLayout) findViewById (R.id.IdToBeSetInXml);' –

+0

Có, bạn có thể làm điều đó. –