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

时间:2016-09-06 11:11:49

标签: java android list android-recyclerview adapter

我有 fragment_1 ,其中有一个按钮调用 ADD ,可将用户发送到 fragment_2 使用4 5 编辑文本填写表单并在同一片段_2上用户需要按保存表单返回到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中,来自适配器的logcat中没有任何Log显示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();


    }

3 个答案:

答案 0 :(得分:0)

我尝试了以下代码,它对我有用:

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您首先调用适配器而没有结果,然后填写表单,并期望详细信息填入您的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或在此处发布您的代码

相关问题