Touqeer Aslam
Touqeer Aslam

Reputation: 1

Post method is posting last value from queue in volley android

I am posting value from my fragment adapter through volley I am posting the match ID against each item and every item has its own unique ID. All the items get correct ID but when i post the ID, it gets the last item ID not that ID which is selected. When i post, it posts the last value present in the queue. But i want to post the current ID of that item which is selected. Here is my code of adapter:

public class LiveMatchAdapter extends RecyclerView.Adapter<LiveMatchAdapter.LiveMatchViewHolder> {
private List<LiveMatchModel> liveMatchlist;
private Context context;
private String url = "http://www.stellarsally.com/cricket/live_players_api.php";

private String match_id;
public LiveMatchAdapter(List<LiveMatchModel> liveMatchlist, Context context) {
    this.liveMatchlist = liveMatchlist;
    this.context = context;
}

@NonNull
@Override
public LiveMatchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.layout_live_matches, parent, false);

    return new LiveMatchViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final LiveMatchViewHolder holder, int position) {
    final LiveMatchModel liveMatchModel = liveMatchlist.get(position);

    match_id = liveMatchModel.getMatchId();
    holder.matchType.setText(liveMatchModel.getMatchType());
    holder.liveTeam1.setText(liveMatchModel.getTeam1());
    holder.liveTeam2.setText(liveMatchModel.getTeam2());
    holder.liveTotalRuns.setText(liveMatchModel.getMatchRuns());
    holder.liveOuts.setText(liveMatchModel.getMatchOuts());
    holder.liveOvers.setText(liveMatchModel.getMatchOvers());
    holder.liveOverBalls.setText(liveMatchModel.getMatchOverBall());
   // holder.matchId.setText(match_id);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            postData();
            Toast.makeText(context, ""+match_id, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, LiveMatchDetailsActivity.class);
            intent.putExtra("team1", holder.liveTeam1.getText().toString());
            intent.putExtra("team2", holder.liveTeam2.getText().toString());
            context.startActivity(intent);

        }
    });

}

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

public class LiveMatchViewHolder extends RecyclerView.ViewHolder {
    TextView liveTeam1, matchId, liveTeam2, liveTotalRuns, liveOuts, liveOvers, liveOverBalls, matchType;

    public LiveMatchViewHolder(@NonNull View itemView) {
        super(itemView);
       // matchId = itemView.findViewById(R.id.tvMatchId);
        matchType = itemView.findViewById(R.id.textViewLiveMatchType);
        liveTeam1 = itemView.findViewById(R.id.tvLiveTeam1);
        liveTeam2 = itemView.findViewById(R.id.tvLiveTeam2);
        liveTotalRuns = itemView.findViewById(R.id.tvLiveTotalRuns);
        liveOuts = itemView.findViewById(R.id.tvLiveMatchOuts);
        liveOvers = itemView.findViewById(R.id.tvLiveMatchOver);
        liveOverBalls = itemView.findViewById(R.id.tvLiveMatchOverBall);
    }
}

public void postData() {
    final ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String ServerResponse) {

                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Showing response message coming from server.
                    Toast.makeText(context, "Server "+ServerResponse, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Showing error message if something goes wrong.
                    Toast.makeText(context, "Error"+volleyError.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // Creating Map String Params.
            Map<String, String> params = new HashMap<>();
            // Adding All values to Params.
            params.put("match_id", match_id);
            return params;
        }
    };
    // Creating RequestQueue.
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    // Adding the StringRequest object into requestQueue.
    requestQueue.add(stringRequest);
}

}

Upvotes: 0

Views: 36

Answers (0)

Related Questions