Last active
March 30, 2016 13:14
-
-
Save Artur-Sulej/e0e90e7d5a0167db9d7b to your computer and use it in GitHub Desktop.
Generic adapter for feeding ListViews with custom view loaded with data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ListViewAdapter <T> extends BaseAdapter { | |
protected ListView listView; | |
private ArrayList<T> objects = new ArrayList<T>(); | |
public void updateData(ArrayList<T> objects) { | |
this.objects = objects; | |
notifyDataSetChanged(); | |
} | |
public void appendData(ArrayList<T> objectsPortion) { | |
this.objects.addAll(objectsPortion); | |
notifyDataSetChanged(); | |
} | |
public ArrayList<T> getData() { | |
return objects; | |
} | |
@Override | |
public int getCount() { | |
return objects.size(); | |
} | |
@Override | |
public T getItem(int position) { | |
return objects.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (listView == null) { | |
listView = (ListView) parent; | |
} | |
return provideView(convertView, objects.get(position)); | |
} | |
protected abstract View createView(Context context); | |
protected void loadData(LoadableView<T> loadableView, T object) { | |
loadableView.loadData(object); | |
} | |
private View provideView(View convertView, T object) { | |
if (convertView == null) { | |
convertView = createView(listView.getContext()); | |
} | |
@SuppressWarnings("unchecked") | |
LoadableView<T> loadableView = ((LoadableView<T>) convertView); | |
loadData(loadableView, object); | |
return (View) loadableView; | |
} | |
protected View inflateItemView(int layoutId){ | |
LayoutInflater inflater = LayoutInflater.from(listView.getContext()); | |
return inflater.inflate(layoutId, listView, false); | |
} | |
public interface LoadableView <T> { | |
void loadData(T object); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*ContactsListViewAdapter.java - all you need to do is implement factory method that returns your custom view*/ | |
public class ContactsListViewAdapter extends ListViewAdapter<Contact> { | |
@Override | |
protected View createView(Context context) { | |
return new ContactView(context /*plus any arguments your custom view needs*/); | |
} | |
} | |
/*ContactView.java - your custom view must implement LoadableView interface with a method allowing to load data into its subviews*/ | |
public class ContactView extends /*Any*/View implements ListViewAdapter.LoadableView<Contact> { | |
private TextView username; | |
/*(...)*/ | |
@Override | |
public void loadData(Contact contact) { | |
username.setText(contact.getNickname()); | |
} | |
} | |
/*SomeActivity.java*/ | |
public class MainActivity extends Activity { | |
private ContactsListViewAdapter contactsListViewAdapter; | |
private ListView contactsListView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
contactsListView = (ListView) findViewById(R.id.contacts_list_view); | |
contactsListViewAdapter = new ContactsListViewAdapter(); | |
contactsListView.setAdapter(contactsListViewAdapter); | |
} | |
/*New data arrives along with request?*/ | |
public void loadContacts(ArrayList<Contact> contacts) { | |
/*Just use:*/ | |
contactsListViewAdapter.updateData(contacts); | |
} | |
} | |
/*Contact.java - domain class*/ | |
public class Contact implements Serializable { | |
String nickname; | |
public String getNickname() { | |
return nickname != null ? nickname : ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment