2013-09-28 42 views
0

我是新來的android,我想使用自定義佈局文件而不是simple_list_item_1,但它不允許這樣做。我想添加一個背景圖像(在layout-> custom_view.xml中定義)到列表中的每個項目,或者幫助我設置整個頁面的背景,在下面的代碼中,我應該使用setContentView方法。另外讓我知道是否要在下面的.xml文件中進行更改。無法在ArrayAdapter中使用自定義佈局

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

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

回答

0

您沒有設置內容查看在ListActivityListActivity已經爲你做了。如果您想自定義佈局,請使用Activity

0

您需要在setContentView()之前致電super.onCreate()。如果您要在ListActivity中使用自定義佈局,則需要爲ListView指定編號@android:id/list

2

只需使用這個

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