/*
* Copyright 2014 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.examples.realmgridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.Locale;
// This adapter is strictly to interface with the GridView and doesn't
// particular show much interesting Realm functionality.
// Alternatively from this example,
// a developer could update the getView() to pull items from the Realm.
public class CityAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<City> cities = null;
public CityAdapter(Context context) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(List<City> details) {
this.cities = details;
}
@Override
public int getCount() {
if (cities == null) {
return 0;
}
return cities.size();
}
@Override
public Object getItem(int position) {
if (cities == null || cities.get(position) == null) {
return null;
}
return cities.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View currentView, ViewGroup parent) {
if (currentView == null) {
currentView = inflater.inflate(R.layout.city_listitem, parent, false);
}
City city = cities.get(position);
if (city != null) {
((TextView) currentView.findViewById(R.id.name)).setText(city.getName());
((TextView) currentView.findViewById(R.id.votes)).setText(String.format(Locale.US, "%d",city.getVotes()));
}
return currentView;
}
}