Reputation: 36219
I dont know how to set up my activity layour right. I have a layout with button at the top and when I click it I want to populate ListView to present some data(like history of ordered pizzas). But when I click button I get this:
I dont understand why this button keeps copying. I just want one at the top.
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>
code:
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;
}
Upvotes: 0
Views: 126
Reputation: 13435
You are telling the adapter that the each row has the same layout as the whole thing.
look at
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_tab_history, R.id.textViewItem);
the second item given should be the layout for your row. it should be like this
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.my_row_template, R.id.textViewItem);
and the row template is another layout file which shows what you want each row to look like, on the other hand you could do this
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1);
which will use the default android row layout of just a line of text
Upvotes: 3
Reputation: 13233
If R.layout.activity_tab_history
is the XML included, it contains a Button
. This layout is the one being used as the layout to inflate for every row.
You are also including a ListView
for every row item.
Upvotes: 2
Reputation: 1881
You are using the entire history tab as your list item in the ArrayAdapter
. So every time you're adding you're inflating the view with the button in it.
Make your list item something simple, that contains just a TextView
.
Upvotes: 2