Ten_Ten_Steve
Ten_Ten_Steve

Reputation: 431

Android BaseAdapter Dealing with an Empty ArrayList

I'm trying to populate a ListView with an ArrayList> using a base adapter. The ArrayList is populated by a database, and it is possible for the database to have no entries, and thus a empty ArrayList, for example when the app is launched for the first time. While the ArrayList is empty, I receive a non de-script "java.lang.RuntimeException: Unable to start activity ComponentInfo ... java.lang.NullPointerException".

My onCreate method looks like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.samples_list_layout);
    mContext = getApplicationContext();
    lv = getListView();
    list = myDatabase.getInstance(mContext).getAllSamples();
    sa = new SamplesAdapter(this, list);
    lv.setAdapter(sa);
    registerForContextMenu(lv);
}

Setting lv.setAdapter(null) will get the app to display the empty list view I have set up. However, when I leave it up to the BaseAdapter, I get the error. I've followed the 2 Android List8.java and List14.java examples, and either way give the same results. My BaseAdapter class looks like this:

public class SamplesAdapter extends BaseAdapter {

private static final String SAMPLE_NAME_COL = "name";

private static final String SAMPLE_HOST_COL = "host";

private static final String SAMPLE_ICON_COL = "icon";

private static final String SAMPLE_MODIFIED_STAMP_COL = "moddate";

private Context mContext;
private ArrayList<HashMap<String, String>> samples = new ArrayList<HashMap<String, String>>();

public SamplesAdapter(Context c, ArrayList<HashMap<String, String>> list){
    mContext = c;
    samples = list;
}

public int getCount() {
    return samples.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.samples_row_layout, parent, false);
    TextView name = (TextView) v.findViewById(R.id.samples_name);
    name.setText(samples.get(position).get(SAMPLE_NAME_COL));
    TextView host = (TextView) v.findViewById(R.id.samples_host);
    host.setText(samples.get(position).get(SAMPLE_HOST_COL));
    TextView moddate = (TextView) v.findViewById(R.id.samples_mod_stamp);
    moddate.setText(samples.get(position).get(SAMPLE_MODIFIED_STAMP_COL));
    return v;
}

}

I should also note that the ListView properly displays items when there is something to show. It only fails when the ArrayList is empty. Also, I'm using Android 2.2 (Not the greatest, I know). Any help would be greatly appreciated.

Upvotes: 2

Views: 2859

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

let getCount return 0, in orded to avoid the NPE:

public int getCount() {
    return (samples == null) ? 0 : samples.size();
}

Upvotes: 6

Related Questions