В 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); // ListView
datesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = datesList.getItemAtPosition(position); // ERROR!!!
}
});
адаптер
public class DatesAdapter extends ArrayAdapter<DateTime> {
private LayoutInflater inflater;
private int layout;
private ArrayList<DateTime> datesList;
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) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(this.layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.date.setText(datesList.get(position).toString("dd.MM.yyyy"));
return convertView;
}
@Override
public int getCount() {
return datesList.size();
}
private class ViewHolder {
final TextView date;
ViewHolder(View view){
date = (TextView)view.findViewById(R.id.date);
}
}
}