繁体   English   中英

Recycler Adapter不使用更新的适配器进行更新

[英]Recycler Adapter Not updating with updated adapter

我有一个fragment_1 ,它有一个按钮调用ADD ,它将用户发送到fragment_2以填充4 5 Edit Text并在同一个fragment_2用户需要按Save Form返回到fragment_1,其中详细信息填写在fragment_2上,直到这里一切正常。 log我可以看到fragment_1从fragment_2获取数据

Fragment_1具有RecyclerView以显示填充在Fragment_2上的用户表单

我的问题是因为我在fragment_1中使用单独的方法获取数据。在该方法中,我调用的是adapter.notifyDataSetChanged(); 应该调用适配器方法,但只有getItemCount运行的不是处理RecyclerView其他方法。 请检查下面的Logcat以了解问题

这是我的方法,我调用notifydatasetchanged()

   public void PPL_Location(PPL_list_wrapper ppl_list_wrapper){
        PPL_wrapper=ppl_list_wrapper;
        Log.d(TAG,"PROFILE DATA CALLING");
        Log.d(TAG,ppl_list_wrapper.toString());
        Loc_details.add(PPL_wrapper);
        Log.d(TAG,PPL_wrapper.getName());
        adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details);
        int item=adapter.getItemCount();
    adapter.notifyDataSetChanged();
        Log.d(TAG,"Here is the value of Location Details: "+"Size "+Loc_details.size()+" "+"Details "+Loc_details.iterator()+" "+ "Location "+Loc_details.get(0)+" "+item);
    }

这是我的适配器名为PPL_Re_Adapter

public class ppl_Recycler_Adapter extends RecyclerView.Adapter<ppl_Recycler_Adapter.ViewHolder> {
    List<PPL_list_wrapper> ppl_Details;
    Context context;
    public static final String TAG="PPL_Re_Adapter####";


    public ppl_Recycler_Adapter(Context context,List<PPL_list_wrapper> ppl_Details ) {
        this.ppl_Details=ppl_Details;
        this.context=context;
        Log.d(TAG,"Adapter Constructor Running With Parameters");

    }


    @Override
    public ppl_Recycler_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater= (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.ppl_single_row,parent,false);
        Log.d(TAG,"onCreate View Holder");    
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ppl_Recycler_Adapter.ViewHolder holder, int position) {
        Log.d(TAG,"onBindViewHolder");
        if (ppl_Details.size()==0){
            Log.d(TAG,"List is Null");    
        }
        else {
            Log.d(TAG,"Process Views Here");
        }    
    }

    @Override
    public int getItemCount() {
        if (ppl_Details==null && ppl_Details.isEmpty()){
            Log.d(TAG,"List Is Null");    
            return 0;    
        }
       else {
            Log.d(TAG,"List Is Not Null");
            Log.d(TAG,"Array Size is "+ppl_Details.size());
            return ppl_Details.size();
        }
    }

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

        ImageView ppl_Image;
        TextView ppl_name;
        TextView ppl_address;
        TextView ppl_timePeriod;
        ImageButton ppl_delete;
        ImageButton ppl_Verify;
        FloatingActionButton fab;    

        public ViewHolder(View itemView) {
            super(itemView);
            Log.d(TAG,"View Holder Running");
            ppl_Image= (ImageView) itemView.findViewById(R.id.past_permanent_location_picture);
            ppl_name= (TextView) itemView.findViewById(R.id.name);
            ppl_address= (TextView) itemView.findViewById(R.id.address);
            ppl_timePeriod= (TextView) itemView.findViewById(R.id.time_period);
            ppl_delete= (ImageButton) itemView.findViewById(R.id.delete);
            ppl_Verify= (ImageButton) itemView.findViewById(R.id.verify);
            fab= (FloatingActionButton) itemView.findViewById(R.id.PPL_fab_Add_PPL);
            itemView.setOnClickListener(this);    
        }

        @Override
        public void onClick(View v) {
            Context context=v.getContext();
            Intent showPPL_Form=new Intent(context,Add_PPL.class);
            context.startActivity(showPPL_Form);
        }
    }

这里是Logcat 。在Logcat中,没有任何Log在logcat中显示来自适配器然后是getItemCount

09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: PROFILE DATA CALLING
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: com.example.com.pro_working1.mainActivityFragments.PPL_list_wrapper@426c3f50
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: n sana
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Adapter Constructor Running With Parameters
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 1
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: Here is the value of Location Details: Size 1 Details java.util.ArrayList$ArrayListIterator@426c51d8 Location com.example.com.pro_working1.mainActivityFragments.PPL_list_wrapper@426c3f50 1
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/Navigation Drawer*****: Here is PPL DATA com.example.com.pro_working1.mainActivityFragments.PPL_list_wrapper@426c3f50
09-06 16:12:46.523 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Adapter Constructor Running With Parameters
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0

Fragment_1完整代码

RecyclerView mRecyclerView;
    RecyclerView.Adapter adapter;
    List<PPL_list_wrapper> Loc_details=new ArrayList<PPL_list_wrapper>();
    FloatingActionButton fab;
    PPL_list_wrapper PPL_wrapper;
    public static final String TAG="====Fragment_1====";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_fragment_1, container, false);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.ppl_RecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details);
        mRecyclerView.setAdapter(adapter);

        fab= (FloatingActionButton) view.findViewById(R.id.PPL_fab_Add_PPL);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
                Add_PPL add_ppl=new Add_PPL();
                fragmentTransaction.add(R.id.Navigation_Main_Layout,add_ppl);
                fragmentTransaction.commit();
            }
        });




        return view;

    }

    public void PPL_Location(PPL_list_wrapper ppl_list_wrapper){
        PPL_wrapper=ppl_list_wrapper;
        Log.d(TAG,"PROFILE DATA CALLING");
        Log.d(TAG,ppl_list_wrapper.toString());
        Loc_details.add(PPL_wrapper);
        Log.d(TAG,PPL_wrapper.getName());
        adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details);
        int item=adapter.getItemCount();
        adapter.notifyDataSetChanged();
        Loc_details.add(PPL_wrapper);
        Log.d(TAG,"Here is the value of Location Details: "+"Size "+Loc_details.size()+" "+"RecyclerView "+mRecyclerView+" "+ "Location "+Loc_details.get(0)+" "+"Item Size "+item);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.invalidate();


    }

我试过下面的代码,它对我有用:

recyclerView.setAdapter(new RecyclerViewAdapter(newList));
recyclerView.invalidate();

在你的情况下,它没有发生,因为你正在为你以前的活动提供新的数据,所以首先你必须填充你的容器,然后需要再次发送适配器并使其无效。

您好,您可以尝试这样做,而不是每次尝试清除适配器并添加新列表时创建新适配器。 此方法应该在您的适配器中,mList应该是填充适配器的列表:

public void setAll(@NonNull
                   final List<Object> yourCustomObjectList) {
    mList.clear();
    mList.addAll(yourCustomObjectList);
    notifyDataSetChanged();
}

你首先调用适配器没有结果,然后你填写表格,并期望细节填写到您的RecyclerView,问题是你得到结果片段但你的列表没有这个结果。

现在按照你的代码我试过,我将适配器的getItemCount返回值硬编码为1,它将所有方法显示到logcat,所以我只是让你的列表静态,现在它的工作。

请将您的列表设为静态

static List<PPL_list_wrapper> Loc_details=new ArrayList<PPL_list_wrapper>();

希望它有所帮助

正如你所说,你的布局同时显示两个片段。 它是片段添加或替换的问题,因此根据您的代码更改此行

Add_PPL add_ppl=new Add_PPL();
fragmentTransaction.add(R.id.Navigation_Main_Layout,add_ppl);

进入这个

fragmentTransaction.replace(R.id.Navigation_Main_Layout,add_ppl);

对于你的第二个问题,布局显示大的边距,请检查你的单行布局xml并确保高度是wrap_content或在此发布您的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM