mike yaworski mike yaworski - 7 months ago 34
Java Question

ListView Elements Not Displaying

ListView elements are not displaying even though I've used almost identical code before. Sorry that I have to ask this type of question, but I seriously just don't understand why it's not working. None of the other similar questions I looked at were useful.

The heights of the items and ListView should be fine. Not sure what's going on. Thanks guys.

Here's the code dump:

Main Acitivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_city);
findViewById(R.id.loadingPanel).setVisibility(View.GONE); // remove loading animation

ArrayList<String> citiesList = new ArrayList<>();
citiesList.add("Waterloo");
citiesList.add("Guelph");
populateCities(citiesList);
}

public void populateCities(ArrayList<String> citiesList) {

// see what the list is
Toast.makeText(getApplicationContext(),citiesList.toString(),Toast.LENGTH_LONG).show();

// populate the ListView
CitiesListAdapter citiesListAdapter = new CitiesListAdapter(this, citiesList);
ListView citiesListView = (ListView)findViewById(R.id.cities_list);
citiesListView.setAdapter(citiesListAdapter);

// listen for when a city in the list is clicked
citiesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// go to the bar list activity (send the city and province with it)
TextView lblCity = (TextView)view.findViewById(R.id.lblCity);
String city = lblCity.getText().toString();
Toast.makeText(getApplicationContext(), city, Toast.LENGTH_LONG).show();
}
});
}


Here's the list adapter code (
CitiesListAdapter
):

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class CitiesListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final ArrayList<String> cities;

public CitiesListAdapter(Activity context, ArrayList<String> cities) {
super(context, R.layout.cityitem);
this.context = context;
this.cities = cities;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.cityitem, null, true);

TextView lblCity = (TextView)rowView.findViewById(R.id.lblCity);
lblCity.setText(this.cities.get(position));

return rowView;
}
}


Then here's the main layout:

<?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:fitsSystemWindows="true"
tools:context="com.myapp.SelectCity">

<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:id="@+id/progressBar" />
</RelativeLayout>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBack"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:onClick="back"
android:src="@mipmap/ic_menu_back"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnRefresh"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:onClick="refresh"
android:src="@mipmap/ic_action_refresh"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Select City"
android:id="@+id/lblTitle"
android:layout_below="@+id/btnBack"
android:layout_centerHorizontal="true" />


<RelativeLayout
android:id="@+id/listViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/lblTitle"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp">

<ListView
android:background="#F00"
android:id="@+id/cities_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true" />

</RelativeLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/listViewLayout"
android:layout_centerHorizontal="true" />

</RelativeLayout>


Then the ListView item layout (
cityitem.xml
):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="City"
android:id="@+id/lblCity" />
</LinearLayout>


Edit: Looks like I'm also getting this stupid error that Android can't seem to fix (just sneaks in the LogCat):

E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7ccc40


Not sure if that's why nothing is displaying.

Screenshot: http://prntscr.com/avpy9s

Answer

The super call in your constructor doesn't include the ArrayList, so your Adapter is returning 0 for the item count, and the ListView won't try to retrieve any Views. You could include the ArrayList in the super call, or override the getCount() method to return cities.size().

However, you don't necessarily need a custom Adapter for a simple ArrayList of Strings. You could simply use the constructor for ArrayAdapter that takes a layout and a TextView resource ID.

ArrayAdapter<String> citiesListAdapter = new ArrayAdapter<>(this,
                                                            R.layout.cityitem,
                                                            R.id.lblCity,
                                                            cities);
Comments