Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Adding Views to Images doesn't update - onClick

Summary

When a user clicks on the RecyclerView item, I would like to add tags to that image from the information that has been stored in a BaaS [Sashido] (X Co-ordinates, Y Co-ordinates and Tag name). But, the problem I'm having isn't getting the position per-say. I create a toast when the image has been clicked, it shows the correct position corresponding to the view itself. (zero for the beginning, so on and so forth)

But how to update the position once the user clicks on another item in the list, so that the tags that correspond to the position in the array in Sashido, match the position in the RecyclerView, because at the moment the first row in the Sashido class is populating all images with that row's tags.

My assumption was to the pass the position to the getTagInformation() method using getLayoutPosition() so that when objects.get(position) array is called, it'll get the same position for Sashido class but it isn't. I feel the adapter must not be updating correctly after the user has clicked on a new item.

onBindViewHolder:

@Override
public void onBindViewHolder(RecyclerViewHolderPreviousPosts holder, int position) {
    holder.bind(previousPostsList.get(position), listener);
}

onBind:

void bind(final PreviousPostsDataModel model, final OnItemClickListener listener) { ...

uploadedImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        if (count == 0) {
                            imageid = model.getImageId();
                            Toast.makeText(App.getContext(), "Image ID: " + imageid, Toast.LENGTH_SHORT).show();
                            Toast.makeText(App.getContext(), "Position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                            getTagInformation(getLayoutPosition());
                        } else {
                            Log.e("qwert", "" + imageid);
                            imageContainer.removeAllViews();
                            imageContainer.addView(uploadedImage);
                            count = 0;
                        }
                }
            });
... }

getTagInformation:

private void getTagInformation(final int position) {
                ParseQuery<ParseObject> query = ParseQuery.getQuery("FashionFeed");
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        if (e == null) {
                            Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
                            JSONArray tagNamesArray = objects.get(position).getJSONArray("tagName");
                            JSONArray posXArray = objects.get(position).getJSONArray("tagPointX");
                            JSONArray posYArray = objects.get(position).getJSONArray("tagPointY");

                            for (int i = 0; i < tagNamesArray.length(); i++) {
                                for (int t = 0; t < tagNamesArray.length(); t++) {
                                    tagNames.add(tagNamesArray.optString(t));
                                    tagXPositions.add(posXArray.optString(t));
                                    tagYPositions.add(posYArray.optString(t));

                                }

                                for (int o = 0; o < tagNamesArray.length(); o++) {
                                    tag = new TextView(App.getContext());
                                    tag.setX(Float.parseFloat(tagXPositions.get(o)));
                                    tag.setY(Float.parseFloat(tagYPositions.get(o)));
                                    tag.setText(tagNames.get(o));
                                    tag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                    tag.setMaxLines(1);
                                    tag.setTextSize(11);
                                    tag.setClickable(true);
                                    tag.setHintTextColor(Color.WHITE);
                                    tag.setTextColor(Color.WHITE);
                                    tag.setBackgroundResource(R.drawable.tags_rounded_corners);
                                    imageContainer.addView(tag);
                                    count = 1;
                                }
                            }
                        } else {
                            Toast.makeText(context, "" + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
        }

I have also tried

public void getTagInformation(String imageid) {
ParseQuery query = ParseQuery.getQuery("FashionFeed");
query.WhereEqualTo("objectId", imageId);
....
}

with the imageId passed into the method and with also me manually entering an objectId that will match, it'll still only produce the tags that belong to that objectId. it just doesn't seem that this query is going through all of the objects. Just getting the tag information from that one object and then setting all the images with those tags.

if you need me to provide anymore code, I'm more than happy to.

like image 617
BIW Avatar asked Sep 23 '16 14:09

BIW


1 Answers

Hi @BIW please follow below link link

In onBindViewHolder, you are adding listener every time, so it returns same object every time as recyclerView ViewHolder pattern uses same object to render recyclerView item in onBindViewHolder. So you need to add when you are creating holder object and set listener to it so that you will get a proper position.

package com.subbu.moviemasti.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.subbu.moviemasti.Constants;
import com.subbu.moviemasti.R;
import com.subbu.moviemasti.entities.Movie;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by subrahmanyam on 25-11-2015.
 */
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private final List<Movie> movieList;
    private onRecyclerViewItemClickListener mItemClickListener;

    public MovieAdapter(List<Movie> movieList) {
        this.movieList = movieList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Movie movie = movieList.get(position);
        String imageUrl = Constants.MOVIE_POSTER_BASE_URL + movie.getPosterPath();
        if (imageUrl != null) {
            Picasso.with(holder.posterImage.getContext()).load(imageUrl).
                    placeholder(R.drawable.img_default).
                    into(holder.posterImage);
        }
    }

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

    @Override
    public int getItemCount() {
        return movieList.size();
    }

    public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public interface onRecyclerViewItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.poster)
        ImageView posterImage;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mItemClickListener.onItemClickListener(v, getAdapterPosition());
        }
    }

}

Where we are creating adaper object, from there we need to set listener like adapter.setOnItemClickListener(this)

class MyActivity extendsActivity implements onRecyclerViewItemClickListener {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    manager = new GridLayoutManager(getActivity(), cols);
            gridView.setLayoutManager(manager); 
gridView.setAdapter(adapter);
adapter.setOnItemClickListener(this);
}
 @Override
        public void onItemClickListener(View view, int position) {
            //Do wantever you want to do
        }
}

From ViewHolder class we need to pass position of a recyclerView or we can write code what you need to execute when click on item.

like image 65
subrahmanyam boyapati Avatar answered Sep 21 '22 09:09

subrahmanyam boyapati