2

I found this example to create a custom listview:

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

        }

}

However, they are using a xml layout for each row. Is this good practice? What if I wanted to create my own layout programmatically and utilize it in the adapter?

I guess it would be easier just to use the xml layout, but it would be nice to know

2 Answers 2

1

change adapter like this

setListAdapter(new ArrayAdapter(this, 
                    android.R.layout.simple_expandable_list_item_1, statesList));
Sign up to request clarification or add additional context in comments.

1 Comment

well this: R.layout.list_item is pointing to an xml specifically for each row. What if I wanted a method or a new class to handle the layout? How would I target it in my adapter?
0

This is the best practice to use the default android layout, if you do not want your customized view. and if you wanted to use your customized list you can always inflate it from xml and render the data according to list item.

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.