没有更新Android中CardView的适配器位置。

时间:2018-05-22 09:20:59

标签: android android-fragments android-recyclerview android-cardview

我已在两个上为相同的Api数据设置。点击其中一个卡后,另一个片段上的同一张卡的新详细信息片段已打开。包含图片。点击其中一张图片后,我需要获取该CardView位置的信息。但是,当我点击First Fragment中的一张卡片时,我会移动到同一张卡片{{1在position Second,但我刷到其他卡并点击其他Fragment的图像仍然我获取了首先点击的同一张卡的信息。
这是CardView

的代码
Adapter

这是设置class public class StatusInfoAdapter extends RecyclerView.Adapter<StatusInfoAdapter.ViewHolder> { private FragmentManager fm; private FragmentTransaction tx; private ArrayList<StatusEntry> entries; private int finalPos,tempPos; Context context; String People, id,Id; SnackBarAlert alertMsg=new SnackBarAlert(); private String myToken; public StatusInfoAdapter(ArrayList<StatusEntry> entries, Context context) { this.entries = entries; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.status_info_card, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder,int position) { StatusEntry entry = entries.get(position); tempPos = holder.getAdapterPosition(); finalPos = tempPos + 1; holder.id.setText(entry.getid()); holder.Idno.setText(entry.getidno()); holder.fname.setText(entry.getFname()); holder.lname.setText(entry.getLname()); holder.people.setText(entry.getPeople()); noOfPeople = entries.get(position).getPeople(); fid = entries.get(position).getFyraid(); Id = entries.get(position).getid(); } @Override public int getItemCount() { return entries.size(); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; } public class ViewHolder extends RecyclerView.ViewHolder { public TextView fname; public TextView lname; public TextView people; public TextView d; TextView Idno; ImageView cancelEntry; public ViewHolder(View v) { super(v); SharedPreferences preferences=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE); fname = (TextView) v.findViewById(R.id.fname); lname = (TextView) v.findViewById(R.id.lname); people = (TextView) v.findViewById(R.id.adults); fyra_id = (TextView) v.findViewById(R.id.fyraid); Idno = (TextView) v.findViewById(R.id.Id); cancelEntry = (ImageView) v.findViewById(R.id.cancel); cancelEntry.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final AppCompatActivity activity = (AppCompatActivity) v.getContext(); final Dialog dialog=new Dialog(activity); dialog.setContentView(R.layout.cancel_reason); dialog.show(); Button yes = (Button) dialog.findViewById(R.id.yes); Button no = (Button) dialog.findViewById(R.id.no); no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); } }); yes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); removeFromQueue(); alertMsg.infoMessage(v,"Removed from List"); entries.remove(tempPos); notifyItemRemoved(tempPos); ChooseTab chooseTab1=new ChooseTab(); fm=activity.getSupportFragmentManager(); CurrentStatus currentStatus=new CurrentStatus(); tx=fm.beginTransaction(); tx.detach(currentStatus); tx.attach(currentStatus); tx.replace(R.id.frame,chooseTab1); tx.commit(); } }); } }); } }

的代码
RecyclerView

刷卡到其他卡后,我为所有CardView获得相同的JsonArrayRequest request=new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { try{ for(int i=0;i<response.length();i++) { JSONObject object=response.getJSONObject(i); final StatusEntry entry=new StatusEntry(); String id=object.getString("fid"); Idno=object.getString("idno"); JsonObjectRequest(Request.Method.GET,url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { String first_name=response.getString("firstname"); String last_name=response.getString("lastname"); String id=response.getString("id"); entry.setFname(first_name); entry.setLname(last_name); entry.setid(id); entry.setidNo(Idno); adapter.notifyDataSetChanged(); progressDialog.hide(); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) }; AppController.getInstance().addToRequestQueue(foodie_request,foodie_data); String no_of_people=object.getString("no_people"); entry.setPeople(no_of_people); progressDialog.dismiss(); entries.add(entry); adapter = new StatusInfoAdapter(entries, getActivity().getApplicationContext()); recyclerView.setAdapter(adapter); } }catch(JSONException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) }; AppController.getInstance().addToRequestQueue(request,foo_request); } 。如何解决此问题?

1 个答案:

答案 0 :(得分:0)

在开始事务时传递片段构造函数中的list模型,并通过在构造函数的另一个片段中创建相同模型来接收它