我不知道如何設置我的活動layour權。我有一個頂部按鈕的佈局,當我點擊它時,我想填充ListView來呈現一些數據(比如有序比薩餅的歷史記錄)。但是,當我點擊按鈕,我得到這個:ListView和其他元素的活動
我不明白爲什麼這個按鈕保存拷貝。我只想要一個在頂部。
activity_tab_history.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.tomek.pizzaservice.TabHistoryActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/buttRefresh"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="buttRefreshAction" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:layout_below="@+id/textViewItem" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewItem"
android:layout_below="@+id/buttRefresh"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
代碼:
public void buttRefreshAction(View view){
listView.setAdapter(loadDataFromDatabase());
}
private ArrayAdapter<String> loadDataFromDatabase(){
Toast.makeText(TabHistoryActivity.this, "Loading orders", Toast.LENGTH_SHORT).show();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_tab_history, R.id.textViewItem);
Random rand = new Random();
for(int i=0; i<10; i++){
arrayAdapter.add("Order date: 2000:01:" +(01+i)+
", pizza: " + (rand.nextInt(15)+1) +
". SOME PIZZA NAME BLA BLA BLA" +
" Status: somthin");
}
Toast.makeText(TabHistoryActivity.this, "Completed loading orders", Toast.LENGTH_SHORT).show();
return arrayAdapter;
}
'R.layout.activity_tab_history'是怎麼看的? – Emmanuel
@Emmanuel這是我包括這個XML。 – Tomasz
函數離開活動然後回來嗎? –