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.
Related
FolderListAdapter Code
public class FolderListAdapter extends RecyclerView.Adapter<FolderListAdapter.myViewHolder> {
private final Context context;
private final ArrayList<Folder> folderList;
public FolderListAdapter(Context context, ArrayList<Folder> folderList) {
this.context = context;
this.folderList = folderList;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.folder_view, parent, false);
return new myViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull myViewHolder holder, int position) {
Folder folder = folderList.get(position);
Glide.with(context).load(folder.getFolder()).into(holder.imageView);
holder.folder_name.setText(folder.getFolder().getName());
holder.folder_item_count.setText(folder.getFilesList().size() + " Items");
}
#Override
public int getItemCount() {
return folderList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
private final ShapeableImageView imageView;
private final TextView folder_name;
private final TextView folder_item_count;
public myViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.folder_image_view);
folder_name = itemView.findViewById(R.id.folder_name_text_view);
folder_item_count = itemView.findViewById(R.id.folder_items_count_text_view);
}
}
}
MainActivity Code for RecyclerView
FolderListAdapter adapter = new FolderListAdapter(this, folderList);
RecyclerView recyclerView = binding.folderRecyclerView;
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
XML CODE OF recycler view
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/folder_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/folder_view" />
folder_view.xml code.
<?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">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/folder_image_view"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="?android:attr/selectableItemBackground"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerSize50Percent"
tools:srcCompat="#tools:sample/backgrounds/scenic" />
<TextView
android:id="#+id/folder_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Camera"
android:textColor="#android:color/primary_text_light"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/folder_image_view"
app:layout_constraintStart_toStartOf="#+id/folder_image_view"
app:layout_constraintTop_toBottomOf="#+id/folder_image_view" />
<TextView
android:id="#+id/folder_items_count_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="600 Items"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="#+id/folder_name_text_view"
app:layout_constraintTop_toBottomOf="#+id/folder_name_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_folder.xml layout file
<?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=".ui.addFiles.FoldersActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/folder_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
The size of folderList is 2
I also check the size of adapter. It is also 2
I also tried by making a new Adapter but it also does not work.
I don't know where I am making mistake and why it is now showing nothing in activity.
Change 1 :
In the xml set attribute android:layout_width="0dp" to android:layout_width="match_parent" and android:layout_height="0dp" to android:layout_height="match_parent"
Change 2 :
In your MainActivity code set LayoutManager to the RecyclerView before setting up adapter. so the code goes this way :
//Defining RecyclerView and setting LayoutManager
RecyclerView recyclerView = binding.folderRecyclerView;
FolderListAdapter adapter = new FolderListAdapter(this, folderList);
//Setting up Adapter
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(adapter);
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 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
Before marking as duplicate et al, viewpager from recyclerview is kind of triccky. The code is able to retrieve data from db and display it in a recyclerview with cardview. I want to be able to scroll the images in the viewpager while in the recyclerview as shown in the wireframe below, the dots represent number of images retrieved from DB which should be swipeeable right or left. Imageview seems not to be swipeable.
IMAGE ADAPTER.JAVA
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
List<ImageList>imageLists;
Context context;
public ImageAdapter(List<ImageList> imageLists, Context context) {
this.imageLists = imageLists;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.image_list,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ImageList imageList = imageLists.get(position);
holder.tvname.setText(imageList.getName());
holder.tv2name.setText(imageList.getName2());
String picture = imageList.getImageurl();
String picture2 = imageList.getImage2url();
String picture3 = imageList.getImage3url();
String[] imageUrls = {picture, picture2, picture3};
Picasso.get()
.load(picture)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(0, 200)
.into(holder.img);
}
#Override
public int getItemCount() {
return imageLists.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvname, tv2name;
private ImageView img;
public ViewHolder(View itemView) {
super(itemView);
img=(ImageView)itemView.findViewById(R.id.image_view);
tvname=(TextView)itemView.findViewById(R.id.txt_name);
tvname=(TextView)itemView.findViewById(R.id.txt_name2);
}
}
}
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
android:padding="2dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="ServerImg"
android:scaleType="fitXY" />
</androidx.cardview.widget.CardView>
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I'm struggling to find a way to add a native ad within my app on Android.
MainActivity Class:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
final String[] numbers = {"one","two", "", "three", "four", "five","six","seven" "eight","nine", "ten" "eleven","twelve", "thirteen"};
Integer[] images = {0,1,2,3,4,5,6,7,8,9,10,11,12,13
};
CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), mobileArray, images);
NativeExpressAdView adView = (NativeExpressAdView) rootView.findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
list=(ListView) rootView.findViewById(R.id.list_view);
list.setAdapter(adapter);
CustomListAdapter Class:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] numbers;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] numbers, Integer[] imgid) {
super(context, R.layout.listv, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.numbers=numbers;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.listv, null,true);
NativeExpressAdView adView = (NativeExpressAdView) rowView.findViewById(R.id.adView);
TextView txtTitle = (TextView) rowView.findViewById(R.id.number);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgid);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(numbers[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Number: "numbers[position]);
return rowView;
};
}
I want to add the native add within a particular index in array, ideally where it shows "". I'm trying to add the adView into the adapter but unsure how to do this.
Edit:
listv layout:
<?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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgid"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4CBE99" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_gravity="center"
ads:adSize="345x80"
ads:adUnitId="ca-app-pub-3940256099942544/2793859312"></com.google.android.gms.ads.NativeExpressAdView>
You can try like this,
public class CustomListAdapter extends ArrayAdapter<String> {
private NativeExpressAdView adView;
private NativeExpressAdView getAddView() {
if (adView != null) {
return adView;
}
adView = new NativeExpressAdView(context);
adView.setAdUnitId("[your unit id]");
adView.setAdSize(new AdSize(AdSize.FULL_WIDTH, 80));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adView.setLayoutParams(layoutParams);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
return adView;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.layout_add, null, true);
// NativeExpressAdView adView = (NativeExpressAdView) rowView.findViewById(R.id.adView);
LinearLayout linearLayout = (LinearLayout) rowView.findViewById(R.id.addView);
linearLayout.removeAllViews();
TextView txtTitle = (TextView) rowView.findViewById(R.id.number);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgid);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(numbers[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Number: " + numbers[position]);
if (position == 2) {
linearLayout.addView(getAddView());
linearLayout.setVisibility(View.VISIBLE);
} else {
linearLayout.removeAllViews();
linearLayout.setVisibility(View.GONE);
}
return rowView;
}
;
}
Layour xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgid"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4CBE99"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/addView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>