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);
    }
}
2
  • for one thing, you're using the ArrayAdapter constructor incorrectly. you want to use the two parameter version: (Context context, int textViewResourceId) Commented May 27, 2011 at 19:35
  • like this: ArrayAdapter<String> lAdap = new ArrayAdapter<String>(this, lText.getId()); Commented May 27, 2011 at 19:38

5 Answers 5

23

here's a solution that does not require you to write any xml layouts. it uses standard android layouts where possible and no inflation is necessary:

Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
dialog = builder.create();
Sign up to request clarification or add additional context in comments.

3 Comments

ignore the dialog stuff. you can adapt this to work with a ListActivity as well.
I copied your code exactly but no listview is generated.
sorry i got it now, your code is missing dialog.show();
4

Try this..

Paste the following code in 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" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/border_cell">
</TextView>

Here is the activity class....

    public class UsersListActivity extends ListActivity{    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);         
            String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
            setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                    statesList)); 
            ListView lv = getListView(); 

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


                     Toast.makeText(getApplicationContext(),
                     "You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
     }
            });

        }

}

Comments

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

Comments

0
public class ListApp extends ListActivity

2 Comments

Could I add ListView form Activity. I don't want to inherit from ListActiviy. Is it possible?
I have the same issue, porting a project from iPhone and Blackberry and using Android's XML is a redesign... argh. If it's easy to procedurally create these things it would make life soooo much easier and looks like it should be possible but hey I'm still new to Android :)
0

Make a layout XML file with your listview and use findViewById to set it's adapter after first setting the content view to your layout

Comments

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.