2016-03-08 83 views
0

當我嘗試設置listAdapter或適配器(#setListAdapter(), #getListView().setAdapter())它不顯示。當我嘗試單獨設置ListView時,它工作得很好。當我在#runOnUiThread()中運行它時,它不起作用。爲什麼我的列表不顯示?ListActivity - 列表不顯示

下面是我的代碼

public class ShopActivity extends ListActivity { 

    private Intent mainIntent = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.shop); 
     mainIntent = new Intent(this, MainActivity.class); 

     String[] items = {"Test"}; 
     ArrayAdapter adapter = new ArrayAdapter<>(this, 
     android.R.layout.simple_list_item_1, items); 

     //ListView view = (ListView) findViewById(R.id.list); 

     //view.setAdapter(adapter); 
     getListView().setAdapter(adapter); 
    } 

    public void onBack(View view) { 
     startActivity(mainIntent); 
    } 

} 

編輯:

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="1"> 

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="450dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_centerHorizontal="true" 
     android:textAllCaps="false" 
     android:text="@string/button_back" 
     android:onClick="onBack" 
     android:id="@+id/back" 
/> 

<TextView 
     android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="EMPTY" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="-500dp"/> 


<ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.11"/> 

</LinearLayout> 
+0

顯示您的佈局 –

+0

可以請你列出的XML列表佈局。 – Archana

回答

1

將您的linearlayout高度設置爲match_parent,並擺脫重量,因爲它用於比率,看起來不像您正確使用它。加上-500的餘量並不是正確的做法。更換你的LinearLayout先從以下內容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

如果你希望你的TextView是在頂部的按鈕前加入,然後加保證金的按鈕來獲得保證金文本視圖。如果你想要在textview旁邊的後退按鈕,將它們添加到水平方向的線性佈局,並將它們並排放置在頂部。

0

您可能需要使用

this.setAdapter(adapter); 

代替。

+0

該方法不存在ListActivity –

0

確保佈局R.layout.shop有一個ListView元素,其ID爲@android:id/list。見ListActivity

0

如果您使用的是ListActivity,在你的XML,列表視圖應該像這樣與android:id="@android:id/list"

<ListView android:id="@android:id/list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#00FF00" 
        android:layout_weight="1" 
        android:drawSelectorOnTop="false"/> 
+0

它的ID爲「@android:id/list」(添加了xml) –

0

的問題是,由於某種原因的LinearLayout位於低在屏幕上,所以我不能看見。我將android:layout_marginTop="-500dp"添加到ListView並顯示正確:)

+0

請看我建議的答案,它會幫助你更好的佈局設計。 –