Answer the question
In order to leave comments, you need to log in
Why is recyclerview not showing in fragment but working in Activity?
recyclerview is not showing in fragment, just empty fragment. Although everything works in a normal activity, the data is loaded from the database in the log, it is displayed that the data has been added, but is not displayed in the fragment ..
public class Income extends Fragment implements View.OnClickListener {
MyDatabaseHelper myDB;
TextView no_data;
ImageView empty_imageview;
Float translationY = 100f;
Boolean isMenuOpen = false;
OvershootInterpolator interpolator = new OvershootInterpolator();
private RecyclerView mRecyclerView;
CustomAdapter customAdapter;
ArrayList<String> id_txt,textName,textIncome,textExpenses,textTotal, textDate;
FloatingActionButton fab_menu, fab_reset, fab_scan, fab_add;
Activity activity;
Context context;
public Income() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.income_fragment, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.income_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(new CustomAdapter(getActivity(),getContext(),id_txt, textName, textIncome,
textExpenses,textTotal,textDate));
fab_menu = view.findViewById(R.id.fab_menu);
fab_scan = view.findViewById(R.id.fab_scan);
fab_reset = view.findViewById(R.id.fab_reset);
fab_add = view.findViewById(R.id.fab_add);
loadData();
initFabMenu();
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.fab_menu:
if (isMenuOpen) {
closeMenu();
} else {
openMenu();
}
break;
case R.id.fab_scan:
startActivity(new Intent(getActivity(), ScanActivity.class));
break;
case R.id.fab_add:
startActivity(new Intent(getActivity(),AddActivity.class));
break;
}
}
private void initFabMenu(){
fab_scan.setAlpha(0f);
fab_reset.setAlpha(0f);
fab_add.setAlpha(0f);
fab_menu.setOnClickListener(this);
fab_scan.setOnClickListener(this);
fab_reset.setOnClickListener(this);
fab_add.setOnClickListener(this);
fab_scan.setTranslationY(translationY);
fab_reset.setTranslationY(translationY);
fab_add.setTranslationY(translationY);
}
private void openMenu(){
isMenuOpen = !isMenuOpen;
fab_menu.animate().setInterpolator(interpolator).rotationBy(45f).setDuration(300).start();
fab_scan.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
fab_reset.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
fab_add.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
}
private void closeMenu(){
isMenuOpen = !isMenuOpen;
fab_menu.animate().setInterpolator(interpolator).rotationBy(0f).setDuration(300).start();
fab_scan.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
fab_reset.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
fab_add.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
}
private void initNoDataView(){
empty_imageview = view.findViewById(R.id.empty_imageview);
no_data = view.findViewById(R.id.no_data);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
recreate();
}
}
private void recreate() {
}
private void storeDataInArrays() {
Cursor cursor = myDB.readAllData();
if (cursor.getCount() == 0) {
empty_imageview.setVisibility(View.VISIBLE);
no_data.setVisibility(View.VISIBLE);
} else {
while (cursor.moveToNext()) {
id_txt.add(cursor.getString(0));
textName.add(cursor.getString(1));
textIncome.add(cursor.getString(2));
textDate.add(cursor.getString(3));
}
empty_imageview.setVisibility(View.GONE);
no_data.setVisibility(View.GONE);
}
}
private void loadData(){
myDB = new MyDatabaseHelper(getContext());
//myDB = new MyDatabaseHelper(MainActivity.this);
id_txt = new ArrayList<>();
textName = new ArrayList<>();
textIncome = new ArrayList<>();
textExpenses = new ArrayList<>();
textTotal = new ArrayList<>();
textDate = new ArrayList<>();
}
}
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private Activity activity;
private ArrayList id_txt, textName, textIncome, textExpenses,textTotal, textDate;
public CustomAdapter(Activity activity, Context context,ArrayList<String> id_txt, ArrayList<String> textName,
ArrayList<String> textIncome, ArrayList<String> textExpenses,
ArrayList<String> textTotal, ArrayList<String> textDate) {
this.activity = activity;
this.context = context;
this.id_txt = id_txt;
this.textName = textName;
this.textIncome = textIncome;
this.textExpenses = textExpenses;
this.textTotal = textTotal;
this.textDate = textDate;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.id_txt.setText(String.valueOf(id_txt.get(position)));
holder.textName.setText(String.valueOf(textName.get(position)));
holder.textIncome.setText(String.valueOf(textIncome.get(position)));
holder.textExpenses.setText(String.valueOf(textExpenses.get(position)));
holder.textTotal.setText(String.valueOf(textTotal.get(position)));
holder.textDate.setText(String.valueOf(textDate.get(position)));
//Recyclerview onClickListener
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(id_txt.get(position)));
intent.putExtra("name", String.valueOf(textName.get(position)));
intent.putExtra("income", String.valueOf(textIncome.get(position)));
intent.putExtra("expenses", String.valueOf(textExpenses.get(position)));
intent.putExtra("total", String.valueOf(textTotal.get(position)));
intent.putExtra("date", String.valueOf(textDate.get(position)));
activity.startActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
// return id_txt.size();
return (id_txt == null) ? 0: id_txt.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView id_txt, textName, textIncome, textExpenses,textTotal,textDate;
LinearLayout mainLayout;
MyViewHolder(@NonNull View itemView) {
super(itemView);
id_txt = itemView.findViewById(R.id.id_txt);
textName = itemView.findViewById(R.id.textName);
textIncome = itemView.findViewById(R.id.textIncome);
textExpenses = itemView.findViewById(R.id.textExpenses);
textTotal = itemView.findViewById(R.id.textTotal);
mainLayout = itemView.findViewById(R.id.mainLayout);
textDate = itemView.findViewById(R.id.textDate);
//Animate Recyclerview
Animation translate_anim = AnimationUtils.loadAnimation(context, R.anim.translate_anim);
mainLayout.setAnimation(translate_anim);
}
}
}
Answer the question
In order to leave comments, you need to log in
Run the debugger and see what the database returns and why the data in the adapter is not loaded.
storeDataInArrays is not called anywhere.
loadData doesn't actually do anything.
notify* methods on the adapter are not called.
In general, the point is not in the fragment, but in the fact that the code is not completed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question