Android - 当我点击列表视图项目时,在吐司中显示错误的数据

时间:2015-10-02 21:16:53

标签: android json listview android-volley

当我点击某个特定ID时,它会显示其他一些id数据,请检查以下代码以了解我的问题

这段代码出了什么问题,我发现在这里没有保存任何位置,如果只是问题给出了一些提示呢

public FeedListAdapter(Activity activity, List<FeedItem> movieItems) {
    this.activity = activity;
    this.feedItems = movieItems;
    // this.intentContext = activity;
}


@Override
public int getCount() {
    return feedItems.size();
}

@Override
public Object getItem(int location) {
    return feedItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_news_feed, null);

    if (imageLoader == null)
        imageLoader = VolleyApplication.getInstance().getImageLoader();
    final NetworkImageView profiePic = (NetworkImageView) convertView.findViewById(R.id.imgProfilePic);
    final NetworkImageView postImage = (NetworkImageView) convertView.findViewById(R.id.imgPostImage);
    personName = (TextView) convertView.findViewById(R.id.txPersonName);
    personEmail = (TextView) convertView.findViewById(R.id.txEmail);
    message = (TextView) convertView.findViewById(R.id.txMessage);
    createdAt = (TextView) convertView.findViewById(R.id.txCreatedAt);
    final TextView webLink = (TextView) convertView.findViewById(R.id.txWebLink);
    webLink.setPaintFlags(webLink.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    webLink.setTextColor(Color.parseColor("#0073bd"));

    postImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String getURL = postImage.getImageURL().toString();
            String name = personName.getText().toString();
            Intent intent = new Intent(v.getContext(), ImageViewActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("imageurl", getURL);
            intent.putExtra("name", name);
            v.getContext().startActivity(intent);
        }
    });

    personName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = personName.getText().toString();
            Toast.makeText(personName.getContext(),name,Toast.LENGTH_SHORT).show();
        }
    });

    postImage.setOnTouchListener(new View.OnTouchListener() {
        private Rect rect;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                postImage.setColorFilter(Color.argb(50, 0, 0, 0));
                rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                postImage.setColorFilter(Color.argb(0, 0, 0, 0));
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                postImage.setColorFilter(Color.argb(0, 0, 0, 0));

            }
            return false;
        }
    });


    // getting movie data for the row
    m = feedItems.get(position);

    personName.setText(m.getPersonName());
    personEmail.setText(m.getEmail());
    message.setText(m.getMessage());
    createdAt.setText(m.getCreatedAt());

    imgDelete = (ImageView) convertView.findViewById(R.id.imgDelete);
    SharedPreferences preferencesUserDetails = convertView.getContext().getSharedPreferences("UserDetails", convertView.getContext().MODE_PRIVATE);
    final String userName = preferencesUserDetails.getString("name", null);
    final String emailId = preferencesUserDetails.getString("email", null);
    String getPersonName = personName.getText().toString();
    String getPersonEmail = personEmail.getText().toString();

    if (getPersonName.equals(userName) && getPersonEmail.equals(emailId)) {
        imgDelete.setVisibility(View.VISIBLE);
    } else {
        imgDelete.setVisibility(View.GONE);
    }


    txId = (TextView) convertView.findViewById(R.id.txId);
    txId.setText(m.getIdOf());

    liLike = (LinearLayout) convertView.findViewById(R.id.liLike);
    imgLike = (ImageView) convertView.findViewById(R.id.imgLike);
    txLike = (TextView) convertView.findViewById(R.id.txLike);
    txLike.setText(m.getLiked());

    liLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                insertLike();
                //Toast.makeText(liLike.getContext(),"dssd",Toast.LENGTH_SHORT).show();
        }
    });

    // thumbnail image
    profiePic.setImageUrl(m.getThumbnailUrl(), imageLoader);


    return convertView;
}


private void insertLike() {

    final String geTtxId = txId.getText().toString();

    //  showProgress(true);
    final StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {

            imgLike.setImageResource(R.drawable.icn_thumb_up_active);
            Intent i = new Intent(liLike.getContext(),MainActivity.class);
            liLike.getContext().startActivity(i);
            Toast toast = Toast.makeText(liLike.getContext(), "Data Inserted Successfully"+geTtxId, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            //showProgress(false);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //  showProgress(false);
            imgLike.setImageResource(R.drawable.icn_thumb_up);
            Toast toast = Toast.makeText(liLike.getContext(), "please check your internet connection", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {

            SharedPreferences preferencesUserDetails = liLike.getContext().getSharedPreferences("UserDetails", liLike.getContext().MODE_PRIVATE);
            final String userName = preferencesUserDetails.getString("name", null);
            final String emailId = preferencesUserDetails.getString("email", null);

            Log.w("postid", userName);
            Map<String, String> params = new HashMap<String, String>();
            params.put("person_name", userName);
            params.put("postid", geTtxId);
            params.put("email_id", emailId);
            return params;
        }
    };

    // Adding request to request queue
    VolleyApplication.getInstance().addToRequestQueue(postRequest);

}

1 个答案:

答案 0 :(得分:0)

我认为您的问题是因为您正在设置

personName.setOnClickListener

在同一个对象上多次。 回收视图时,使用:

  if (convertView == null)
    convertView = inflater.inflate(R.layout.list_news_feed, null);

因此,假设您已经使用setOnClickListener连接了此视图,在回收后再次连接它。

一个简单的解决方案是使用以下方法中的索引:

 personName.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        m = feedItems.get(position);
        //now get what you need from m
        Toast.makeText(personName.getContext(),m.getPersonName(),Toast.LENGTH_SHORT).show();
    }
});

您可能还想添加一项检查以了解对象已经具有ClickListener的天气,而不是多次设置

可能的解决方案是这样的:

 if (convertView == null) {
   //Init your views etc...
     personName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
      });
  }
  else
  {
      //just update the data, but don't set the click listeners
  }