I am making 50 data appear in my code, but I am putting it in a recycler view in a fragment because I have a dropdown menu, but when I put the code to call everything I get these two lines of error:
package com.example.tallerof.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
ArrayList<String> ListadeDatos;
RecyclerView recyclerView;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
recyclerView=(RecyclerView) findViewById(R.id.Recycler1); /* Error #1 Cannot resolve method findViewById in GalleryFragment*/
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); /* Error #2 required type: context provide: Gallery Fragment*/
ListadeDatos=new ArrayList<String>();
for(int i=0; i<=50; i++){
ListadeDatos.add("Dato # "+i+"");
}
UsuariosAdapter adapter= new UsuariosAdapter(ListadeDatos);
recyclerView.setAdapter(adapter);
return null;
}
}
you must to do this.
recyclerView = getActivity().findViewbyId(R.id.Recycler1);
Regards.
Related
I have been using the fragments(Java). The list is showing in the RecyclerView but when I swipe left or right, it is not getting detected.
I do not know Kotlin. Need help in Java implementation.
Bible - Chapter view
The expected behavior is to go to the next chapter or previous chapter when I swipe left or right. Each chapter is a List.
I have tried with ViewPager2 but I am unable to understand how to use RecyclerView inside ViewPager2.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.handsoncloud.holybible.R;
import com.handsoncloud.holybible.adapters.RecyclerBibleAdapter;
import com.handsoncloud.holybible.ui.chapter.ChaptersFragment;
import com.handsoncloud.holybible.utils.Books;
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_bibleBooks);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecyclerBibleAdapter recyclerBibleAdapter = new RecyclerBibleAdapter(getContext(), Books.getBooks(), (book, position) -> {
ChaptersFragment.BOOK_NAME_ENGLISH = Books.booksInEnglish.get(position);
ChaptersFragment.BOOK_NAME_TELUGU = Books.booksInTelugu.get(position);
});
recyclerView.setAdapter(recyclerBibleAdapter);
return rootView;
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
}
I have an android app where I display a form with some Checkboxes.I want to query firestore on the basis of checked checkboxes. I'm unable to get the array/arraylist of checked checkboxes. For testing purposes I've added a textview . If my test works I want to set the text of textview using members of newly populated array/arraylist.
When I check any checkbox and click on the button,my app crashes. Suppose I checked box4. The condition
(item4.isChecked) should be true and hence item4 must be added to my arraylist whose size is now not equal to zero
if(item4.isChecked()){
testingArrayList.add("item4");
}
Why am I getting the error that length of array is zero?
Here is my code:
package com.example.XX;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldPath;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
public class todayQuestionFragment extends Fragment {
FirebaseFirestore db = FirebaseFirestore.getInstance();
CheckBox item1;
CheckBox item2;
CheckBox item3;
CheckBox item4;
CheckBox item5;
CheckBox item6;
CheckBox item7;
CheckBox item8;
CheckBox item9;
CheckBox item10;
Button button;
ProgressBar progressBar;
TextView textView;//just for testing
public todayQuestionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_today_question, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
item1=view.findViewById(R.id.checkBox_item1);
item2=view.findViewById(R.id.checkBox_item2);
item3=view.findViewById(R.id.checkBox_item3);
item4=view.findViewById(R.id.checkBox_item4);
item5=view.findViewById(R.id.checkBox_item5);
item6=view.findViewById(R.id.checkBox_item6);
item7=view.findViewById(R.id.checkBox_item7);
item8=view.findViewById(R.id.checkBox_item8);
item9=view.findViewById(R.id.checkBox_item9);
item10=view.findViewById(R.id.checkBox_item10);
textView=view.findViewById(R.id.testingCheckBox);
Button button=view.findViewById(R.id.toMainQueryButton);
final NavController navController= Navigation.findNavController(view);
final ArrayList<String> testingArrayList = new ArrayList<String>();
if(item1.isChecked()){
testingArrayList.add("item1");
}
if(item2.isChecked()){
testingArrayList.add("item2");
}if(item3.isChecked()){
testingArrayList.add("item3");
}if(item4.isChecked()){
testingArrayList.add("item4");
}
if(item5.isChecked()){
testingArrayList.add("item5");
}
if(item6.isChecked()){
testingArrayList.add("item6");
}
if(item7.isChecked()){
testingArrayList.add("item7");
}if(item8.isChecked()){
testingArrayList.add("item8");
}if(item9.isChecked()){
testingArrayList.add("item9");
}
if(item10.isChecked()){
testingArrayList.add("item10");
}
final String[] testArray = new String[testingArrayList.size()];
for(int j =0;j<testingArrayList.size();j++){
testArray[j] = testingArrayList.get(j);
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(testArray[0]);
}
});
}
}
Logcat:
2020-06-12 14:48:10.772 4622-4622/com.example.XX E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.XX, PID: 4622
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.XX.todayQuestionFragment$1.onClick(todayQuestionFragment.java:140)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27582)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Found my error. All my logic with if conditions and arraylist/array needs to be added INSIDE the onClickListener of my button
I am trying to create edittext using Java in bottom fragment class based on input that is to be passed from top fragment. But when I type
Button add_submit = new Button(this);
I get error for the this for parameter. However I can use this code in MainActivity.java.
Why is this so? What is causing the error and how to fix it?
The following are the complete code for the class
package com.test.gpacalc;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class AddActivityBottom extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_bottom,container,false);
return view;
}
public void createAddInput(String number_of_subjects){
Button add_submit = new Button(this);
}
}
Button constructor requires context as an argument. Fragment doesn't implement it, activity does. try new Button(getActivity())
Im trying to put cards in one of the fragment but I got this error: There is no applicable constuctor to '(com.sample.app.FragmentOne)'. By the way Im using AIDE IDE.
The error is the (this) here:
mLayoutManager = new LinearLayoutManager(this);
FragmentOne.java
package com.sample.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import com.sample.app.MainActivity;
import com.sample.app.R;
public class FragmentOne extends Fragment {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_one, container, false);
((MainActivity) getActivity()).getSupportActionBar().setTitle("Fragment One");
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CardAdapter();
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
Please help me, thanks in advance.
I changed this
mLayoutManager = new LinearLayoutManager(this);
Into
mLayoutManager = new LinearLayoutManager(getActivity());
Change that this to getActivity()
For Activity, use something like (MainActivity.this);
For Fragments, use getActivity(); or requirActivity();
When I click at the menu items, then layouts not coming to view.
And MainActicity wanna "menu1_Fragment.java" encode with "android.support.v4.app.Fragment"
İf I encode only "Fragment", MainActivity is getting error.
In compatible types.
Required :android.support.v4.app.Fragment
Found :intizamyazilim.navigationdrawernew.menu1_Fragment
Here is menu1_Fragment.java
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 01.03.2015.
*/
public class menu1_Fragment extends android.support.v4.app.Fragment {
View rootview;
#Nullable
// #Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
}
I Fix it.
I changed this "menu1_Fragment.java" with this codes:
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_Fragment extends android.support.v4.app.Fragment {
public menu1_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu1_layout, container, false);
return rootView;
}
}
No it is not the proper way to fix it. The proble here is that you are extending your menu1_Fragment using support library and on the other hand you are importing fragment from SDK(import android.app.Fragment;). Make it consistent. Either use fragment from support or from android SDK.