简体   繁体   中英

Is it possible to add another view to an Android listactivity?

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'

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.

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.

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!!!!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM