0

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.

  1. 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?

  2. Does a list view mean you cannot use the setContentView() method and have to use setListAdapter?

  3. 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>

2 Answers 2

2

1) Yes, you can have a ListView in your layout, and update the data displayed by the ListView. Typically you'd make a class that inherits from ArrayAdapter to bridge the gap between your underlying data and generating views for your ListView.

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 from ListActivity and you have a ListView element with an id of @android:id/list. Here's an example of such an element:

<ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"/>

You still use setContentView() to pass in the desired XML layout to use.

3) Yes, for the reasons explained above.

Sign up to request clarification or add additional context in comments.

1 Comment

I like both answers above but need to pick one so this one gets it for the code. Thanks, also I have added my code just in case anyone else comes across the same problem
1

You can create a ListView as a part of any XML layout. You needn't extend ListActivity (which I think is where you're getting the idea from) but it can help.

If you don't extend ListActivity, you can setContentView to the ID of whichever layout you have already created (and contains your ListView).

1) Yes, you can add a listview to a pre-existing layout, either statically or on-the-fly (by either LayoutInflater inflation or new ListView(...)).

2) You can use setContentView to set an existing layout, whether it contains a listview or not. If it contains an existing listview you'll need to say findViewById(R.id.myListLayoutId) and make sure to include in your XML.

3) You can add the listview to an existing layout, either before or after anything that already exists, by specifying the index at which you'd like to myExistingView.addView(listView, index).

In any case you will need to set an adapter on the listview (however you have created/added it) which will contain the data to be populated. You can add data to the adapter at any time so long as you notify the adapter that its data set has changed by calling adapter.notifyDataSetChanged(). This will cause the listview whose adapter it is to be updated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.