I am unable to get data in my recycler view.I am very new to android and trying to implement this recycler view in tablayout but not getting any data .There are three tab and trying this code with one tab.Please help me out with this.Thanks in advance.
My code is below.
I have created two xml file one for recyler view and another one for custom rows.
Below here is my activity code.
My Fragment Activity
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private vivzadapter adapter;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView= (RecyclerView)layout.findViewById(R.id.lis);
adapter = new vivzadapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data =new ArrayList<>();
int icons=(R.drawable.icon);
String[] titles = {"Ashish", "shivam", "yogesh", "ravi"};
for (int i=0;i<titles.length;i++) {
Information information = new Information();
information.iconId=icons;
information.title=titles[i];
data.add(information);
}
return data;
}
}
My Adapter Activity-
public class vivzadapter extends RecyclerView.Adapter<vivzadapter.MyViewHolder>{
private final LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public vivzadapter(Context context,List<Information> data){
inflater =LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= inflater.inflate(R.layout.customrow, parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder ;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconId);
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title =(TextView) itemView.findViewById(R.id.textvw);
icon =(ImageView) itemView.findViewById(R.id.imageView);
}
}
}
Change
#Override
public int getItemCount() {
return 0;
}
to:
#Override
public int getItemCount() {
return data.size();
}
Related
I'm trying to start a new Fragment that shows the details of the item that was clicked on in the RecyclerView. The RecyclerView is on a Fragments. I've tried this in Activities and it worked perfectly but I'm trying to convert it to use on Fragments. And Also, how to detect other gestures in the RecyclerView (swipe, etc...)
My Adapter :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final ArrayList<Tip> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewType;
//TextView textViewDetail;
ImageView imageViewImage;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewTitle = itemView.findViewById(R.id.title);
this.textViewType = itemView.findViewById(R.id.type);
//this.textViewDetail = itemView.findViewById(R.id.detail);
this.imageViewImage = itemView.findViewById(R.id.itemImage);
}
}
public MyAdapter(ArrayList<Tip> data) {
this.dataSet = data;
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item, parent, false);
view.setOnClickListener(HomeFragment.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int listPosition) {
TextView textViewTitle = holder.textViewTitle;
TextView textViewType = holder.textViewType;
//TextView textViewDetail = holder.textViewDetail;
ImageView imageViewImage = holder.imageViewImage;
textViewTitle.setText( dataSet.get(listPosition).getTitle() );
textViewType.setText( dataSet.get(listPosition).getType() );
//textViewDetail.setText( dataSet.get(listPosition).getDetail() );
imageViewImage.setImageResource( dataSet.get(listPosition).getImage() );
}
#Override
public int getItemCount() {
return dataSet.size();
}
}
My Fragment :
public class HomeFragment extends Fragment {
public static RecyclerView.Adapter myAdapter;
private static RecyclerView.LayoutManager myLayoutManager;
private static RecyclerView myRecyclerView;
public static View.OnClickListener myOnClickListener;
private static ArrayList<Tip> myData;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Get the integer from the integers.xml resource file
Resources res = getResources();
int gridColumnCount = res.getInteger(R.integer.grid_column_count);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
// Add the following lines to create RecyclerView
myRecyclerView = view.findViewById(R.id.my_recycler_view);
myRecyclerView.setHasFixedSize(true);
//myLayoutManager = new LinearLayoutManager( view.getContext() );
myLayoutManager = new GridLayoutManager( view.getContext(),gridColumnCount );
myRecyclerView.setLayoutManager( myLayoutManager );
myRecyclerView.setItemAnimator( new DefaultItemAnimator() );
myData = new ArrayList<Tip>();
for (int i = 0; i < MyData.tipTitleArray.length; i++) {
myData.add(new Tip( MyData.tipTitleArray[i], MyData.tipTypeArray[i], MyData.tipDetailArray[i], MyData.id_[i], MyData.tipImageArray[i] ));
}
myAdapter = new MyAdapter(myData);
myRecyclerView.setAdapter(myAdapter);
//On retourne la View
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
fr.cnam.smb116.mimi_tips_navdraw.databinding.FragmentHomeBinding binding = null;
}
}
Don't use static listener and recyclerview instance.
Better to add callBack function to handle this.
e.g
interface RecyclerViewItemClickListener<T> {
void onClick(int position, T data);
}
init property in your adapter
RecyclerViewItemClickListener<Tip> listener
constructor of your Adapter
MyAdapter(RecyclerViewItemClickListener<Tip> listener) {
this. listener = listener
}
in
onBindViewHolder() {
holder.setOnClickListener(v -> {
listener.onClick(position, dataSet.get(listPosition));
})
}
In Your Fragment
Implement this interface
public class HomeFragment extends Fragment implements RecyclerViewItemClickListener<Tip> {
}
your adapter instance
myAdapter = new MyAdapter(myData,this);
implement onClick Method in your fragment
void onClick(int position, Tip tip){
// your listener
}
My Recycler view in image and name which is contained in ArrayList it not show particular like ArrayList in containing all image and name and the last image and name show "ArrayList.size" times like I enter four images and four names so last input image and last input name are shown in recycler view four times.
fragment.java class:
public class Revfragment extends Fragment {
public Revfragment() {
// Required empty public constructor
}
ArrayList<gettersetter> arrayList = new ArrayList<>();
CustomeAepter customeAepter;
RecyclerView recyclerView;
String name[] = {"kishan","karan","jigar","raman"};
int img[] = {R.drawable.a,R.drawable.ad,R.drawable.ac,R.drawable.ab};
RecyclerView.LayoutManager manager;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataset();
}
private void initDataset() {
for (int i = 0; i < name.length; i++) {
gettersetter gettersetter = new gettersetter(img[i], name[i]);
arrayList.add(gettersetter);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_revfragment, container, false);
recyclerView = rootview.findViewById(R.id.revs);
manager = new LinearLayoutManager(getActivity());
customeAepter = new CustomeAepter(getActivity(),arrayList);
recyclerView.setAdapter(customeAepter);
recyclerView.setLayoutManager(manager);
return rootview;
}
}
Adapter class
public class CustomeAepter extends RecyclerView.Adapter<CustomeAepter.myclass> {
TextView textView;
ImageView imageView;
ArrayList<gettersetter> arrayList;
Context context;
public CustomeAepter(Context cotexts,ArrayList<gettersetter> arrayLists) {
arrayList = arrayLists;
context = cotexts;
}
#NonNull
#Override
public CustomeAepter.myclass onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.textitem,parent,false);
return new myclass(v);
}
#Override
public void onBindViewHolder(#NonNull CustomeAepter.myclass holder, int position) {
holder.getTextView().setText(gettersetter.getName());
holder.getImageView().setImageResource(gettersetter.getImg());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class myclass extends RecyclerView.ViewHolder {
public myclass(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txt);
imageView = itemView.findViewById(R.id.img);
}
public TextView getTextView() {
return textView;
}
public ImageView getImageView()
{
return imageView;
}
}
}
You are not using the arraylist in your Adapter. You have to set the value according to the item at corresponding adapter position. Change you onBindViewHolder as follows:
#Override
public void onBindViewHolder(#NonNull CustomeAepter.myclass holder, int position) {
holder.getTextView().setText(arrayList.get(position).getName());
holder.getImageView().setImageResource(arrayList.get(position).getImg());
}
By this your data will be set according to the items at positions in the list you have passed to the adapter.
i am trying to add a recyclerview on a fragment
fragments code:
public class MediaPlayerController extends Fragment{
private static final String TAG = "MediaPlayerController";
private RecyclerView recyclerViewb;
private RecycleViewAdapter myAdapter;
private ArrayList<String> myRecordings = new ArrayList<>();
//private DBHelper dbHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: started");
View v = inflater.inflate(R.layout.media_player_area, container, false);
recyclerViewb = (RecyclerView)v.findViewById(R.id.recyclerViewXml);
//dbHelper = new DBHelper(v.getContext(), null, null, 1);
myRecordings.add("hau");
myRecordings.add("hau2");
myRecordings.add("hau3");
myRecordings.add("hau4");
myRecordings.add("ha5");
myRecordings.add("hau23");
myRecordings.add("ha31u");
myRecordings.add("haudsa");
System.out.println("what does this print?"+myRecordings);
myAdapter = new RecycleViewAdapter(v.getContext(), myRecordings);
recyclerViewb.setAdapter(myAdapter);
recyclerViewb.setLayoutManager(new LinearLayoutManager(v.getContext()));
Log.d(TAG, "onCreateView: returned");
return v;
}
in the logs i see that that it doesn't even get to the RecycleViewAdapter sections of code at all the fragment is completely blank
i get not errors as well
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private static final String TAG = "RecycleViewAdapter";
private Context mContext;
private ArrayList<String> mRecName = new ArrayList<>();
public RecycleViewAdapter(Context mContext, ArrayList<String> mRecName) {
this.mContext = mContext;
this.mRecName = mRecName;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: i am here");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_recycle, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.recTxt.setText(mRecName.get(position).toString());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "sas gamaw", Toast.LENGTH_LONG);
}
});
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RelativeLayout parentLayout;
TextView recTxt;
public ViewHolder(View itemView) {
super(itemView);
recTxt = itemView.findViewById(R.id.recTextView);
parentLayout = itemView.findViewById(R.id.parentLayoutXml);
}
}
}
in the logs it only displays oncreateView started and the myRecordings arraylist
onCreateView: started
I/System.out: what does this print?[hau, hau2, hau3, hau4, ha5,
hau23, ha31u, haudsa]
MediaPlayerController: onCreateView: returned
i can't think of what is going wrong :/ please help out
i also want to add that i am fairly new to android studio thanks
your code problem it is in Recycler Adapter in getItemCount() method,
You don't return any Item , you must change it to :
#Override
public int getItemCount() {
return mRecName.size();
}
When I try Recyclerview With Fragments its getting error Fragments With RecyclerView not working. Its getting Error No layout manager attached. Skipping entry.
My FrgamentList.java
public class FragmentList extends Fragment {
String [] names={"Nitin","Manish","Ankit","Hardik","Mayur","Mayank"};
RecyclerView mRecyclerView;
Context context;
RecyclerAdapter mAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_list,container,false);
mRecyclerView= (RecyclerView)v.findViewById(R.id.recycler_nit);
LinearLayoutManager mLayout= new LinearLayoutManager(getActivity());
mLayout.setOrientation(LinearLayoutManager.VERTICAL);
mAdapter= new RecyclerAdapter(getActivity(),names);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
RecyclerAdapeter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
String[] names;
public RecyclerAdapter(Context context, String[] names) {
this.context = context;
this.names = names;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_list,parent,false);
vh=new ViewHolder(v);
return (ViewHolder) vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.txt.setText(names[position]);
}
#Override
public int getItemCount() {
return names.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt= (TextView) itemView.findViewById(R.id.name_listtext);
}
}
}
You should set your created layout manager to RecyclerView
mRecyclerView= (RecyclerView)v.findViewById(R.id.recycler_nit);
LinearLayoutManager mLayout= new LinearLayoutManager(getActivity());
mLayout.setOrientation(LinearLayoutManager.VERTICAL);
//add this line
mRecyclerView.setLayoutManager(mLayout);
You need to set a LayoutManager to recyclerView like this:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
or
mRecyclerView.setLayoutManager(mLayout);
I am trying to pull data from Parse.com cloud to my android application.This is the code for my custom adapter:
public class AdvertisingAdapter extends RecyclerView.Adapter<AdvertisingAdapter.TatsViewHolder> {
List<AdvertInfo> data = new ArrayList<>();
private LayoutInflater inflater;
public AdvertisingAdapter(Context context, List<AdvertInfo> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public TatsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.single_ad, parent, false);
TatsViewHolder holder = new TatsViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(TatsViewHolder holder, int position) {
AdvertInfo current = data.get(position);
holder.daPoster.setText(current.theadvertiser);
holder.daAdvert.setText(current.theadvert);
}
#Override
public int getItemCount() {
return data.size();
}
class TatsViewHolder extends RecyclerView.ViewHolder{
TextView daPoster;
TextView daAdvert;
public TatsViewHolder(View itemView) {
super(itemView);
daPoster = (TextView)itemView.findViewById(R.id.txtAdvertiser);
daAdvert = (TextView)itemView.findViewById(R.id.txtAdvert);
}
}
}
and the code for my member class
public class AdvertInfo {
public String theadvertiser;
public String theadvert;
}
then for my fragment where i am displaying the data here is the code
public class ViewAdverts extends android.support.v4.app.Fragment {
private RecyclerView myRecyclerView;
AdvertisingAdapter myAdapter;
public ViewAdverts() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.view_adverts, container, false);
myRecyclerView = (RecyclerView)layout.findViewById(R.id.advert_list);
myAdapter = new AdvertisingAdapter(getActivity(), getData());
myRecyclerView.setAdapter(myAdapter);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<AdvertInfo> getData(){
final List<AdvertInfo> myposts = new ArrayList<>();
ParseQuery<Posts> query = ParseQuery.getQuery(Posts.class);
query.findInBackground(new FindCallback<Posts>() {
#Override
public void done(List<Posts> list, ParseException e) {
if (e == null){
for (int i = 0; i < list.size(); i++){
AdvertInfo ad = new AdvertInfo();
ad.theadvertiser = list.get(i).getString(ParseConstants.ADVERTISER);
ad.theadvert = list.get(i).getString(ParseConstants.DESCRIPTION);
myposts.add(ad);
}
}
}
});
return myposts;
}
}
I have tried to debug by putting breaks on getData method in the fragment I am get the data but I dont know why my recycler view appearing empty when I run.