androidlayoutlistactivity

Add listview and a linearlayout inside a ListActivity android


I'm using ListActivity to set up my listview. What I want to do is I want to display listview in upper portion and a linearlayout at parent bottom.

Since I'm using ListActivity, I'm not laying out any layout file. Still what I managed to do is getting the listview instance but when I'm trying to set it in linearLayout I'm getting an error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

I'm using the following code:

  final ListView lv = getListView();    

  LinearLayout root = new LinearLayout(this);
  root.setOrientation(LinearLayout.VERTICAL);
  root.setLayoutParams(new LayoutParams(
  LinearLayout.LayoutParams.FILL_PARENT,
  LinearLayout.LayoutParams.FILL_PARENT));

  root.addView(lv);
  setContentView(root);

Solution

  • what you got to do is define your custom layout, doesn´t matter if is a ListActivity always that your custom xml layout has one ListView with id: android:id="@android:id/list"

    i defined my activity layout like this:

    <?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"
      android:background="@drawable/background_new_search_activity_1">
    
        <include layout="@layout/generic_header_layout" android:id="@+id/listaBusquedasHeader"/>
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"/>
    
    </LinearLayout>
    

    later on my activity create defined the layout above:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            setContentView(R.layout.lista_busquedas_activity);
            listView = (ListView) findViewById(android.R.id.list);
            listView.setOnItemClickListener(listItemListener);
            ((TextView)findViewById(R.id.genericHeaderTitle)).setText(R.string.listaBusquedasGuardadasTitle);
    
            if(LoginManager.getInstance().existeUsuarioValidoLogueado()){
                aTask = new AsynchronousTask(this, AsynchronousTask.MODE_STRING, handler, OBTENER_BUSQUEDAS_GUARDADAS);
                aTask.execute( OperationsFacadeManager.obtenerBusquedasGuardadas(1) );
            }else{
                Toast.makeText(this, R.string.genericErrorUserNotLogged, Toast.LENGTH_SHORT);
            }
        }
    

    that´s all, folks