I want to create a table that the columns'text depends on spinner. Later to add the row, i can color the specified cell. It looks like this : http://tinypic.com/view.php?pic=2j639xf&s=8#.U-mRRpD-Lcs That example is landscape because i develop it on dekstop, so i want a table like that as portait in android.
The problem is : 1. when i run it on device, the spinner is gone 2. I don't know yet how to color each cell in the table. Or should I make a new question?
Here's my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservation_schedule);
Spinner spinnerFacility = (Spinner)findViewById(R.id.spinnerFacility);
ArrayList<String> alTop = getTimeTableColumnTop((int)spinnerFacility.getSelectedItemId());
String[] column = alTop.toArray(new String[alTop.size()]);
ArrayList<String> alLeft = getTimeTableColumnLeft();
String[] row = alLeft.toArray(new String[alLeft.size()]);
int rl=row.length+1; int cl=column.length+1;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
private ArrayList<String> getTimeTableColumnTop(int facilitySelectedIndex)
{
ArrayList<String> timeTableColumnTop = new ArrayList<String>();
if(facilitySelectedIndex == 0)
{
timeTableColumnTop.add("Discuss Room 1");
timeTableColumnTop.add("Discuss Room 2");
timeTableColumnTop.add("Discuss Room 3");
}else
{
timeTableColumnTop.add("Pri-View 1");
timeTableColumnTop.add("Pri-View 2");
timeTableColumnTop.add("Pri-View 3");
timeTableColumnTop.add("Pri-View 4");
timeTableColumnTop.add("Pri-View 5");
}
return timeTableColumnTop;
}
private ArrayList<String> getTimeTableColumnLeft()
{
ArrayList<String> timeTableColumnLeft = new ArrayList<String>();
for(int i = 8 ; i<17; i++)
{
timeTableColumnLeft.add(i+":00");
}
return timeTableColumnLeft;
}
For the function createTableLayout(row,column,rl,cl); i use the code from http://www.prandroid.com/2014/05/creating-table-layout-dynamically-in.html
The activity_reservation_schedule.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/reservation_spinner_facility_prompt"/>
<Spinner
android:id="@+id/spinnerFacility"
android:prompt="@string/reservation_spinner_facility_prompt"
android:entries="@array/reservation_facility_entries"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ReservationTab(Schedule)</string>
<string name="reservation_spinner_facility_prompt">Pick a facility</string>
<string-array name="reservation_facility_entries">
<item>Discussion Room</item>
<item>Private Viewing</item>
</string-array>
</resources>
Note : i'm new on Android, i'm using eclipse. This is my first post here, please excuse my english. Any suggestion is appreciated, thanks.
Few points:
If i understood you correctly,
eg
Sample Code:
@Override protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
ll = (LinearLayout)findViewById(R.id.ll);
Spinner spinnerFacility = (Spinner)findViewById(R.id.spinnerFacility);
spinnerFacility.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// more of your code.....
ll.addView(sv) // instead of setContentView(sv)
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
ll.removeView(sv);
ArrayList<String> alTop = getTimeTableColumnTop(pos);
String[] column = alTop.toArray(new String[alTop.size()]);
ArrayList<String> alLeft = getTimeTableColumnLeft();
String[] row = alLeft.toArray(new String[alLeft.size()]);
int rl=row.length+1; int cl=column.length+1;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
sv = new ScrollView(getApplicationContext());
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(getApplicationContext());
hsv.addView(tableLayout);
sv.addView(hsv);
ll.addView(sv);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.