尝试将recylerview添加到片段时找不到资源异常

时间:2019-07-03 07:12:19

标签: java android android-fragments android-recyclerview recycler-adapter

我正在尝试将recyclerview添加到fragment,但是不知何故adapter中出现错误。

我认为这是绑定它的方式,我无法找到解决方案。有人可以帮我解决问题吗?

这是我的适配器:

public class MyLeaguesListAdapter extends 
    RecyclerView.Adapter<MyLeaguesListAdapter.MyViewHolder>{

    List<MyContest> listItems;
    Context context;
    public interface OnItemClickListener {
        void onItemClick(View v,int pos);

    }
    public MyLeaguesListAdapter(List<MyContest> listItems,Context context){
        this.listItems = listItems;
        this.context = context;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v;
        v = LayoutInflater.from(context).inflate(R.layout.my_contest_item,parent,false);

        MyViewHolder myViewHolder = new MyViewHolder(v);
        return myViewHolder;

    }
    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView leagueName,playerName,prizePool,playerRank,playerPoints,entryFee,spots;
        OnItemClickListener onItemCheckListener;

        public MyViewHolder(View itemView) {
            super(itemView);
            leagueName = (TextView) itemView.findViewById(R.id.mc_contest_name);
            playerName = (TextView) itemView.findViewById(R.id.mc_player_name);
            prizePool = (TextView) itemView.findViewById(R.id.mc_prize_pool);
            playerRank = (TextView) itemView.findViewById(R.id.mc_player_rank);
            playerPoints = (TextView) itemView.findViewById(R.id.mc_player_points);
            entryFee = (TextView) itemView.findViewById(R.id.mc_entry_fee);
            spots = (TextView) itemView.findViewById(R.id.mc_spots);
            itemView.setOnClickListener(this);
        }
        public void setItemClickListener(OnItemClickListener ic){
            this.onItemCheckListener = ic;
        }

        @Override
        public void onClick(View view) {
            this.onItemCheckListener.onItemClick(view,getAdapterPosition());
        }
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MyContest listItem = listItems.get(position);
        holder.leagueName.setText(listItem.getLeaguename());
        holder.prizePool.setText(listItem.getPrizepool());
        holder.playerPoints.setText(listItem.getScore());
        holder.playerRank.setText(listItem.getRank());
        holder.playerName.setText(listItem.getUsername());
        holder.spots.setText(listItem.getSpots());
        holder.entryFee.setText(listItem.getFee());
        holder.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {

            }
        });
    }
}

这是我的片段

public class MyLeagues extends Fragment {
    SharedPreferences sharedPreferences,ui;
    int score,userid,i=0;
    String username,lname,inviteCode;
     List<MyContest> list_item;
     MyLeaguesListAdapter adapter;
    ProgressBar progressBar;
    Sprite fadingCircle;
    RecyclerView recyclerView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_leagues,null);
        ui = this.getActivity().getSharedPreferences("User",Context.MODE_PRIVATE);
        userid = ui.getInt("userid",-1);
        progressBar = (ProgressBar) view.findViewById(R.id.mcspin_kit);
        fadingCircle = new FadingCircle();
        progressBar.setIndeterminateDrawable(fadingCircle);
        Log.d("eede", "onCreateView: "+inviteCode);
        recyclerView = (RecyclerView) view.findViewById(R.id.mc_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        list_item = new ArrayList<>();
        getMYContests();
        return view;
    }
    public void getMYContests(){
        Call<MyContestResponse> call = RetrofitClient
                .getInstance()
                .getApi()
                .getMycontests(userid);
        call.enqueue(new Callback<MyContestResponse>() {
            @Override
            public void onResponse(Call<MyContestResponse> call, Response<MyContestResponse> response) {
                list_item=response.body().getData();
                adapter = new MyLeaguesListAdapter(list_item,getContext());
                progressBar.setVisibility(View.INVISIBLE);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<MyContestResponse> call, Throwable t) {
                Log.d("bruv", "onFailure: "+t);
            }
        });
    }

}

错误:

  

07-03 09:36:20.980 30095-30095 /? E / AndroidRuntime:致命异常:   主要           流程:com.example.android.khelo,PID:30095           android.content.res.Resources $ NotFoundException:字符串资源ID#0xffffffff               在android.content.res.Resources.getText(Resources.java:363)               在android.widget.TextView.setText(TextView.java:6485)               在com.example.android.khelo.MyLeaguesListAdapter.onBindViewHolder(MyLeaguesListAdapter.java:75)               在com.example.android.khelo.MyLeaguesListAdapter.onBindViewHolder(MyLeaguesListAdapter.java:20)

根据我所读的内容,问题出在我绑定recylerview的方式上,但是我对问题的实际位置尚不明确。

2 个答案:

答案 0 :(得分:0)

您的listItem属性中的一个或多个可能返回诸如getScoregetFee之类的整数。因此,让我们看一下TextView.setText(int)

的实现
public final void setText(int resid) {
     ...
}

如您所见,它仅接受int作为资源ID,例如R.string.something。 因此,在这里您可以做的就是让您的int成为string的setText之类的

holder.playerPoints.setText(listItem.getScore().toString());

答案 1 :(得分:0)

如果每种方法都返回MyContest,请检查String文件的getter方法,或者使用下面的onBindViewHolder()将所有值转换为String来设置文本。

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    MyContest listItem = listItems.get(position);
    holder.leagueName.setText(listItem.getLeaguename().toString());
    holder.prizePool.setText(listItem.getPrizepool().toString());
    holder.playerPoints.setText(listItem.getScore().toString());
    holder.playerRank.setText(listItem.getRank().toString());
    holder.playerName.setText(listItem.getUsername().toString());
    holder.spots.setText(listItem.getSpots().toString());
    holder.entryFee.setText(listItem.getFee().toString());
    holder.setItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(View v, int pos) {}
    });
}