D
D
DarkByte20152017-05-25 10:38:49
Java
DarkByte2015, 2017-05-25 10:38:49

Why is the list not showing up?

I'm trying to make a list. The code is this:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/datesList">

</ListView>

date_item.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"
    android:id="@+id/date" />

adapter
public class DatesAdapter extends ArrayAdapter<DateTime> {
    private LayoutInflater inflater;
    private int layout;
    private ArrayList<DateTime> datesList;

    private TextView date;

    public DatesAdapter(@NonNull Context context, @LayoutRes int resource, ArrayList<DateTime> datesList) {
        super(context, resource);

        this.inflater = LayoutInflater.from(context);
        this.layout = resource;
        this.datesList = datesList;
    }

    @Override
    public @NonNull View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null){
            convertView = inflater.inflate(this.layout, parent, false);
            date = (TextView)convertView.findViewById(R.id.date);
        }

        date.setText(datesList.get(position).toString());

        return convertView;
    }
}

in MainActivity.onCreate
final ArrayList<DateTime> dates = new ArrayList<>();

dates.add(new DateTime(2017, 1, 1, 0, 0));
dates.add(new DateTime(2016, 1, 1, 0, 0));
dates.add(new DateTime(2015, 1, 1, 0, 0));
dates.add(new DateTime(2014, 1, 1, 0, 0));

DatesAdapter adapter = new DatesAdapter(this, R.layout.date_item, dates);

datesList.setAdapter(adapter);

The list does not want to be displayed in any way ... Debazhil - the getView method does not even go even once. Why is this happening??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DarkByte2015, 2017-05-25
@DarkByte2015

It turns out that it was necessary to override the getCount method. I did not know.

D
Dmitry Serkov, 2017-05-25
@Hutaab

Can datesList.notifyDataSetChanged();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question