Skip to content

Instantly share code, notes, and snippets.

@mcatta
Created May 10, 2013 15:40
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 mcatta/5555248 to your computer and use it in GitHub Desktop.
Save mcatta/5555248 to your computer and use it in GitHub Desktop.
Simple android adapters
String[] items = { "this", "is", "a", "really" };
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, items));
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<String> items;
public CustomAdapter(Activity activity, ArrayList<String> items) {
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView tv = (TextView) vi.findViewById(android.R.id.text1);
tv.setText(items.get(position).intern());
return vi;
}
}
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater inflater;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText(cursor.getString(cursor.getColumnIndex("column")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = null;
if(parent!=null)
view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
return view;
}
}
String[] from = new String[] { "column1", "column2" };
int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this ,android.R.layout.simple_list_item_2, cursor, from, to);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment