Skip to content

Instantly share code, notes, and snippets.

@HybridEidolon
Last active December 17, 2015 19:19
Show Gist options
  • Save HybridEidolon/5659211 to your computer and use it in GitHub Desktop.
Save HybridEidolon/5659211 to your computer and use it in GitHub Desktop.
list adapter example
package com.furyhunter.gw2assistant;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
public class GW2World implements Parcelable {
public GW2World() {
}
public GW2World(int id, String name) {
this.name = name;
this.id = id;
}
public GW2World(JSONObject obj) {
try {
name = obj.getString("name");
id = obj.getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
public int id;
public String name;
@Override
public String toString() {
return name + " [" + id + "]";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(name);
}
public static final Parcelable.Creator<GW2World> CREATOR
= new Parcelable.Creator<GW2World>() {
@Override
public GW2World createFromParcel(Parcel source) {
return new GW2World(source.readInt(), source.readString());
}
@Override
public GW2World[] newArray(int size) {
return new GW2World[size];
}
};
public void insert(SQLiteDatabase db) {
ContentValues cv = new ContentValues();
cv.put("id", id);
cv.put("name", name);
db.insert("worlds", null, cv);
}
}
package com.furyhunter.gw2assistant;
import java.util.ArrayList;
import java.util.Collection;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class GW2WorldAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
private ArrayList<GW2World> worlds;
private Context context;
public GW2WorldAdapter(Context context, Collection<GW2World> worlds) {
this.context = context;
this.worlds = new ArrayList<GW2World>(worlds);
}
@Override
public int getCount() {
return worlds.size();
}
@Override
public Object getItem(int position) {
return worlds.get(position);
}
@Override
public long getItemId(int position) {
return worlds.get(position).id;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = View.inflate(context, android.R.layout.simple_list_item_1, null);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(worlds.get(position).name);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment