Alex Rivera | Logout

Create ListView programmatically

Asked 2010-09-15T14:20:19.163
15

Hi I am new in Android. Could anyone tell me pls whats the wrong with the following code:

public class ListApp extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView lText = new TextView(this);
        lText.setId(0);       

        ListView lView = new ListView(this);
        String[] lStr = new String[]{"AA","BB", "CC"};
        ArrayAdapter lAdap = new ArrayAdapter(this,lText.getId(),lStr);
        lView.setAdapter(lAdap);
        lView.setFocusableInTouchMode(true);        

        setContentView(lView);
    }
}
Edit
Report

1 Answer

1

Best practice is to separate view (xml layout) and controller (Activity).

If you dont want this, try to put

 setContentView(lView);

at the beginning of onCreate

answered 2011-05-20T11:56:44.050

Your Answer