Reputation: 43
So I'm trying to create a custom ListView with a custom ArrayAdapter. I got a fragment that loads the ListView, creating and setting my own ArrayAdapter called CardsAdapter.
Problem? The list is not showing up. Only the default text (android:id="@android:id/empty"), that is set to show if the list is empty, shows up.
For matters of testing this method cards.createDummy();
fills up the list with 3 dummy objects to be shown.
Fragment where I declare and create the listview
public class MainFragment extends Fragment implements Callback{
private ListView listView;
private CardsAdapter cardsAdapter;
private CardList cards; //contains the array to be shown
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cards = new CardList(getActivity().getApplicationContext());
cards.createDummy(); //Fills up dummy list
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) view.findViewById(R.id.list);
//Creates the adapter with the dummy list cards.cardList
cardsAdapter = new CardsAdapter(getActivity().getApplicationContext(),cards.cardList);
listView.setAdapter(cardsAdapter);
//don't bother with this
HttpHandle handle = new HttpHandle( listView, this);
handle.download("http://someapp.herokuapp.com/");
// Inflate the layout for this fragment
return view;
}
The layout file of the list view (correspondent to R.layout.fragment_main
)
I've got the list, and a textview to be shown if the list is empty:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6" >
<ListView
android:id="@+id/list"
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" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:text="Retrieving data"/>
</RelativeLayout>
My custom ArrayAdapter:
public class CardsAdapter extends ArrayAdapter<Card> {
public LinearLayout cardView;
public CardsAdapter(Context context, ArrayList cards) {
super(context, R.layout.card, cards);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.card, parent, false);
}
Card card = getItem(position);
this.cardView = (LinearLayout) rowView.findViewById(R.id.tableRow1);
TextView title = (TextView) rowView.findViewById(R.id.title);
title.setText(card.getTitle());
return rowView;
}
}
The card layout used by the CardsAdapter (referenced on the constructor as R.layout.card
)
file: res/layout/card.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginBottom="7dp"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:adjustViewBounds="True"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:paddingLeft="7dp"
android:src="@drawable/photo" />
<TextView
android:id="@+id/title"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingLeft="3dp"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/title"/>
<TextView
android:id="@+id/dateStart"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingLeft="3dp"
android:maxLines="2"
android:ellipsize="end"/>
</TableRow>
</LinearLayout>
Note: The dummy list is created and is not empty: I checked it with the debug tools The app runs, and does not crash. But it only shows the empty TextView
Upvotes: 1
Views: 1983
Reputation: 3111
You didn't toggle the visibility of the TextView to be invisible if the ListView is empty, try to remove the TextView and run the application, the ListView should appear.
Using Empty TextView you should either make your activity extends ListViewActivity or you have to set the empty TextView for ListView in Java using listView.setEmptyView(youTextView);
to handle the toggle of visibility.
Upvotes: 1