Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating ListView programmatically

Android beginner here. I was playing around with ListViews, trying to create them dynamically instead of the XML file. I observe the following odd behavior in my code.

public class SettingsHolder extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ListView lv = new ListView(this);
    String[] values = new String[10];
    for(int i=0;i<10;i++){
        values[i] = ""+i;
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            //Toast.makeText(getBaseContext(), ""+arg2,     Toast.LENGTH_SHORT).show();
            Log.d("DEBUG", ""+arg2);

        }

    });

    ll.addView(lv);
    setContentView(ll);

}


}

Basically I first create a LinearLayout object and then make a ListView object as one of its child. I observed that the list items so created are not clickable. But if I write

setContentView(lv);

instead of

setContentView(ll);

the list items are clickable. Can anyone please explain this? How do I make the list items clickable if I have to implement my class the latter way? I don't want to go the ListActivity way.

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
like image 711
VJune Avatar asked Jun 12 '12 01:06

VJune


People also ask

How to create an ArrayAdapter?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

What is ArrayAdapter in android?

android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


2 Answers

change your code:

ll.addView(lv);

to this:

ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

Problem solved, I guess, you have to give the listview some kind of layout restriction or definition, so it can work properly.

like image 103
Ruobin Wang Avatar answered Oct 04 '22 08:10

Ruobin Wang


I was able to get this to work, but I had to create the LinearLayout in XML, which I realize is different than how you have it. However, given that either way the LinearLayout is the root element, and given that you are still creating the ListView programmatically, it really should make no difference IMHO.

public class ExampleActivity extends Activity implements OnItemClickListener {

private LinearLayout ll;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    ll = (LinearLayout) findViewById(R.id.main_ll);
    ListView lv = new ListView(this);
    ll.addView(lv);

    String[] values = new String[10];
    for (int i = 0; i < 10; i++) {
        values[i] = "" + i;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Toast.makeText(this, "" + arg2, Toast.LENGTH_SHORT).show();
    Log.d("DEBUG", "" + arg2);
}

}

like image 33
Swifty McSwifterton Avatar answered Oct 04 '22 06:10

Swifty McSwifterton