Refactored to move out nearby adapter from the activity

This commit is contained in:
maskara 2017-04-02 15:24:29 +05:30
parent a5fe5ff5a6
commit c2ccd95089
4 changed files with 87 additions and 47 deletions

View file

@ -0,0 +1,7 @@
package fr.free.nrw.commons;
import android.content.Context;
public interface ViewHolder<T> {
void bindModel(Context context, T model);
}

View file

@ -0,0 +1,45 @@
package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.List;
import fr.free.nrw.commons.R;
public class NearbyAdapter extends ArrayAdapter<Place> {
public List<Place> placesList;
private Context mContext;
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place, places);
this.mContext = context;
placesList = places;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Place place = getItem(position);
Log.d("NearbyAdapter", "Place " + place.name);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_place, parent, false);
}
NearbyViewHolder viewHolder = new NearbyViewHolder(convertView);
viewHolder.bindModel(mContext, place);
// Return the completed view to render on screen
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
}

View file

@ -190,53 +190,6 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
} }
} }
private class NearbyAdapter extends ArrayAdapter<Place> {
public List<Place> placesList;
private Context mContext;
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place, places);
mContext = context;
placesList = places;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Place place = getItem(position);
Log.d(TAG, "Place " + place.name);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_place, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
TextView distance = (TextView) convertView.findViewById(R.id.distance);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
String quotelessName = place.name.replaceAll("^\"|\"$", "");
// Populate the data into the template view using the data object
tvName.setText(quotelessName);
tvDesc.setText(place.description);
distance.setText(place.distance);
icon.setImageResource(ResourceUtils.getDescriptionIcon(place.description));
// Return the completed view to render on screen
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
}
private List<Place> loadAttractionsFromLocation(final LatLng curLatLng) { private List<Place> loadAttractionsFromLocation(final LatLng curLatLng) {
List<Place> places = NearbyPlaces.get(); List<Place> places = NearbyPlaces.get();

View file

@ -0,0 +1,35 @@
package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.ViewHolder;
import fr.free.nrw.commons.utils.ResourceUtils;
public class NearbyViewHolder implements ViewHolder<Place> {
@BindView(R.id.tvName) TextView tvName;
@BindView(R.id.tvDesc) TextView tvDesc;
@BindView(R.id.distance) TextView distance;
@BindView(R.id.icon) ImageView icon;
public NearbyViewHolder(View view) {
ButterKnife.bind(this, view);
}
@Override
public void bindModel(Context context, Place place) {
String quotelessName = place.name.replaceAll("^\"|\"$", "");
// Populate the data into the template view using the data object
tvName.setText(quotelessName);
tvDesc.setText(place.description);
distance.setText(place.distance);
icon.setImageResource(ResourceUtils.getDescriptionIcon(place.description));
}
}