我有带有代码的 LocationAdapter 类:
package ru.neverdark.phototools.utils;
import java.util.ArrayList;
import ru.neverdark.phototools.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* A class provide adapter for locations
*/
public class LocationAdapter extends ArrayAdapter<LocationRecord> {
private Context mContext;
private int mResource;
private ArrayList<LocationRecord> mObjects = new ArrayList<LocationRecord>();
public LocationAdapter(Context context, int resource,
ArrayList<LocationRecord> objects) {
super(context, resource, objects);
Log.message("Enter");
mContext = context;
mResource = resource;
mObjects = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.message("Enter");
View row = convertView;
LocationHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(mResource, parent, false);
holder = new LocationHolder();
holder.locationRow_image_edit = (ImageView) row
.findViewById(R.id.locationRow_image_edit);
holder.locationRow_image_remove = (ImageView) row
.findViewById(R.id.locationRow_image_remove);
holder.locationRow_label = (TextView) row
.findViewById(R.id.locationRow_label);
row.setTag(holder);
} else {
holder = (LocationHolder) row.getTag();
}
LocationRecord record = mObjects.get(position);
holder.locationRow_label.setText(record.locationName);
// TODO need change "<=" to ">"
/* if is not current location and not point on map */
if (position <= Constants.LOCATION_POINT_ON_MAP_CHOICE) {
holder.locationRow_image_edit.setVisibility(View.VISIBLE);
holder.locationRow_image_remove.setVisibility(View.VISIBLE);
holder.locationRow_image_edit
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.message("Enter");
Log.message("Edit #" + String.valueOf(position));
}
});
holder.locationRow_image_remove
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.message("Enter");
Log.message("Remove #" + String.valueOf(position));
// TODO remove
}
});
}
return row;
}
static class LocationHolder {
ImageView locationRow_image_remove;
ImageView locationRow_image_edit;
TextView locationRow_label;
}
}
位置记录类:
package ru.neverdark.phototools.utils;
/**
* A class contains one record from Locations table
*/
public class LocationRecord {
public long _id;
public String locationName;
public double latitude;
public double longitude;
public LocationRecord(long _id, String locationName, double latitude,
double longitude) {
super();
this._id = _id;
this.locationName = locationName;
this.latitude = latitude;
this.longitude = longitude;
}
}
我有 LocationSelectionFragment(LocationAdapter 的类调用者):
package ru.neverdark.phototools.fragments;
import java.util.ArrayList;
import ru.neverdark.phototools.R;
import ru.neverdark.phototools.utils.LocationAdapter;
import ru.neverdark.phototools.utils.LocationRecord;
import ru.neverdark.phototools.utils.Log;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockDialogFragment;
public class LocationSelectionFragment extends SherlockDialogFragment {
private View mView;
private ListView mListView;
/**
* Binds classes objects to resources
*/
private void bindObjectsToResources() {
mListView = (ListView) mView
.findViewById(R.id.locationSelection_listView);
}
/**
* Fills list view
*/
private void fillData() {
Log.message("Enter");
ArrayList<LocationRecord> arrayList = new ArrayList<LocationRecord>();
arrayList.add(new LocationRecord(0,
getString(R.string.locationSelection_label_currentLocation), 0,
0));
arrayList.add(new LocationRecord(1,
getString(R.string.locationSelection_label_pointOnMap), 0, 0));
LocationAdapter adapter = new LocationAdapter(mView.getContext(),
R.layout.location_row, arrayList);
mListView.setAdapter(adapter);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.message("Enter");
Dialog dialog = new Dialog(getActivity());
dialog.setTitle(R.string.locationSelection_label_selectLocation);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.message("Enter");
super.onCreateView(inflater, container, savedInstanceState);
mView = inflater.inflate(R.layout.activity_location_selection,
container, false);
bindObjectsToResources();
setOnItemClickListener(this);
fillData();
return mView;
}
// more codes
如何使用 getView 中的“int position”将 OnClickListener 从 LocationAdapter 移动到 LocationSelectionFragment?