0

I'm new to android, I want to use my custom layout file instead of simple_list_item_1 but its not allowing to do so. I want to add an backgroud image(which is defined in layout->custom_view.xml) to each items in the list OR help me to set the backgroud for the whole page, In the below code where should I use the setContentView Method. Also let me know if any changes are to be made in the below .xml file.

public class Planet extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        //setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
    String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
                        setListAdapter(adapter);
                        ListView lv = getListView();
                        lv.setTextFilterEnabled(true);
                        lv.setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                                if(position == 0)
                                {
                                    Intent myIntent = new Intent(Planet.this, Galaxy.class);
                                    startActivityForResult(myIntent, 0);

                            }

                }
                });
                }

custom_view.xml file is as follows,

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"> 

         <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />     
    </LinearLayout>

3 Answers 3

2

Simply use this

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);
Sign up to request clarification or add additional context in comments.

Comments

0

You don't set the contentview in a ListActivity. The ListActivity already does that for you. Use an Activity, if you want to customize your layout.

Comments

0

You need to call super.onCreate() before setContentView(). And if you are going to use a custom layout in a ListActivity, you need to give the ListView the id @android:id/list.

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.