在自定义适配器中ListView项目内的项目上设置OnClickListener

时间:2014-07-16 09:55:15

标签: android listview android-listview

我为列表创建了一个自定义适配器,其中不同的项目和每个项目都有一个要邀请的按钮。当单击相应的邀请按钮并且工作正常时,该项应该水平翻转。问题是,当我点击第一个项目的邀请按钮时,也会点击第四个项目的邀请按钮。我附上代码嘿

package rovoltlabs.coffeechat.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rovoltlabs.coffeechat.R;
import rovoltlabs.coffeechat.animation.AnimationFactory;
import rovoltlabs.coffeechat.animation.AnimationFactory.FlipDirection;
import rovoltlabs.coffeechat.volley.utils.Const;
import rovoltslabs.coffeechat.app.AppController;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;

public class MyCustomAdapter extends BaseAdapter implements OnClickListener {

    Context contexts;
    List<String> name = new ArrayList<String>();
    List<String> ids = new ArrayList<String>();
    List<String> slots = new ArrayList<String>();
    List<String> heading = new ArrayList<String>();
    List<String> showtime = new ArrayList<String>();
    List<Bitmap> img = new ArrayList<Bitmap>();
    List<ViewHolder> myview = new ArrayList<ViewHolder>();
    private String tag_json_obj = "jobj_req";

    private LayoutInflater mLayoutInflater;
    View row;
    ViewHolder holder;

    public MyCustomAdapter(Context context, List<String> name,
            List<Bitmap> img, List<String> heading, List<String> slots,
            List<String> id) {
        super();
        this.contexts = context;
        this.name = name;
        this.img = img;
        this.heading = heading;
        this.slots = slots;
        this.ids = id;

        mLayoutInflater = ((Activity) contexts).getLayoutInflater();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return name.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub

        // check to see if the reused view is null or not, if is not null then
        // reuse it
        if (view == null) {
            holder = new ViewHolder();
            view = mLayoutInflater.inflate(R.layout.item, null);
            holder.name = (TextView) view.findViewById(R.id.Name);
            holder.fname = (TextView) view.findViewById(R.id.flipName);
            holder.msg = (EditText) view.findViewById(R.id.fsend);
            holder.heading = (TextView) view.findViewById(R.id.ddheading);
            holder.distance = (TextView) view.findViewById(R.id.Distance);
            holder.image = (ImageView) view.findViewById(R.id.imagecoffee);
            holder.invite = (Button) view.findViewById(R.id.inviteButton);
            holder.send = (Button) view.findViewById(R.id.fsendButton);
            holder.time = (TextView) view.findViewById(R.id.timefree);
            holder.viewAnimator = (ViewAnimator) view
                    .findViewById(R.id.viewFlipper);
            myview.add(holder);
            holder.v = view;

        } else {
            holder = (ViewHolder) view.getTag();

        }
        view.setTag(holder);
        holder.invite.setOnClickListener(this);


        holder.send.setOnClickListener(this);

        holder.name.setText(name.get(position).split("\n")[0]);
        holder.fname.setText(name.get(position).split("\n")[0]);
        holder.heading.setText(heading.get(position));
        String temp = "";
        int sl = Integer.parseInt(slots.get(position));
        if (sl % 2 == 0) {
            temp = "" + ((sl / 2) - 1) + ":30 - " + ((sl / 2)) + ":00";
        } else {
            temp = "" + ((sl / 2)) + ":00-" + ((sl / 2)) + ":30";
        }

        holder.time.setText(temp);

        holder.distance.setText(name.get(position).split("\n")[1] + " m");
        holder.image.setImageBitmap(img.get(position));

        holder.invite.setTag(position);
        holder.send.setTag(position);

        return view;
    }

    private static class ViewHolder {

        protected TextView name;
        protected TextView heading;
        protected TextView distance, time;
        protected ImageView image;
        protected Button invite, send;
        protected View v;
        protected ViewAnimator viewAnimator;
        protected TextView fname;
        protected EditText msg;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.inviteButton) {

            AnimationFactory.flipTransition(
                    myview.get((Integer) v.getTag()).viewAnimator,
                    FlipDirection.LEFT_RIGHT);


        } else if (v.getId() == R.id.fsend) {
            SharedPreferences pref = contexts.getApplicationContext()
                    .getSharedPreferences("MyPref", 0);
            String slotsss = "{\"slots\":[" + slots.get((Integer) v.getTag())
                    + "]}";
            final Map<String, String> params = new HashMap<String, String>();

            params.put("slot", slotsss);
            Log.e("invite slots", slotsss);
            params.put("to", ids.get((Integer) v.getTag()));
            params.put("from", pref.getString("id", "N/A"));
            params.put("message", myview.get((Integer) v.getTag()).msg
                    .getText().toString());
            StringRequest jsonObjReq = new StringRequest(Method.POST,
                    Const.URL_INVITE, new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            Log.d("Slots Response: ", response.toString());
                            Toast.makeText(contexts, "invited" + response,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d("invite Error:",
                                    "Error: " + error.getMessage());

                        }
                    }) {

                /**
                 * Passing some request headers
                 * */

                @Override
                protected Map<String, String> getParams() {

                    return params;
                }

            };

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(jsonObjReq,
                    tag_json_obj);

        }

    }

}

1 个答案:

答案 0 :(得分:0)

View个对象正在循环使用,您为这两个位置使用相同的ViewHolder对象(以及其他UI组件)。被点击的视图已将其标记更新为所需位置,但由于回收,动画对象与另一个位置相同。