Skip to content

Instantly share code, notes, and snippets.

@ardok
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardok/c46617a0f4f116f65438 to your computer and use it in GitHub Desktop.
Save ardok/c46617a0f4f116f65438 to your computer and use it in GitHub Desktop.
Adapter Sample
// this is adapter
public class UsersAdapter extends ArrayAdapter<RowItem> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// use pattern holder, just Google it and you'll see
if (convertView == null) {
// ...
convertView.setTag(...);
} else {
// ...
contactItemHolder = (...) convertView.getTag();
}
RowItem rowItem = getItem(position); // get your data
// everything you need should be in `rowItem`
...
}
}
public MyActivity extends Activity {
private RowAdapter mAdapter;
public void onCreate() {
mAdapter = new RowAdapter(this);
}
public void onPostCreate() {
// fetch data from the internet or something and just need to call `mAdapter.add(new RowItem(...))`
// as you get more data. Don't forget `notifyDateSetChanged`. I think you can also use `mAdapter.addAll(...)` ?
}
public void addNewRow() {
// the idea is:
// 1) loop through all the data
// 2) in each loop, update the data by actually touching the view to get what you need
// 3) add a new row
// 4) call notify data set changed
int max = mAdapter.getCount();
for (int i = 0; i < max; i++) {
RowItem item = mAdapter.getItem(i);
// use ListView to get the value from each edit text
EditText labelET = (EditText) myListView...;
EditText valueET = (EditText) myListView...;
item.label = labelET.getText().toString();
item.value = valueET.getText().toString();
}
// finally add a new row
mAdapter.add(new RowItem(...));
mAdapter.notifyDateSetChanged();
}
}
public RowItem {
public String label;
public String value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment