Ritu Ritu - 3 months ago 15
Android Question

Recycler Adapter Not updating with updated adapter

I have a fragment_1 which has button call ADD which send user to fragment_2
to fill form with 4 5 Edit Text and on same fragment_2 user need to press Save Form to return back to fragment_1 with details filled on fragment_2 till here everything is working fine.In log i can see fragment_1 getting data from fragment_2

Fragment_1 which has

RecyclerView
to show user form filled on Fragment_2

My problem is as i am getting data in fragment_1 in separate method.In that method i am calling
adapter.notifyDataSetChanged();

which supposed to call adapter methods but only
getItemCount
is running not other method which process
RecyclerView
.
Please Check Logcat below to get Idea about issue

Here is my method where i am calling
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);
}


Here is my Adapter called 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);
}
}


Here Is Logcat.In Logcat none of the Log showing in logcat from adapter then
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 Complete Code

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();


}

Answer

You calling adapter first with no result then you fill form and expected that details fills into your RecyclerView and problem is that you getting result into fragment but your list don't have that result.

Now as per your code i tried and i hard coded return value of adapter's getItemCount into 1 and it was showing all method into logcat so i simply made your list static and now its work.

Please make your list static

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

Hope it help

As you saying your layout show both fragment at once. Its a issue of fragment add or replace so as per your code change this line

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

Into this

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

And for your second issue that the layout showing large margin please check your single row layout xml and make sure height is to wrap_content or post post your code here