add ListView in the code to Linearlayout


Last Updated:

  1. vccrest

    vccrest New Member This Topic's Starter

    Joined:
    Feb 11, 2010
    Messages:
    2
    Likes Received:
    0
    I am trying to add listView to a LinearLayout inside the code. But its not working for some reason. I am newbiw to this. can somebody help me out.


    //activity class
    ListView listview = new ListView(this);
    ArrayList myArrayList = new ArrayList<String>();
    myArrayList.add(new String("abcd"));
    ArrayAdapter adapter = new ArrayAdapter<String> (getApplicationContext(),R.layout.list,myArrayList );
    listview .setAdapter(adapter);

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.addView(lv); //this one does not work

    -- adding other views like EditText works fine.
     

    Advertisement
  2. SFLeBrun

    SFLeBrun Member

    Joined:
    Feb 11, 2010
    Messages:
    9
    Likes Received:
    0
    What is the XML for R.layout.list? Is it something that you wrote or are you using one of the layouts that come with the Android SDK? You may want to try android.R.layout.simple_list_item_1 from the SDK instead of R.layout.list to see if it makes a difference.

    I am also new to Android application development, only picked up the O'Reilly book less than a fortnight ago and just got my first application running on my phone (with lots of quirks but it will work for what I need it for this weekend). A couple of things that I learned in this last week are:

    ** I found it easier to do most of my layout work in XML in files in /res/layout. Using Eclipse with the Android plug-in makes the static part of the layout easy.

    ** Both of the ListView in my application have a very strange quirk. When they first come up (calling onCreate() in the Activity that hosts the ListView), the List does not appear. If I start typing to shrink the list, it appears even if I backspace over all the characters that I typed. If the application does a startActivity and starts another activity, when it comes back to the page, the list is visible. Whenever the onCreate() (starting the ListActivity instead of returning to it) the list does not display. If the ListActivity is pushed to the background so another Activity can run, the list displays when control comes back to the ListActivity. I do not know if this is a bug in my code or in the SDK.

    ** I had a difficult time getting my ListView to position itself on the screen with the other views (a TextView and some Buttons). I switched from a LinearLayout to a RelativeLayout and it worked much better.

    Please let me know if any of this information helps.

     

Share This Page

Loading...