Skip to content

Instantly share code, notes, and snippets.

@verkaufer
Created July 28, 2013 00:03
Show Gist options
  • Save verkaufer/63d9764dc80c2fa09d10 to your computer and use it in GitHub Desktop.
Save verkaufer/63d9764dc80c2fa09d10 to your computer and use it in GitHub Desktop.
LazyAdapter implementation. Always returned a nullPointer because pokeman (line 72) always remained null for some reason.
package com.dsgunter.randompokemongenerator;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* Created with IntelliJ IDEA.
* User: David
* Date: 7/4/13
* Time: 4:14 PM
* <p/>
* (c) David Gunter 2013 unless otherwise noted
*/
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public HashMap<String, String> pokeman;
//JSON code names
static final String TAG_NAME = "name";
static final String TAG_TYPE1 = "type1";
static final String TAG_TYPE2 = "type2";
static final String TAG_REAL_POKEMON_ID = "real_pokemon_id";
static final String TAG_SPRITE_URL = "sprite_url";
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount(){
return data.size();
}
public Object getItem(int position){
return data.get(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(R.layout.list_generated_pokemon, null);
}
TextView name = (TextView)vi.findViewById(R.id.name);
TextView type1 = (TextView)vi.findViewById(R.id.type1);
TextView type2 = (TextView)vi.findViewById(R.id.type2);
TextView id = (TextView)vi.findViewById(R.id.pokeID);
ImageView sprite = (ImageView)vi.findViewById(R.id.pokemonSprite);
pokeman = data.get(position);
//set values in listview
name.setText(pokeman.get(TAG_NAME));
type1.setText(pokeman.get(TAG_TYPE1));
type2.setText(pokeman.get(TAG_TYPE2));
id.setText(pokeman.get(TAG_REAL_POKEMON_ID));
// Load image, decode it to Bitmap and display Bitmap in ImageView
imageLoader.displayImage(pokeman.get(TAG_SPRITE_URL), sprite);
return vi;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment