2016-03-12 53 views
1

我有兩個活動。 MainActivity.java和HomeActivity.java。我試圖在HomeActivity.java中創建一個ListView作爲List項目的「One」,「Two」,「Three」和「Four」,但ListView根本沒有顯示出來。ListView不顯示/出現android studio

HomeActivity.java:

public class HomeActivity extends AppCompatActivity { 

String personName; 
String email; 
String rfid; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    populateListView(); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     personName = extras.getString("Name"); 
     email = extras.getString("Email"); 
     rfid = extras.getString("RFID"); 
    } 

    String params = (email + "+" + rfid); 

    new MyAsyncTask().execute(new Pair<Context, String>(this, params)); 

    toolbar.findViewById(R.id.ButtonSettings).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      PopupMenu popup = new PopupMenu(HomeActivity.this, findViewById(R.id.ButtonSettings)); 
      popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); 
      popup.getMenu().add(personName); 
      popup.getMenu().add(email); 
      popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
       public boolean onMenuItemClick(MenuItem item) { 
        Toast.makeText(
          HomeActivity.this, "You Clicked" + item.getTitle(), Toast.LENGTH_SHORT).show(); 
        return true; 
       } 
      }); 
      popup.show(); 
     } 
    }); 
} 

private void populateListView(){ 
    String [] myItems = {"One", "Two", "Three", "Four"}; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems); 

    ListView list = (ListView) findViewById(R.id.listView); 
    list.setAdapter(adapter); 
} 
} 

content_home.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="ie.myy3p.ticktask.HomeActivity" 
tools:showIn="@layout/activity_home"> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/profile_layout" 
    android:layout_below="@+id/toolbar" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tView" 
     android:layout_alignBottom="@id/toolbar" 
     android:layout_alignParentEnd="true" /> 

    <ListView android:id = "@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</RelativeLayout> 

</RelativeLayout> 

show_tasks.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

</TextView> 

編輯:

因此,我對代碼進行了一些更改,現在我的ListView顯示在Android Studio的content_home.xml和activity_home.xml中,它以前沒有做過。但是,當我在手機上運行應用程序時,ListView不顯示。我調試了它,並檢查了我的populateList方法,它似乎工作正常。調試在populateList方法的末尾寫道:

this = {[email protected]} 
myItems = {String[4]@20825} 
adapter = {[email protected]} 
list = {[email protected]} "android.widget.ListView... etc 
+0

發佈您的logcat錯誤,如果有的話。 –

回答

0

通過給自定義視圖的適配器,你需要一個額外的步驟來填充自定義視圖如何使用陣列適配器的數據來填充。最簡單的辦法是剛剛溝自定義視圖,並使用簡單的列表:

adapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, 
            myItems); 

如果你想繼續使用自定義視圖爲你的細胞,你需要創建一個覆蓋getView()到自定義適配器類告訴列表視圖如何給你的字符串值,它如何填充視圖,本質上它設置textView上的文本(但這是android.R.layout.simple_list_item_1的默認行爲)。

這裏有一個很好的例子,如果你想保持你的自定義視圖:https://stackoverflow.com/a/15832564/1316346

編輯:

看起來也像你誇大了錯誤的觀點:

setContentView(R.layout.activity_home); 

與資源膨脹稱爲activity_home,從您發佈的內容中應該可能會讀到:

setContentView(R.layout.content_home); 
+0

感謝您的幫助。不幸的是,即使當我將自己的代碼從自定義視圖更改爲簡單列表時,ListView仍然不會顯示 – Aine

+0

@Aine檢查我的編輯,看起來您可能會誇大錯誤的視圖。如果這不起作用,請開始註釋掉一些視圖代碼,這是一個簡單的列表視圖,以確保它不僅僅被其他視圖覆蓋。 –

0

在這裏更改。

private void populateListView(){ 
    String [] myItems = {"One", "Two", "Three", "Four"}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems); 
    ListView list = (ListView) findViewById(R.id.listView); 
    list.setAdapter(adapter); 
} 

向該

private void populateListView(){ 
    String [] myItems = {"One", "Two", "Three", "Four"}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, android.R.id.text1, myItems); 
    ListView list = (ListView) findViewById(R.id.listView); 
    list.setAdapter(adapter); 

}

備選2

變更設定內容查看來自該

setContentView(R.layout.activity_home); 

對此

setContentView(R.layout.content_home); 

它已完成。