Reputation: 512
I have implemented a recyclerview with some items (Downloaded from an API). when I run the app or go to this fragment I don't see any Items unless I start typing in the SearchView (the functioning of searchview and other things is ok)
Please find the Gif here
I am using Volley to download the data and Picasso to download images.
where may I be wrong?
note: It happened a few times that it showed the items naturally and without problems (but just like 3 times in 100 times)
here is my fragment.xml layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abed.whitelabel.Fragments.MainFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_view">
</android.support.v7.widget.SearchView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/content_employee_list"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
Main parts of my MainFragment.java class:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
downloadEmployeeData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
MainFragment.setMainFragment(this);
sv = (SearchView)view.findViewById(R.id.search_view);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.content_employee_list);
mAdapter = new EmployeeAdapter(mainFragment.getContext(), employees_Array);
//mAdapter = new EmployeeAdapter(this, employees_Array); this was the old one
recyclerView.setAdapter(mAdapter);
//LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(mainFragment.getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
//Events for searching through list
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return view;
}
public void downloadEmployeeData() {
// here I download the data and fill the array
}
public class EmployeeAdapter extends RecyclerView.Adapter<EmployeesDataViewHolder> implements Filterable {
Context c;
ArrayList<EmployeesInfo> mEmployeesInfo;
ArrayList<EmployeesInfo> currentList;
public EmployeeAdapter(Context c, ArrayList<EmployeesInfo> mEmployeesInfo){
this.c = c;
this.mEmployeesInfo = mEmployeesInfo;
this.currentList = mEmployeesInfo;
}
@Override
public void onBindViewHolder(EmployeesDataViewHolder holder, int position) {
final EmployeesInfo employee = this.mEmployeesInfo.get(position);
holder.updateUI(employee);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.loadDetailsFragment(employee);
}
});
}
@Override
public int getItemCount() {
return mEmployeesInfo.size();
}
@Override
public EmployeesDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_employees, parent, false);
//VolleyGet.getInstance(getA)
return new EmployeesDataViewHolder(card);
}
// FOR search stuff
@Override
public Filter getFilter() {
return FilterHelper.newInstance(currentList,this);
}
public void setEmployeesInfo(ArrayList<EmployeesInfo> filteredEmployees)
{
this.mEmployeesInfo = filteredEmployees;
}
}
And my DataViewHolder:
public class EmployeesDataViewHolder extends RecyclerView.ViewHolder{
private ImageView mainPhoto;
private TextView firstName;
private TextView lastName;
public EmployeesDataViewHolder(View itemView){
super(itemView);
mainPhoto = (ImageView)itemView.findViewById(R.id.list_photo);
firstName = (TextView)itemView.findViewById(R.id.list_firstName);
lastName = (TextView)itemView.findViewById(R.id.list_lastName);
}
public void updateUI(EmployeesInfo employee){
firstName.setText(employee.getFirstName());
lastName.setText(employee.getLastName());
Picasso.with(mainPhoto.getContext()).load(employee.getPhoto()).into(mainPhoto);
}
}
Upvotes: 0
Views: 607
Reputation: 2160
Add following line if not added after you download all employees data in downloadEmployeeData() function:
mAdapter.notifyDataSetChanged();
It must be you are not refreshing your adapter once data is added in your downloadEmployeeData function and when you click on Search your filter code is refreshing the data.
Upvotes: 0
Reputation: 2335
Try to update these much part in your code
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.content_employee_list);
//LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(mainFragment.getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new EmployeeAdapter(mainFragment.getContext(), employees_Array);
//mAdapter = new EmployeeAdapter(this, employees_Array); this was the old one
recyclerView.setAdapter(mAdapter);
Upvotes: 1