简体   繁体   English

无法在ArrayAdapter中使用自定义布局

[英]Not able to use custom layout in ArrayAdapter

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. 我是android的新手,我想使用我的自定义布局文件而不是simple_list_item_1,但它不允许这样做。 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. 我想在列表中的每个项目中添加一个背景图像(在layout-> custom_view.xml中定义)或者帮我设置整个页面的背景,在下面的代码中我应该在哪里使用setContentView方法。 Also let me know if any changes are to be made in the below .xml file. 如果要在下面的.xml文件中进行任何更改,请告诉我。

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, custom_view.xml文件如下,

<?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>

只需使用它

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);

You don't set the contentview in a ListActivity . 您没有在ListActivity中设置ListActivity The ListActivity already does that for you. ListActivity已经为您ListActivity了这一点。 Use an Activity , if you want to customize your layout. 如果要自定义布局,请使用“ Activity

You need to call super.onCreate() before setContentView() . 你需要在setContentView()之前调用super.onCreate() 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 . 如果您要在ListActivity中使用自定义布局,则需要为ListView提供id @android:id/list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM