2017-02-12 46 views
0

如何爲每行創建具有相同數據的多行應用程序。這就是所有行的樣子。它包含三個文本視圖和兩個旋鈕。這個程序是幫助計算一學期的學生一學期的GPA。如何創建多行活動

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.mbedobe.android.samplegpacalculator.app.MainActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="C1" 
    android:id="@+id/course_textView"/> 



<Spinner 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/spinner" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/course_textView" 
    android:layout_toEndOf="@+id/course_textView" 
    android:layout_marginLeft="23dp" 
    android:layout_marginStart="23dp" /> 

<Spinner 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/spinner2" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/spinner" 
    android:layout_toEndOf="@+id/spinner" 
    android:layout_marginLeft="23dp" 
    android:layout_marginStart="23dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/spinner2" 
    android:layout_toEndOf="@+id/spinner2" 
    android:id="@+id/gradePoints" 
    android:layout_marginLeft="23dp" 
    android:layout_marginStart="23dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/gradePoints" 
    android:layout_toEndOf="@+id/gradePoints" 
    android:id="@+id/gradeValue" 
    android:layout_marginLeft="23dp" 
    android:layout_marginStart="23dp"/> 

</RelativeLayout> 
+0

查找ListView或RecyclerView。 –

+0

您正在尋找['ListView'](https://developer.android.com/guide/topics/ui/layout/listview.html)與自定義列表項 –

回答

1

您應該考慮使用ListView with BaseAdapter

+0

使用基本適配器的列表視圖後,我得到了一個空白屏幕 –

+0

列表視圖與基礎適配器工作得很好。 –

1

使用RecyclerViewcustom adapter。上面的佈局將是這種情況下每行的行佈局。

使用上面的鏈接瞭解如何創建與自定義佈局自己的自定義RecyclerView適配器。

編輯:

好吧,我會在這裏給你的基礎知識。您需要自定義RecyclerView的三件大事。

它們是:

  1. 列出對象的(數據)
  2. 每一行
  3. 一個適配器,將改變你的數據在上述佈局中的自定義佈局。

現在,讓我們舉一個例子,並試着解釋這一點。

你一定見過流行的應用程序,如Gmail,Whatsapp等 當您打開這些應用程序,你會看到電子郵件/聊天等的列表 有一點要注意的是,每行的佈局是一樣的,甚至儘管它們內的內容是不同的。

這告訴我們兩件事情:第一,數據必須是同一類型的對象的列表,其次,每行的佈局是一樣的。因此,要創建這樣的listView或RecyclerView,我們不需要創建與行一樣多的佈局。一行可以重複使用一個佈局。

所以,我們的兩個三個方面的需求理解。最後的項目是適配器。該適配器是什麼需要您的列表並將列表中的每個項目轉換爲您的RecyclerView中的一行。這些行由適配器在用戶滾動列表時自動創建,並在用戶無法再看到這些行時移除。

如果你想代碼這一點,讓我知道。我將上傳代碼解釋這一點。但我會建議你自己嘗試一下。這並不難。

+0

我已經看過這些和其他一些教程,但我我並不真正瞭解正在發生的事情。你能幫我嗎? –

+0

我看了你給的鏈接,但問題是我不理解如何去做。我甚至查了幾個教程,我該如何使用我的佈局? –

+0

@ K.D.MBEDOBE看我的編輯。 – Rachit

0

無論是使用ListView控件,recyclerView或動態滾動視圖中添加的項目。

使用ListView或RecyclerView將是最適合您的方法。

我會給你最後一個應該從java工作的選項,只有當你有良好的java知識。 (只有步驟不完整的代碼)

1) your xml file should contain scroll view. 
2) for each item to be added (3 textview 2 spinner), add a layout and provide layout params for it i.e width/height/orientation/background etc. 
3)add this layout to the scrollview you created in xml by 
findViewById(R.id.scrollviewId).add(layout) 
4)create your textviews/spinners in and give them layout params.{ this process is much logical in a pattern of your design so must be very careful} 
5)add all these textviews/spinners in the layout you created in step 3. 

注:根據您可能需要在步驟2/3和步驟4中創建的其他項目創建的佈局之間添加其他佈局設計要求。

這裏是你喜歡的代碼:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.layout_in_xml); 
for(int i = 0; i < required_fields.length; i++){ 

LinearLayout lr = new RelativeLayout(DisplaySadip.this); 
    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
lr.serLayoutParams(layoutParams); 
lr.setOrientation(LinearLayout.HORIZONTAL); 
//add linear layout to your layout in xml 
myLayout.add(lr); 
//add textview, spinner to this layout now 
TextView tv1 = new TextView(YourActivity.this) 
tv1.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
tv1.setText("......"+i); 
lr1.add(tv1) 

Spinner spinner1 = new Spinner(YourActivity.this) 
spinner.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

Spinner spinner2 = new Spinner(YourActivity.this) 
spinner.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

lr.add(spinner1); 
lr.add(spinner2); 
TextView tv2 = new TextView(YourActivity.this) 
    tv2.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    tv2.setText("......"+i); 
    lr.add(tv2) 
TextView tv3 = new TextView(YourActivity.this) 
    tv3.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    tv3.setText("......"+i); 
    lr1.add(tv3) 
} 

應添加添加許多沒有在你的佈局你想要的景色。

+0

仍然是java的新東西,所以任何幫助表示讚賞 –

+0

發佈所需的佈局圖像 –

+0

xml文件是每行的預期佈局。該應用程序將有最多8行。 –