Im trying to add a list view to my activity. Here is what is suppose to happen:
The user clicks on a button and that uses an asyncTask to get data from the internet. That data should then be added to the list view. The data comes from a table online therefore it there are 10 rows in the table then there should be 10 values in the Listview.
-
But I’m having some problems.
First of all can a listview be added to a pre-existing layout and be updated on the fly? -
Does a list view mean you cannot use the setContentView() method and have to use setListAdapter?
- If you have a layout that already has two buttons arranged on it can the list view be added in below them and still be implemented using the setContentView() method?
Thanks
Edit
Updated Code – Which works:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.new_sightings );
ListView list = getListView();
list.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, values ) );
list.setTextFilterEnabled( true );
}
XML Layout
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation = "vertical">
<TableLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"
>
<TextView
android:id = "@+id/txtTitle"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"
android:layout_weight = "1"
android:gravity = "center"
android:text = "ISS Predictions"
android:textStyle="bold"
android:textColor = "#FFFFFF"
/>
<Button
android:id="@+id/reloadTen"
android:layout_height="wrap_content"
android:layout_width ="fill_parent"
android:layout_gravity = "right"
android:layout_weight = "1"
android:text = "Reload"
android:textColor = "#FFFFFF"
android:textStyle = "bold"
android:background = "#000000"
/>
</TableRow>
</TableLayout>
<ListView
android:id = "@+id/android:list"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "15dp"
android:textStyle = "bold"
/>
</LinearLayout>
1) Yes, you can have a
ListViewin your layout, and update the data displayed by theListView. Typically you’d make a class that inherits from ArrayAdapter to bridge the gap between your underlying data and generating views for yourListView.When you want to update the data displayed, use add() and notifyDataSetChanged() on your adapter.
2) No. You call
setListAdapter()only when your activity inherits fromListActivityand you have aListViewelement with an id of@android:id/list. Here’s an example of such an element:You still use
setContentView()to pass in the desired XML layout to use.3) Yes, for the reasons explained above.