0

我有带有代码的 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?

4

2 回答 2

0

我无法准确理解您的需求。如果您想从LocationSelectionFragment以下位置调用方法onClickListener

创建LocationSelectionFragment你的方法

public void doSomethingWithOisitionFromGetView(int position) {
    //method body
}

在 LocationAdapter 的构造器上添加一个参数,同时持有一个指向新参数的私有变量

private LocationSelectionFragment lsf;

public LocationAdapter(LocationSelectionFragment lsf, Context context, int resource,
            ArrayList<LocationRecord> objects) {
        super(context, resource, objects);
        this.lsf=lsf;
        Log.message("Enter");
        mContext = context;
        mResource = resource;
        mObjects = objects;
    }

现在在你fillData()的类方法上LocationSelectionFragment,像这样实例化适配器

LocationAdapter adapter = new LocationAdapter(LocationSelectionFragment.this, mView.getContext(),
                R.layout.location_row, arrayList);

现在onClickListener你可以做:

holder.locationRow_image_edit .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.message("Enter");
                        Log.message("Edit #" + String.valueOf(position));
                        lsf.doSomethingWithOisitionFromGetView(position);
                    }
                });
于 2013-10-25T06:49:28.547 回答
0

你不能,真的。

您可以做的是在适配器中定义一个接口,该接口具有 onLocationImageEdit(int position) 和 onLocationImageRemove(int position) 方法,并使调用类实现该接口。保留对调用者类的引用,将其转换为适配器内的接口并将其用于回调。以您现在的方式设置 OnClickListeners,但在侦听器中调用适当的方法。

这是一个使其更清晰的示例:

public class LocationAdapter extends ArrayAdapter<LocationRecord> {
    public interface LocationImageChangeListener{
        public void onLocationImageEdit(int position);
        public void onLocationImageRemove(int position);
    }

    private LocationImageChangeListener mCallback;

    public LocationAdapter(Context context, int resource,
            ArrayList<LocationRecord> objects, LocationImageChangeListener callback) {
        ....
        mCallback = callback;
    }
    ....
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ...
        holder.locationRow_image_edit
                    .setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            mCallback.onLocationImageEdit(position);
                        }
                    });
        ...
    }
}


public class LocationSelectionFragment extends SherlockDialogFragment implements LocationImageChangeListener {
        .....
    private void fillData(){
        ...
        LocationAdapter adapter = new LocationAdapter(mView.getContext(),
            R.layout.location_row, arrayList, this);
    }

    @Override
    onLocationImageEdit(int position){
           Log.message("Enter");
           Log.message("Edit #" + String.valueOf(position));
       }
    }
}

虽然有点冗长,但这种方法促进了松散耦合,并允许您在适配器中使用 LocationImageChangeListener 的任何实现,这在此过程中可能很有用,并且通常被认为是软件架构中的良好实践。

我徒手写了这个,所以它可能有语法错误或其他东西,但我希望你能得到它的一般设计。

于 2013-10-25T06:49:41.963 回答