0

I have a list of a POJO in an Android app and I am currently displaying one of the fields in a listview/listitem like so:

List<NotificationItem> notifItems;
// snip, populate

ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.notification_item, notifItems);

ListView listView = (ListView) findViewById(R.id.notification_listview);
listView.setAdapter(adapter);

And it is my understanding that the listivew or adapter uses the toString of the POJO, which is

public String toString() {
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    return _notificationTitle + " | " + dateFormatter.format(_notificationReceivedDate);
}

R.id.notificationitem is

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"></TextView>

So far so good, but what I want to do is add elements to the notificationitem layout file and then update the Java code to populate the new fields.

How is this done? I don't really understand how the adapter knows/puts the toString value into the one field in the notificationitem.

2
  • 2
    You need to create a custom adapter. Commented Apr 5, 2017 at 14:12
  • you need some custom adapter like: class Adapter extends MatchableArrayAdapter<POJO> { ... and override its onBind() method, extra bonus: override matches() method if you want easy item filtering Commented Apr 5, 2017 at 14:15

1 Answer 1

1
public class NotificationItem {
    //your fields here
}
//-----------------------
public class NotifAdapter extends BaseAdapter {

    Context context;
    List<NotificationItem> data;

    public NotifAdapter(Context context, List<NotificationItem> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.your_row_layout,parent, null);
        TextView tx = convertView.findViewById(R.id.your_widget_inside_row_layout);
        tx.setText(data.yourDataPropery);
        return convertView;
    }
}
// In your activity
NotifAdapter adapter = new NotifAdapter(this, ArrayList<NotificationItem>);
listView.setAdapter(adapter);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.