Итак, я пытаюсь создать собственный ListView с настраиваемым ArrayAdapter. У меня есть фрагмент, который загружает ListView, создавая и устанавливая мой собственный ArrayAdapter под названием CardsAdapter.ListView с пользовательским ArrayAdapter, не отображающим элементы
Проблема? Список не отображается. Отображается только текст по умолчанию (android: id = "@ android: id/empty"), который установлен для отображения пустого списка.
По вопросам тестирования этот метод cards.createDummy();
заполняет список тремя фиктивными объектами, которые должны быть показаны.
фрагмент, где я объявляю и создать 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;
}
Файл Компоновка в виде списка (корреспондент R.layout.fragment_main
) у меня есть список, и TextView будет отображаться, если список пуст :
<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>
Мой заказ 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;
}
}
Макет карты используется CardsAdapter (ссылка на конструкторе как R.layout.card
) файла: 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>
Примечание: Фиктивный список создается и не пусто: Я проверил это с отладочными инструментами The приложение запускается и не сбой. Но это только показывает пустое TextView
Покажите вам * xml * файл и код, в котором вы проверяете, является ли список emtpy или нет, чтобы переключать видимость ** textView ** –
Объявив идентификатор текста как «android: id =» @ android: id/empty ", андроид знает, чтобы показать этот текст, если список пуст. Это автоматически. Обратите внимание, что id является андроидом по умолчанию «@android: id/empty», а не «@ + id/empty» – Pedro