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();
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 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.
I want to set wordtoSpan string at OnCreateView.
my code:
package com.tachles;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
return rootView;
horef = (TextView) findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
}
}
Eclipse says that I cant use findViewById, so I tried to insert getView() but still the same problem. Please show me my mistake
Thank you
If history_moadim_a is your layout containing the textView3, change your onCreateView() to something like
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
You want to find the TextView in the layout you just inflated (rootView), and return the layout at the end (and not in the middle of the method).
use
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
}
}
i can not see a return statement at onCreateView at perfect place so please do return rootView ; inside onCreateView at the end of the logics by means at the end of the onCreateView
I'm having trouble creating a Listview adapter, here's the code.
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Recipes extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView recipelist = (ListView) getView().findViewById(R.id.recipe_drawer);
String[] items ={ getResources().getString(R.string.block),
getResources().getString(R.string.tool),
getResources().getString(R.string.support),
getResources().getString(R.string.veggie)
};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);
recipelist.setAdapter(adapt);
View rootView = inflater.inflate(R.layout.recipes, container, false);
return rootView;
}
}
The error I get is in ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items); and it says
The constructor ArrayAdapter(Recipes,int,String[]); is undefined.
I tried changing Recipes.this to just this and I still get the same error. Any help would be appreciated!
Change Recipes.this to getActivity(). You are in a Fragment, which is not a Context, which is the first parameter to the ArrayAdapter family of constructors.
Replace this
ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);
By
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.recipes_list_item, items);
Use getActivity to get the activity context of the hosting activity
public final Activity getActivity ()
Added in API level 11
Return the Activity this fragment is currently associated with.
I have used actionbar and it is working fine. I have four fragment in the actionbar. I want to implement tabhost in one of the fragment. I have used the following code.
package com.main.udebate;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Info extends Fragment {
public Info() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(Info.this, about.class);
View view = inflater.inflate(R.layout.info, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content");
tab.setContent(i);
mTabHost.addTab(tab);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
return view;
}
}
I am getting error on Intent i = new Intent(Info.this, about.class); line as The constructor Intent(Info, Class) is undefined.
About.java
package com.main.udebate;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
Can someone pls help me to set tabhost inside fragment.
Thanks,
try to use this
Intent i = new Intent(getActivity(), about.class);
instead of this line
Intent i = new Intent(Info.this, about.class);
As Info.this does not mean any context thats why you get "The constructor Intent(Info, Class) is undefined." this error.
In fragment where you use this reference you should instead use getActivity() reference