5

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>
3
  • Your code looks fine, I even compiled to be sure, but the OnItemClickListener executes as promised... Commented Jun 12, 2012 at 1:42
  • What happens if you set the content view and then add the listvieww? Commented Jun 12, 2012 at 2:58
  • Nope, still doesn't work. The list displays fine but the list items are un-clickable. Commented Jun 12, 2012 at 3:35

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

hmm... it works but making the width and the height of the child View equal to -1 is a bit odd. Don't you think?
Why odd? -1 means View.FILL_PARENT, I think it is reasonable. Sorry, maybe I didn't make it clear, you can write like this: ll.addView(lv, View.FILL_PARENT, View.FILL_PARENT);
Small correction, I think it should be LayoutParams.FILL_PARENT instead of View.FILL_PARENT.
yep, you're right, I have corrected my answer, to avoid some misleading.
2

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);
}

}

1 Comment

My question was intended more towards understanding the behavior of my program. I guessed alternate implementations would definitely exist. But thanks for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.