I keep getting the error "E/RecyclerView: No adapter attached; skipping layout" when the RecyclerView list is shown. I'm getting data correctly from API but I got "void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
I know this question has been asked a lot before but I checked most of the questions and none has worked for me.
This is my fragment:
public class HomeFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
private ArrayList<Result> arrayList;
private SwipeRefreshLayout refreshLayout;
private PostAdapter postAdapter;
private MaterialToolbar toolbar;
private SharedPreferences userPref;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_home,container,false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
userPref = getContext().getApplicationContext().getSharedPreferences("user", Context.MODE_PRIVATE);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerHome);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
refreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipeHome);
toolbar =(MaterialToolbar) view.findViewById(R.id.toolbarHome);
((HomeActivity)getContext()).setSupportActionBar(toolbar);
getResults();
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
getResults();
}
});
}
private void getResults() {//Working Fine}
}
My adapter:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostsHolder>{
private ArrayList<Result> list;
private Context context;
public PostAdapter(Context context, ArrayList<Result> list) {
this.list = new ArrayList<Result>(list);
this.context = context;
}
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home, parent, false);
return new PostsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PostsHolder holder, int position) {
Result result = list.get(position);
holder.imgPostDate.setText(result.getDate());
}
#Override
public int getItemCount() {
return list.size();
}
public static class PostsHolder extends RecyclerView.ViewHolder{
private TextView imgPostProfile,imgPostName,imgPostDate;
private ImageButton btnPostOption;
public PostsHolder(#NonNull View itemView) {
super(itemView);
imgPostProfile =(TextView) itemView.findViewById(R.id.imgPostProfile);
imgPostName = (TextView)itemView.findViewById(R.id.imgPostName);
imgPostDate =(TextView) itemView.findViewById(R.id.imgPostDate);
btnPostOption =(ImageButton) itemView.findViewById(R.id.btnPostOption);
}
}
}
I modified my code according to the answers I found to similar questions but none of them worked.
layout_home
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarHome"
android:elevation="1dp"
android:theme="#style/Theme.MaCoSISBottomAppBar"
app:title=""
tools:targetApi="lollipop">
<!-- <ImageView-->
<!-- android:layout_width="100dp"-->
<!-- android:layout_height="40dp"-->
<!-- android:src="#drawable/logo_dark"/>-->
</com.google.android.material.appbar.MaterialToolbar>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/swipeHome"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerHome"
android:layout_height="match_parent"
android:layout_width="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
layout_post
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/imgPostProfile"
android:layout_width="60sp"
android:layout_height="60sp"
android:background="#drawable/shape_circle"
android:text="MC"
android:gravity="center"
android:textColor="#color/colorWhite"
android:textSize="14sp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="6dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgPostName"
android:text="BBAD1252 Social Harmony and Business Skills Development"
android:textColor="#color/colorBlack"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgPostDate"
android:text="12 Novermber 2020"
android:textColor="#color/colorLightGrey"
android:textSize="12dp" />
</LinearLayout>
<ImageButton
android:id="#+id/btnPostOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_baseline_more_vert_24"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/colorLightGrey"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
In onCreateViewHolder you inflated the wrong xml. It should've been R.layout.layout_post
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post, parent, false);
return new PostsHolder(view);
}
To prevent from error E/RecyclerView: No adapter attached; skipping layout, you should call recyclerView.setAdapter(adapter); inside your init(); method.
Then after you have your data you should notify the adapter in order to display your data by calling adapter.notifyDataSetChanged(); or using any other notify methods. Check this answer for other notify methods.
If your getResults() is where you're calling your recyclerview.setAdapter(), you're calling setAdapter too late. You need to make sure your adapter is set during onCreateView() so that the Recyclerview isn't being skipped while drawing the layout.
No Adapter Attached; Skipping layout:
you should setadapter in your main thread methods of fragment; then add or update data to adapter
Nullpointer at settext [at adapter]: In onCreateViewHolder you inflated the wrong xml. It should be R.layout.layout_post instead of R.layout.layout_home
Related
I've put a RecyclerView in the 'Tutorial' activity in my application, to list tutorial versions, which come from the Room database my app has. It is hosted within a fragment, but that's of little consequence since I had the same problem before putting it in there. The RecyclerView adapter reaches onBindViewHolder() without any issues and I get logs from the Log.w I used for debugging there with correct data. The problem seems to be that Layout does not get inflated at all, and the buttons that each row is supposed to output never appear.
THE ADAPTER
public class VersionListAdapter extends RecyclerView.Adapter<VersionListAdapter.VersionViewHolder> {
private List<Version> mVersionList;
private final LayoutInflater mInflater;
private final OnViewClickListener mListener;
public VersionListAdapter(Context context)
{
this.mInflater = LayoutInflater.from(context);
this.mVersionList = null;
this.mListener = (OnViewClickListener) context;
}
#NonNull
#Override
public VersionViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View row = mInflater.inflate(R.layout.row_versions_rv, viewGroup, false);
return new VersionViewHolder(row, mListener);
}
#Override
public void onBindViewHolder(#NonNull VersionViewHolder versionHolder, int rowNumber) {
versionHolder.thisVersion = mVersionList.get(rowNumber);
versionHolder.versionButton.setText((mVersionList.get(rowNumber).getText()));
Log.w("THIS IS A TAG", mVersionList.get(rowNumber).getText()); //correct output
}
#Override
public int getItemCount() {
if(mVersionList!=null) return mVersionList.size();
else return 0;
}
public void setElementList(List<Version> versions){
this.mVersionList = versions;
notifyDataSetChanged();
}
public static class VersionViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
OnViewClickListener listenerForThisRow;
Version thisVersion;
Button versionButton;
public VersionViewHolder(
#NonNull View viewForThisRow, OnViewClickListener listenerFromActivity)
{
super(viewForThisRow);
this.listenerForThisRow = listenerFromActivity;
versionButton = viewForThisRow.findViewById(R.id.version_button);
viewForThisRow.setOnClickListener(this);
}
#Override
public void onClick(View v){
listenerForThisRow.onViewClick(thisVersion);
}
}
public interface OnViewClickListener{
void onViewClick(Version version);
}
}
THE FRAGMENT CLASS WHICH HOSTS THE RECYCLER VIEW
public class VersionRecycler extends Fragment {
protected RecyclerView mRecycler;
protected VersionListAdapter mAdapter;
public VersionRecycler(){
super(R.layout.fragment_recycler_versions);
}
#Override
public View onCreateView(
#NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
) {
VersionViewModel mVersionViewModel = new VersionViewModel(requireActivity().getApplication());
long tutorialId = requireArguments().getLong("tutorialId");
View view = inflater.inflate(R.layout.fragment_recycler_versions, container, false);
mAdapter = new VersionListAdapter(requireActivity());
mRecycler = view.findViewById(R.id.versions_rv);
mRecycler.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));
mVersionViewModel.getByTutorialId(tutorialId).observe(requireActivity(), versions->mAdapter.setElementList(versions));
mRecycler.setAdapter(mAdapter);
return view;
}
}
HOW THE FRAGMENT IS EMBEDDED IN THE ACTIVITY
<LinearLayout
android:id="#+id/layout_versions_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
FRAGMENT LAYOUT
<?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=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
LAYOUT FOR RECYCLERVIEW ROW
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<Button
android:id="#+id/version_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You should also add Start and Bottom constraints:
<?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=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You could also change RecyclerView layout_height to wrap_content and add Start constraint only
I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item view
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.
I want to create dynamic views with x number of rows for each CardView that gets created. To help illustrate my idea this image shows what I essentially want to do.
For each CardView I want to be able to put any amount of TableRows into the CardView. So one CardView could have 4 rows whereas the next CardView could have 2 rows and so on.
EDIT: Implementation
Essentially this view is replicated several times, I would like to minimise having to write seperate layouts for each CardView with x amount of rows like in the Card Storage implemenation which is more reusable than what I currently have. As I hit the limit here, I have posted a pastebin of the current layout with rows that I have
https://pastebin.com/9RFDGhXv
Layout: Card Storage
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="#style/CardStyle">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/MainConstraints" >
<TableRow
android:id="#+id/row_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/card_title"
style="#style/Title"/>
</TableRow>
<TableRow
android:id="#+id/row_two"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#+id/row_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<ImageView
android:id="#+id/drawable"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
style="#style/RowDrawableElement"
/>
<TextView
android:id="#+id/percentage_descriptor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/drawable"
android:text="#string/percentage_descriptor"
style="#style/RowTextElement" />
<TextView
android:id="#+id/percentage_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
style="#style/RowTextElement" />
</android.support.constraint.ConstraintLayout>
</TableRow>
<TableRow
android:id="#+id/row_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_two"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
style="?android:attr/progressBarStyleHorizontal"
/>
</TableRow>
<TableRow
android:id="#+id/row_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_three"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/text_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<TextView
android:id="#+id/text_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
</TableRow>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Adapter
public class StorageAdapter extends RecyclerView.Adapter<StorageAdapter.MyViewHolder> {
private Context mContext;
private List<StorageObjects> mList;
public StorageAdapter(Context context, List<StorageObjects> list) {
this.mContext = context;
this.mList = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_storage, parent, false);
return new MyViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
StorageObjects storageObjects = mList.get(position);
long used = storageObjects.getUsed();
long total = storageObjects.getTotal();
holder.mProgress.setMax(0);
holder.mProgress.setProgress(0);
holder.mTitle.setText(storageObjects.getTitle());
MiscUtils.testDrawableIdentifier(mContext.getApplicationContext(), holder.mImageViews, storageObjects.getDrawables());
holder.mPercentage.setText(storageObjects.getPercentage() + mContext.getString(R.string.percentage));
holder.mProgress.setMax((int) (total/100000));
holder.mProgress.setProgress((int) (used/100000));
holder.mUsed.setText(MiscUtils.humanReadableByteSize(used));
holder.mTotal.setText(MiscUtils.humanReadableByteSize(total));
}
#Override
public int getItemCount() {
return mList.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.card_title) TextView mTitle;
#BindViews({R.id.drawable}) List<ImageView> mImageViews;
#BindView(R.id.percentage_text) TextView mPercentage;
#BindView(R.id.progress_bar) ProgressBar mProgress;
#BindView(R.id.text_used) TextView mUsed;
#BindView(R.id.text_total) TextView mTotal;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
Class Storage
public class Storage extends Fragment {
private Unbinder mUnbinder;
private List<StorageObjects> storageList;
private StorageAdapter adapter;
private RecyclerView.LayoutManager mLayoutManager;
#BindArray(R.array.storage_ic_images)
String mDrawables[];
#BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
public Storage() {
// Required empty public constructor
}
public static Storage newInstance() {
return new Storage();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_storage, container, false);
mUnbinder = ButterKnife.bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
initRecyclerView();
}
#Override
public void onDestroy() {
super.onDestroy();
mUnbinder.unbind();
}
private void initRecyclerView() {
storageList = new ArrayList<>();
adapter = new StorageAdapter(getActivity(), storageList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager = ViewUtils.getLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, ViewUtils.dpToPx(
getActivity(), 10), true));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
prepareView();
}
#SuppressLint("SetTextI18n")
private void prepareView(){
StorageObjects storage
= new StorageObjects(
getActivity().getString(R.string.int_storage_title),
StorageUtils.internalUsed(),
StorageUtils.internalTotal(),
StorageUtils.internalPercentage(),
mDrawables[0]);
storageList.add(storage);
storage = new StorageObjects (
getActivity().getString(R.string.ext_storage_title),
StorageUtils.externalUsed(getActivity()),
StorageUtils.externalTotal(getActivity()),
StorageUtils.externalPercentage(getActivity()),
mDrawables[1]
);
storageList.add(storage);
adapter.notifyDataSetChanged();
}
}
If number of rows per card view is less then you can inflate edit text and add it to linear layout.
Note : It's a bad practice to add scroll view inside a scroll view
My Fragment
public class PostFragment extends BaseFragment implements PostView {
#Inject
PostPresenter presenter;
private RecyclerView recyclerView;
PostAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_post, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.postFragmentRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String s = "word";
ArrayList<String> array = new ArrayList<>();
for(int i = 0; i != 20; i++) {
array.add(s);
}
adapter = new PostAdapter(array);
recyclerView.setAdapter(adapter);
return v;
}
Adapter
public class PostAdapter extends RecyclerView.Adapter {
private ArrayList<String> arrayList;
public PostAdapter(ArrayList<String> arrayList) {
Log.d("ADAPTER", "constructor");
this.arrayList = arrayList;
}
class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
MyViewHolder(View itemView) {
super(itemView);
this.view = itemView;
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("ADAPTER", "CREATE");
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.d("ADAPTER", "bind");
}
#Override
public int getItemCount() {
Log.d("ADAPTER", "size = " + String.valueOf(arrayList.size()));
return arrayList.size();
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="exp.glorio.view.fragments.PostFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/postFragmentRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
post_item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/postGroupLogo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"/>
<TextView
android:id="#+id/postGroupName"
android:layout_width="180dp"
android:layout_height="30dp"
android:textColor="#color/colorText"
android:textSize="12sp"
android:layout_marginLeft="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
<TextView
android:id="#+id/postText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/postAttachmentsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
and log:
03-17 17:11:23.753 30538-30538/exp.glorio D/ADAPTER: constructor
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20
I have many other adapters in this app and they work. In this case as u can see I don't forget to add LayoutManager and Recycler view not under ScrollView in xml. Constructor and getItemCount works (why 4 times ?) but onBindViewHolder and onCreateViewHolder no.
The problem was in root layout of activity. It was android.support.constraint.ConstraintLayout. In combination with parameter layout_height"match_parent" of container of fragment it's give that problem. If change ConstraintLayout to any other - it will work properly.
I'm in the process of learning android app development however I have come to an impasse. My RecyclerView is picking up how many items are in my String array however it is not showing the actual data in text1, any help would be much appreciated.
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
#Override
public int getItemCount() {
return fakeData.length;
}
This is the corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/event_view"
android:layout_marginTop="#dimen/task_card_half_spacing"
android:layout_marginBottom="#dimen/task_card_half_spacing"
android:layout_marginLeft="#dimen/gutter"
android:layout_marginRight="#dimen/gutter"
android:layout_gravity="center"
android:elevation="#dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium.Inverse"
android:id="#+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/TextAppearance.Inverse"
android:id="#+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="#+id/text1"
android:layout_below="#+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
Hm... everything looks ok, can you try setting this to it:
android:textColor="#000000"