Items not showing in recycler view - java

I want to wrap my recycler view inside a coordinator layout but the problem is whenever i do that the items stop showing up. For items i use autogenerated dummy text from android studio. And if i only use a recylcer view without coordinatorlayout all the items show up perfectly.
Thanks for your help in advance.
p.s im kinda new to asking questions on stackoverflow so i apologize for any formatting issues
fragment.java
public class TodoFragment extends Fragment {
private static final String ARG_COLUMN_COUNT = "column-count";
private OnListFragmentInteractionListener mListener;
public TodoFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
final FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(new ToDoRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnListFragmentInteractionListener {
void onListFragmentInteraction(DummyItem item);
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.example.kayc.projectplanner.aktivnostiFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="com.example.kayc.projectplanner.FloatButtonBehavior.RVlayoutmanager"
tools:context=".fragmenti.TodoFragment"
tools:listitem="#layout/fragment_todo" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_menu_add" />
</android.support.design.widget.CoordinatorLayout>
Adapter.java
public class ToDoRecyclerViewAdapter extends RecyclerView.Adapter<ToDoRecyclerViewAdapter.ViewHolder> {
private final List<DummyItem> mValues;
private final OnListFragmentInteractionListener mListener;
public ToDoRecyclerViewAdapter(List<DummyItem> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_todo, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null != mListener) {
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
#Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}

The problem is in your onCreateView. it should be like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
final FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(new ToDoRecyclerViewAdapter(DummyContent.ITEMS, mListener));
return view;
}
if (view instanceof RecyclerView) will always return false because view is a ConstraintLayout instance.

if (view instanceof RecyclerView) never execute. cause view's root is
CoordinatorLayout not RecyclerView. So just remove the if(view instanceof RecyclerView).
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.example.kayc.projectplanner.aktivnostiFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".fragmenti.TodoFragment"
tools:listitem="#layout/fragment_todo" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_menu_add" />
</android.support.design.widget.CoordinatorLayout>
And onCreateView().
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
final FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(new ToDoRecyclerViewAdapter(DummyContent.ITEMS, mListener));
return view;
}
Also look for onActivityCreated() cause getActivity() can return null in onCreateView().

Related

Fragment with RecyclerView, no adapter attached error

I'm making a tabbed activity with different fragments and one of them contains a recyclerview with just some names at the moment. I have no idea what I've done wrong, I get a warning every time on starting the app that says no adapter attached, skipping layout. I've created and set the adapter to the recyclerview in my fragment, created an adapter with viewholder, and created a class to set the name on the recyclerview.
Here is my fragment class:
public class fragment_main extends Fragment {
View view;
private RecyclerView mainrecyclerview;
private List<MainSchedule> listStr;
public fragment_main() {}
#NonNull
#Override
public View onCreateView(LayoutInflater inflater, #NonNull ViewGroup container, #NonNull Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mainrecyclerview.setAdapter(mainAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listStr = new ArrayList<>();
listStr.add(new MainSchedule("one"));
listStr.add(new MainSchedule("two"));
listStr.add(new MainSchedule("three"));
}
}
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment_main">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/todayRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="250dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerviewadapter class:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
Context mContext;
List<MainSchedule> mData;
public MainRecyclerViewAdapter(Context context, List<MainSchedule> data){
this.mContext = context;
this.mData = data;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.mainschedule_cards, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mData.get(position).getName());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textViewClass);
}
}
}
I've tried many ways of creating the adapter and binding the layout manager to the recyclerview, but nothing seems to work and I get the same error each time.
Problem seems with onCreateView because you're supposed to return view
Replace your fragment_main with code below
public class fragment_main extends Fragment {
private RecyclerView mainrecyclerview;
private List<MainSchedule> listStr;
public fragment_main() {
}
#NonNull
#Override
public View onCreateView(LayoutInflater inflater, #NonNull ViewGroup container, #NonNull Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listStr = new ArrayList<>();
listStr.add(new MainSchedule("one"));
listStr.add(new MainSchedule("two"));
listStr.add(new MainSchedule("three"));
mainrecyclerview = (RecyclerView) view.findViewById(R.id.todayRecyclerView);
MainRecyclerViewAdapter mainAdapter = new MainRecyclerViewAdapter(getActivity(), listStr);
mainrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mainrecyclerview.setAdapter(mainAdapter);
}
}

How to create selectable tags with RecyclerView?

I want to create tags using RecyclerView that can be selected to produce results.
What I want to achieve is when I tap on Books TextView on the top-right must change to Books immediately. In my case when I tap on Books it just stays in Art and it replaces Art with Books only when I replace my fragments. I also highlight buttons when clicked (changing border from grey to black) but after changing fragments highlights return back to Art again. Shortly I want to highlight button when clicked and change TextView content based on clicked button text. I partly achieved it by using interfacews but didn't get what I wanted. I provided codes below:
TrendCategoryTagsAdapter.java:
public class TrendCategoryTagsAdapter extends FirestoreRecyclerAdapter<CategorySelection, TrendCategoryTagsAdapter.TrendCategoryTagsHolder> {
Context context;
onCategoryTagClicked onCategoryTagClicked;
int row_index;
public TrendCategoryTagsAdapter(#NonNull FirestoreRecyclerOptions<CategorySelection> options, Context context, com.rajabmammadli.paragrafredesign.Interface.onCategoryTagClicked onCategoryTagClicked) {
super(options);
this.context = context;
this.onCategoryTagClicked = onCategoryTagClicked;
}
#Override
protected void onBindViewHolder(#NonNull final TrendCategoryTagsHolder holder, final int position, #NonNull CategorySelection model) {
holder.categoryNameText.setText(model.getCategoryName());
holder.categoryNameContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.black_rounded_bg));
onCategoryTagClicked.onTagClick(holder.categoryNameText.getText().toString());
} else {
holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.grey_rounded_bg));
}
}
#NonNull
#Override
public TrendCategoryTagsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.trendcategory_cell, parent, false);
return new TrendCategoryTagsAdapter.TrendCategoryTagsHolder(v);
}
public static class TrendCategoryTagsHolder extends RecyclerView.ViewHolder {
RelativeLayout categoryNameContainer;
TextView categoryNameText;
public TrendCategoryTagsHolder(#NonNull View itemView) {
super(itemView);
categoryNameContainer = itemView.findViewById(R.id.categoryNameContainer);
categoryNameText = itemView.findViewById(R.id.categoryNameText);
}
}
}
trendcategory_cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">
<RelativeLayout
android:id="#+id/categoryNameContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/grey_rounded_bg">
<TextView
android:id="#+id/categoryNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/gilroyregular"
android:padding="15dp"
android:text="categoryname"
android:textColor="#android:color/black" />
</RelativeLayout>
</RelativeLayout>
TrendingFragment.java:
public class TrendingFragment extends Fragment implements onCategoryTagClicked {
RecyclerView trendingCategoryRV;
TextView noPostTV, selectedCategoryText;
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference categoryRef;
String selectedCategory = "Art";
TrendCategoryTagsAdapter trendCategoryTagsAdapter;
public TrendingFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_trending, container, false);
trendPostRV = view.findViewById(R.id.trendPostRV);
trendingCategoryRV = view.findViewById(R.id.trendingCategoryRV);
noPostTV = view.findViewById(R.id.noPostTV);
selectedCategoryText = view.findViewById(R.id.selectedCategoryText);
selectedCategoryText.setText(selectedCategory);
setUpTrendCategoryTagsRV();
setUpTrendingPostRV();
// Inflate the layout for this fragment
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
trendCategoryTagsAdapter.startListening();
}
#Override
public void onDestroyView() {
super.onDestroyView();
trendCategoryTagsAdapter.stopListening();
}
private void setUpTrendCategoryTagsRV() {
categoryRef = db.collection("Categories");
Query query = categoryRef.orderBy("categoryName", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<CategorySelection> options = new FirestoreRecyclerOptions.Builder<CategorySelection>()
.setQuery(query, CategorySelection.class)
.build();
trendCategoryTagsAdapter = new TrendCategoryTagsAdapter(options, getContext(), this);
trendingCategoryRV.setNestedScrollingEnabled(false);
final LinearLayoutManager trendingTagsLM = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
trendingCategoryRV.setLayoutManager(trendingTagsLM);
trendingCategoryRV.setAdapter(trendCategoryTagsAdapter);
trendCategoryTagsAdapter.notifyDataSetChanged();
}
#Override
public void onTagClick(String categoryName) {
selectedCategory = categoryName;
}
}
Any help will be appreciated. Thanks in advance

RecyclerView onClickItem is not responding to the click

I want to implement an onClickListener for a RecyclerViewItem in my App. My problem is, that the onclick listener isn't being called. Not even in the adapter. I'm trying to get this to work for 5 hours now and I'm really frustrated and don't know how to fix this/proceed.
I hope you can help me.
ItemListAdapter.class
public class ItemListAdapter extends RecyclerView.Adapter<ItemListAdapter.ItemViewHolder> implements Filterable {
private ArrayList<Item> mItemList;
private ArrayList<Item> mItemListFiltered;
private ItemsListAdapterListener mItemsListAdapterListener;
public ItemListAdapter(ArrayList<Item> itemArrayList, ItemsListAdapterListener itemsListAdapterListener) {
mItemList = itemArrayList;
mItemListFiltered = itemArrayList;
this.mItemsListAdapterListener = itemsListAdapterListener;
}
public interface ItemsListAdapterListener {
void onItemClick(int position);
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView mImageView;
public TextView mTextView;
ItemsListAdapterListener mItemsListAdapterListener;
public ItemViewHolder(#NonNull View itemView, ItemsListAdapterListener itemsListAdapterListener) {
super(itemView);
mItemsListAdapterListener = itemsListAdapterListener;
mImageView = itemView.findViewById(R.id.imageViewItem);
mTextView = itemView.findViewById(R.id.textViewItem);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mItemsListAdapterListener.onItemClick(getAdapterPosition());
Log.i("test", "Adapter click");
}
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_view_layout, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view, mItemsListAdapterListener);
return itemViewHolder;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item item = mItemListFiltered.get(position);
holder.mImageView.setImageResource(item.getImgURL());
holder.mTextView.setText(item.getItemName());
}
#Override
public int getItemCount() {
return mItemListFiltered.size();
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mItemListFiltered = mItemList;
} else {
ArrayList<Item> filteredList = new ArrayList<>();
for (Item row: mItemList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getItemName().toLowerCase().contains(charString.toLowerCase()) || row.getItemCategory().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
mItemListFiltered = (ArrayList<Item>) filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mItemListFiltered;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mItemListFiltered = (ArrayList<Item>) filterResults.values;
notifyDataSetChanged();
}
};
}
}
ItemsFragment.class
public class ItemsFragment extends Fragment implements TextWatcher, ItemListAdapter.ItemsListAdapterListener{
private ItemsViewModel itemsViewModel;
private RecyclerView mRecyclerView;
private ItemListAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public View onCreateView(#NonNull final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
itemsViewModel =
ViewModelProviders.of(this).get(ItemsViewModel.class);
View root = inflater.inflate(R.layout.fragment_items, container, false);
final EditText editTextItemSearch;
editTextItemSearch = (EditText) root.findViewById(R.id.editTextItemSearch);
editTextItemSearch.addTextChangedListener(this);
final ArrayList<Item> itemArrayList = new ArrayList<>();
itemArrayList.add(new Item("TestItem1", R.drawable.unbenannt, getString(R.string.item_parts)));
itemArrayList.add(new Item("TestItem2", R.drawable.ic_home_black_24dp, getString(R.string.item_tools)));
itemArrayList.add(new Item("TestItem3", R.drawable.ic_dashboard_black_24dp, getString(R.string.item_consumable)));
itemArrayList.add(new Item("test123", R.drawable.ic_notifications_black_24dp, getString(R.string.item_interactive)));
itemArrayList.add(new Item("test12567687863", R.drawable.ic_notifications_black_24dp,getString(R.string.item_blocks)));
Log.i("test", "items added");
mRecyclerView = root.findViewById(R.id.recylcerViewItems);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new ItemListAdapter(itemArrayList, this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return root;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mAdapter.getFilter().filter(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void onItemClick(int position) {
Log.i("test", "item clicked");
}
}
adapter_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="8dp"
android:backgroundTint="#color/colorBackroundBright"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="15dp"
tools:layout_editor_absoluteX="144dp"
tools:layout_editor_absoluteY="42dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewItem"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="16dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:fontFamily="#font/nunito_regular"
android:text="ItemName"
android:textColor="#959595"
app:layout_constraintBottom_toBottomOf="#+id/imageViewItem"
app:layout_constraintStart_toEndOf="#+id/imageViewItem"
app:layout_constraintTop_toTopOf="#+id/imageViewItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Try removing android:clickable="true" from your CardView.
I think what is happening is you are clicking on the CardView, which is stealing the click so the root view does not get it.
Try this:
In ItemViewHolder add a View object and set it to itemView:
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView mImageView;
public TextView mTextView;
//add here
public View view;
ItemsListAdapterListener mItemsListAdapterListener;
public ItemViewHolder(#NonNull View itemView, ItemsListAdapterListener itemsListAdapterListener) {
super(itemView);
//set here
view = itemView;
.........
.........
Now set the onClickListener in onBindViewHolder:
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item item = mItemListFiltered.get(position);
holder.mImageView.setImageResource(item.getImgURL());
holder.mTextView.setText(item.getItemName());
//here
holder.view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
mItemsListAdapterListener.onItemClick(position);
}
});
}
UPDATE:
Remove this from cardView xml:
android:clickable="true"

RecyclerView Overriden methods not called and getItemCount always return 0

I know that my question has been asked several times but i have been searching for a while and none of all the answers fixed my problem.
The issue is that when i create the recyclerview adapter and all the stuff in the method "onViewCreated" in my "HomeFragment" i am not having any compilation error but my recyclerview doesn't load any image since none of the methods Overriden in recyclerview adapter are called even when i call "mAdapter.notifyDataSetChanged()"
HomeFragment.java
public class HomeFragment extends Fragment implements Response.Listener<GalleryItem[]>, Response.ErrorListener {
RequestQueue requestQueue;
List<GalleryItem> myDataset = new ArrayList<>();
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
CustomJsonObjectRequest jsonObjectRequest;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
mRecyclerView = view.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mAdapter = new GalleryAdapter(myDataset);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(mAdapter);
jsonObjectRequest = new CustomJsonObjectRequest(Constants.HOME_URL, GalleryItem[].class, this, this);
requestQueue = VolleySingleton.getInstance(this.getContext()).getRequestQueue();
VolleySingleton.getInstance(this.getContext()).addToRequestQueue(jsonObjectRequest, "headerRequest");
}
#Override
public void onResponse(GalleryItem[] response) {
myDataset = Arrays.asList(response);
mAdapter.notifyDataSetChanged();
}
#Override
public void onErrorResponse(VolleyError error) {
Log.d(Constants.API_ERROR_TAG, error.getMessage());
}
GalleryAdapter.java
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<GalleryItem> mDataset;
public static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
Log.d("tagdetest2","myviewholder");
imageView = itemView.findViewById(R.id.gallery_image);
}
}
public GalleryAdapter(List<GalleryItem> myDataset) {
mDataset = myDataset;
}
#NonNull
#Override
public GalleryAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
Log.d("tagdetest2","onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gallery, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull GalleryAdapter.MyViewHolder myViewHolder, int i) {
Log.d("tagdetest2", mDataset.get(i).link);
Glide.with(myViewHolder.itemView.getContext()).load(mDataset.get(i).link).into(myViewHolder.imageView);
}
#Override
public int getItemCount() {
Log.d("tagdetest", "size = " + mDataset.size());
return mDataset.size();
}
item_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/gallery_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
myDataset = Arrays.asList(response);
overrides the reference, in your Fragment not in your adapter.
Try with
mDataset.addAll(Arrays.asList(response));
insetad

Button from Fragment is Crashing the app

I put a Button in fragment with the corresponding listener. Every time I call on this activity the app crashes. Logcat show a null pointer exception. The code below is a snippet from the Fragment class.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyCarProfileRecyclerViewAdapter(carProfileContent.ITEMS, mListener));
}
Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
imgbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getActivity(),ReserveActivity.class));
}
});
return view;
}
Logcat shows:
FATAL EXCEPTION: main
Process: com.example.app, PID: 13571
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.CarProfileListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mikepuno.parkeazy.CarProfileFragment.onCreateView(CarProfileFragment.java:78)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5353)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
I've been looking at multiple similar issues here but it just don't seem to work for me. Am I missing anything from the way I added the Button and the listener?
fragment_carprofile_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:name="com.mikepuno.parkeazy.CarProfileFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.mikepuno.parkeazy.CarProfileFragment"
tools:listitem="#layout/fragment_carprofile" />
fragment_carprofile.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/plateNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/brandTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="#+id/modelTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="#+id/colorTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<Button
android:id="#+id/editCarProfileButton"
android:layout_width="64dp"
android:layout_height="64dp"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
Adapter Class
public class MyCarProfileRecyclerViewAdapter extends RecyclerView.Adapter<MyCarProfileRecyclerViewAdapter.ViewHolder> {
private final List<CarProfile> mValues;
private final OnListFragmentInteractionListener mListener;
public MyCarProfileRecyclerViewAdapter(List<CarProfile> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_carprofile, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mPlateNumberTextView.setText(mValues.get(position).getPlateNumber());
holder.mBrandTextView.setText(mValues.get(position).getBrand());
holder.mModelTextView.setText(mValues.get(position).getModel());
holder.mColorTextView.setText(mValues.get(position).getColor());
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mPlateNumberTextView;
public final TextView mBrandTextView;
public final TextView mModelTextView;
public final TextView mColorTextView;
public CarProfile mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mPlateNumberTextView = (TextView) view.findViewById(R.id.plateNumberTextView);
mBrandTextView = (TextView) view.findViewById(R.id.brandTextView);
mModelTextView = (TextView) view.findViewById(R.id.modelTextView);
mColorTextView = (TextView) view.findViewById(R.id.colorTextView);
}
#Override
public String toString() {
return super.toString() + " '" + mPlateNumberTextView.getText() + "'";
}
}
}
After you inflate your view
View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);
you assume that view is RecyclerView. That means root element is recyclerview.
Then trying to find a button inside of that recyclerview by doing
Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
which returns null ofcourse. Then trying to set a click listener on null object.
Looks like your xml file is wrong. If your each row has button that you are trying to find, you need to catch it in your adapter class then notify fragment to do your process.
SOLUTION (EDIT):
Remove these lines from onCreateView in Fragment.
Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
imgbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getActivity(),ReserveActivity.class));
}
});
You have already a interface that notify the fragment.
So that we are going to use it for editing.
So the final adapter class should be like
public class MyCarProfileRecyclerViewAdapter extends RecyclerView.Adapter<MyCarProfileRecyclerViewAdapter.ViewHolder> {
private final List<CarProfile> mValues;
private final OnListFragmentInteractionListener mListener;
public MyCarProfileRecyclerViewAdapter(List<CarProfile> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_carprofile, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mPlateNumberTextView.setText(mValues.get(position).getPlateNumber());
holder.mBrandTextView.setText(mValues.get(position).getBrand());
holder.mModelTextView.setText(mValues.get(position).getModel());
holder.mColorTextView.setText(mValues.get(position).getColor());
}
#Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public final TextView mPlateNumberTextView;
public final TextView mBrandTextView;
public final TextView mModelTextView;
public final TextView mColorTextView;
public CarProfile mItem;
public ViewHolder(View view) {
super(view);
view.setClickable(true); // This is important
view.setOnClickListener(this);
mPlateNumberTextView = (TextView) view.findViewById(R.id.plateNumberTextView);
mBrandTextView = (TextView) view.findViewById(R.id.brandTextView);
mModelTextView = (TextView) view.findViewById(R.id.modelTextView);
mColorTextView = (TextView) view.findViewById(R.id.colorTextView);
}
public void onClick(View view) {
if (null != mListener) {
mListener.onListFragmentInteraction(mValues.get(getAdapterPosition()));
}
}
#Override
public String toString() {
return super.toString() + " '" + mPlateNumberTextView.getText() + "'";
}
}
Then your fragment
#Override
public void onListFragmentInteraction(CarProfile profile) {
// TODO Pass your profile to ReserverActivity by using bundles.
startActivity(new Intent(getActivity(), ReserveActivity.class));
}
You can improve the code by notify fragment on your button click. So your button click listener and notify code must also be in here.
View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);
You are inflating fragment_carprofile_list.xml, which contains the RecyclerView. There is no mention of Button in that XML, which is why you are getting a NullPointerException

Categories

Resources