1

I have two activities. MainActivity.java and HomeActivity.java. I am trying to create a ListView in HomeActivity.java just with "One", "Two", "Three", and "Four" as the List items but the ListView is not showing up at all.

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>

EDIT:

So I made a few changes to my code and now my ListView is showing up in content_home.xml and activity_home.xml in Android Studio, which it wasn't doing before. However, when I run the app on my phone the ListView doesn't show up. I've debugged it and checked my populateList method and it seems to be working fine. Debug at the end of populateList method reads:

this = {HomeActivity@20765}
myItems = {String[4]@20825}
adapter = {ArrayAdapter@20841}
list = {ListView@20853} "android.widget.ListView... etc
Aine
  • 195
  • 1
  • 2
  • 10

2 Answers2

0

By giving a custom view to the adapter, you need an extra step to populate how that custom view uses the array adapter data to populate. The easiest solution is just ditch the custom view and use the simple list:

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

If you want to continue to use a custom view for your cells, you'd need to create a custom adapter class that overrides getView() to tell the listview how given your string value, how it populates the view, essentially it sets the text on your textView (however this is the default behavior of android.R.layout.simple_list_item_1).

Here's a great example if you want to keep your custom view: https://stackoverflow.com/a/15832564/1316346

EDIT:

Also looks like you are inflating the wrong view:

setContentView(R.layout.activity_home);

is inflating with a resource called activity_home, from what you've posted it should probably read:

setContentView(R.layout.content_home);
Community
  • 1
  • 1
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • Thanks for your help. Unfortunately, even when I changed my code from the custom view to simple list, the ListView is still not showing – Aine Mar 12 '16 at 16:26
  • @Aine check my edit, looks like you may be inflating the wrong view. If that doesn't work start commenting out some of your view code so it's a simple list view to ensure it's not just being covered up by other views. – Kevin DiTraglia Mar 12 '16 at 16:30
0

Change here.

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);
}

To this

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);

}

Alternative 2

Change set content view from this

setContentView(R.layout.activity_home);

To this

setContentView(R.layout.content_home);

It's Done.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58