Android binding strategy if adapter does work or view?

I'm not sure if this is something that depends on the opinions of the developers, but I am creating an Android app and have a Listview view that displays a custom view. My layout basically consists of a view (in the form of an xml resource file and a class obtained from the view that is the root), a domain model object (the data is attached to the view) and an adapter that comes from BaseAdapter that binds the data from the domain model to the view in getViewMethod as follows:

Model: SampleItem
User view: SampleView
Adapter: (code below)

   public View getView(int position, View convertView, ViewGroup parent) {
  SampleView sampleView;

  SampleItem item = (SampleItem)getItem(position);

  if(convertView == null) {
   sampleView = new SampleView(_context);
  }
  else {
   sampleView = (SampleView)convertView;
  }

  sampleView.setTitle(item.getTitle());
  sampleView.setContentText(item.getContent());
  sampleView.setItemRowNumer(item.getRowNumber());
  ... //etc
  return sampleView;
}

This is how I always saw it, but I think this is the right way to do it.

, , , , , , , , .

, , , . , , :

   public View getView(int position, View convertView, ViewGroup parent) {
  SampleView sampleView;

  SampleItem item = (SampleItem)getItem(position);

  if(convertView == null) {
   sampleView = new SampleView(_context, item);
  }
  else {
   sampleView = (SampleView)convertView;
  }

  return sampleView;
 }

, , , .

+3
1

. .

1) CursorAdapter#bindView: . , , Android . , , .

2). AdapterView android - , View, . , / .. . . , AdapterView View , . : Adapter View. View, . , .

, View , -. , . , , . , , View, , . , .. toString() . , , underling, .

+3

Source: https://habr.com/ru/post/1786294/


All Articles