Я очень новичок в разработке Android. Я пытаюсь создать приложение. Я использую ArrayAdapter для просмотра списка, но метод ArrayAdapter getView() не вызывается.
Я поместил отладчик в конструктор ArrayAdapter и проверил, я получаю размер списка 16, но метод getView() не вызывается.
Вот мой код для ArrayAdapter.
public class Adapter extends ArrayAdapter<Item> {
private List<Item> list;
private Context context;
private LayoutInflater inflater;
public Adapter(Context context, int resource, List<Item> list) {
super(context, resource, list);
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.section_list, null);
holder = new ViewHolder();
holder.text = convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Item item = getItem(position);
holder.text.setText(item.getTitle());
return convertView;
}
@Override
public int getCount() {
if (this.list != null && this.list.size() > 0) {
return this.list.size();
} else {
return 0;
}
}
private class ViewHolder {
private TextView text;
}
}
Как я вызываю адаптер:
adapter = new Adapter(getContext(), R.layout.section_list, listItem);
notificationListView.setAdapter(adapter);
Вот мой макет:
<?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">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:text="MAXOFEN is PEACHES"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_height="wrap_content" />
</LinearLayout>
Вот мой элемент списка во фрагменте:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shadow"
android:orientation="vertical"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/notifications"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@color/colorAccent"
android:padding="8dp"
android:text="@string/see_all"
android:textColor="@color/white" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/hrLine" />
<LinearLayout
android:id="@+id/userNameLl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/danger_alert_copy" />
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="harsh" />
</LinearLayout>
<com.knowyourmeds.utils.ExpandableHeightListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp" />
</LinearLayout>
</LinearLayout>
Как я генерирую список:
Item item;
List<Item> listItem = new ArrayList<>();
item = new Item(1l,null,"body","title");
listItem.add(item);
item = new Item(2l,null,"body 1","title 1");
listItem.add(item);
item = new Item(3l,null,"body 2","title 2");
listItem.add(item);
item = new Item(5l,null,"body 3","title 3");
listItem.add(item);
Дайте мне знать, если я что-то упустил.
notificationText
? в ViewHoldertext
- person Tung Tran   schedule 28.03.2018listItem
, который вы используете во фрагменте, а не в макете !! - person Santanu Sur   schedule 28.03.2018itemList
вместоlistItem
, орфографическая ошибка ?? - person Santanu Sur   schedule 28.03.2018getView
не звонят? - person Abu Yousuf   schedule 28.03.2018