Last active
March 16, 2016 13:05
-
-
Save SelvinPL/96d1cce71ba3bfaf5ab5 to your computer and use it in GitHub Desktop.
Extending ArrayAdapter hints
This file contains hidden or 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 class MyPojo { | |
| private long id; | |
| private String name; | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public long getId() { | |
| return id; | |
| } | |
| public void setId(long id) { | |
| this.id = id; | |
| } | |
| } |
This file contains hidden or 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 class MyPojoArrayAdapter extends ArrayAdapter<MyPojo> { | |
| public MyPojoArrayAdapter(Context context, List<MyPojo> objects) { | |
| super(context, android.R.layout.simple_list_item_2, android.R.id.text1, objects); | |
| //we know what resourceid so we set it "by hand" here | |
| //we set android.R.id.text1 as text field for not getting ClassCastException in super.getView(...) | |
| //we have methods like add, remove, getItem .. so, do not store object by yourself in ArrayAdapter derived class!!!!!!!! | |
| } | |
| //begin optionally as we have stable ids | |
| @Override | |
| public boolean hasStableIds() { | |
| return true; | |
| } | |
| @Override | |
| public long getItemId(int position) { | |
| return getItem(position).getId(); | |
| } | |
| //end optionally | |
| @Override | |
| public View getView(int position, View convertView, ViewGroup parent) { | |
| final ViewHolder holder; | |
| if(convertView == null) { | |
| holder = new ViewHolder(super.getView(position, null, parent)); | |
| } else { | |
| holder = (ViewHolder) convertView.getTag(); | |
| } | |
| holder.bindItem(getItem(position)); | |
| return holder.parent; | |
| } | |
| static class ViewHolder { | |
| public final TextView textViewName; | |
| public final TextView textViewId; | |
| public final View parent; | |
| //in general we make changes only in this class based on MyPojo class | |
| //you know more widgets etc. | |
| //example customization | |
| //public final MyCustomView yetAnotherViewForExample; | |
| public ViewHolder(View view){ | |
| view.setTag(this); | |
| textViewName = (TextView) view.findViewById(android.R.id.text1); | |
| textViewId = (TextView) view.findViewById(android.R.id.text2); | |
| //example customization | |
| //yetAnotherViewForExample = (MyCustomView)view.findViewById(R.id/yet_another_view_for_example); | |
| parent = view; | |
| } | |
| public void bindItem(MyPojo item) { | |
| textViewName.setText(item.getName()); | |
| textViewId.setText(Long.toString(item.getId())); | |
| //example customization | |
| //like: | |
| //yetAnotherViewForExample.setFoo(item.getBar()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment