ListView没有显示/出现android studio

时间:2016-03-12 16:02:10

标签: java android listview

我有两项活动。 MainActivity.java和HomeActivity.java。我试图在HomeActivity.java中创建一个ListView,只需使用" One"," Two"," Three"和" Four"作为List项目,但ListView根本没有显示。

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 = {HomeActivity@20765}
myItems = {String[4]@20825}
adapter = {ArrayAdapter@20841}
list = {ListView@20853} "android.widget.ListView... etc

2 个答案:

答案 0 :(得分:0)

通过为适配器提供自定义视图,您需要一个额外的步骤来填充该自定义视图如何使用数组适配器数据进行填充。最简单的解决方案就是抛弃自定义视图并使用简单列表:

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

如果要继续为单元格使用自定义视图,则需要创建一个自定义适配器类来覆盖getView(),以告诉listview如何给出字符串值,如何填充视图,本质上它设置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);

答案 1 :(得分: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);

已完成。