0

i want to add a waiting circle to my listactivity. I used the answer in this post: Using the "animated circle" in an ImageView while loading stuff

However i think i have to add setContentView of the waiting circle layout in order to be able to use findViewById. The problem is i don't know if i can use setContentView twice in the same activity.

Right now when i try it, i get an error that i most use setContentView with the id of the listview layout i want to use.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);}


    public void showCallList() // show call list on screen
{
    getListView().setVisibility(View.GONE);
    findViewById(R.loadingPanel).setVisibility(View.VISIBLE);
    CallListArrayAdapter adapter = new CallListArrayAdapter(this,
            arrayListCalls);
    setListAdapter(adapter);
    findViewById(R.id.loadingPanel).setVisibility(View.GONE);
    getListView().setVisibility(View.VISIBLE);
}

the exception i get is :

E/AndroidRuntime(4320): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Community
  • 1
  • 1
Ronen Festinger
  • 2,260
  • 1
  • 23
  • 32
  • I addressed your exception in my answer below; in short, the listview in your R.layout.listview must have the id `@android/id:list`. – Tim Oct 17 '12 at 20:26

4 Answers4

0

you can't call setContentView again. instead, you should add your additional layout into your main XML that also contains your ListView. you can then find it there.

Tamir Scherzer
  • 995
  • 8
  • 8
0

Actually you don't need a ListActivity to have a ListView. You can just as well use a normal Activity with a normal layout.xml and just put a <ListView> in there and all the other views you want.

Instead of getListView(), which is defined by ListActivity use the normal findViewById() with the id you gave your ListView in your layout.xml.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
0

If you're trying to render a custom view of your own(The animated circle), and add it to an activity you have to use addContentView(View view, ViewGroup.LayoutParams params), wich adds an additional content view to the activity. Added after any existing ones in the activity -- existing views are NOT removed. These are the parameters: view The desired content to display. params Layout parameters for the view.

But the right way to do this(depending of course in the amount of data you're going to load) is using AsyncTask wich enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. For example,you can call a progress dialog(Function as your waiting circle) on the onPreExecute method and list your files in doInBackground method. Good Luck!!!!

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
  • Well, no other solution worked, I had to both switch to activity instead of a listactivity. and i had to implement the AsyncTask , surprisingly, it worked without a glitch. anyway all solutions here combined together led to the solution. Thank you all. – Ronen Festinger Oct 17 '12 at 22:57
0

Yes, it can be done easily.

Create a layout xml like you would for a normal activity, and inside this activity create an empty listview with android:id=@android:id/list.

When you start the ListActivity, setContentView to your custom layout. The ListActivity will automatically find the listView with the id android:id/list, and will use that listview for all of the list-related functionality.

Relevant text from ListActivity:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

Tim
  • 35,413
  • 11
  • 95
  • 121