Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView.Adapter get item position of my list

I have created a RecyclerView.Adapter and I want to fill a list.. so my problem now is I don't know how to implement the get position of my list.

below you can see my code:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<Result> _contents;

    public RecyclerViewAdapter(List<Result> contents) {
        this._contents = contents;
    }


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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        View view = null;
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_card_small, parent, false);

        Result tempResult = _contents.get(position);

        TextView temp = (TextView) view.findViewById(R.id.text_city_name);
        temp.setText(tempResult.getInfo().getCity().getName());

        return new RecyclerView.ViewHolder(view) {
        };
    }

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

       }
    }

so the function onCreateViewHolder doesn't get properly the id is always the same id.. how can I fix this or implement to get the right position of mi list?¿ What it´s happening now is the list has the right number of items but is always the same item, the fist one. I guess it´s something simple but I cant figure out how to implement it.

Thanks!!

like image 924
gon250 Avatar asked Feb 10 '23 09:02

gon250


2 Answers

I have fixed the issue implementing my own ViewHolder, I was a little bit confused. Below is the code working properly.

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    List<Result> _contents;

    public RecyclerViewAdapter(List<Result> contents) {
        this._contents = contents;
    }


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

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

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Result tempResult = _contents.get(position);
        holder.mTextView.setText(tempResult.getInfo().getCity().getName());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.text_city_name);
        }
    }
}
like image 90
gon250 Avatar answered Feb 20 '23 01:02

gon250


I'm not sure why you need the position of your list. You can get the position in onBindViewHolder. However, you can put any data you inside ViewHolder and use them later on.

public class RecyclerViewAdapter extends RecyclerView.Adapter<ResultViewHolder> {
    List<Result> _contents;

    public RecyclerViewAdapter(List<Result> contents) {
        this._contents = contents;
    }

    public Result getItem(int position) {
        return _contents != null ? _contents.get(position) : null;
    }

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

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

    @Override
    public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_card_small, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ResultViewHolder holder, int position;) {
        Result result = getItem(position);
        holder.position = position;
        holder.result = result;
        holder.cityName.setText(result.getInfo().getCity().getName());
    }

    public static class ResultViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener {
        int position = -1;
        Result result;
        TextView cityName;

        public ResultViewHolder(View view) {
            super(view);
            cityName = (TextView) view.findViewById(R.id.text_city_name);
            view.setOnClickListener(this);
        }

        public void onClick(View v) {
            // do whatever you want with list posisiton and result...
        }
    }
}
like image 20
Jason Chueh Avatar answered Feb 20 '23 02:02

Jason Chueh