Why won't my CardView appear in my fragment? - java

I am following this tutorial on how to create Swipe Views using a View Pager and am trying to convert it to work with fragments.
My issue is that the cards are not appearing when I select my fragment.
I think the issue lies in this line of code
adapter = new Adapter(models, getActivity());
(originally adapter = new Adapter(models, this); in the tutorial), but I could be wrong.
I am relatively new to android studio and this is my first time using fragments, any help would be greatly appreciated!
Here is my code:
fragment_cards.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"
android:background="#android:color/background_light"
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:foregroundGravity="center"
android:overScrollMode="never"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"></androidx.viewpager.widget.ViewPager>
<Button
android:id="#+id/button_shuffle"
android:text="SHUFFLE"
android:textColor="#fff"
android:background="#drawable/round"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_marginBottom="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:layout_constraintBottom_toBottomOf="#+id/viewPager"
app:layout_constraintEnd_toEndOf="#+id/viewPager"
app:layout_constraintStart_toStartOf="#+id/viewPager">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
item.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="match_parent"
android:background="#color/colorPrimary">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="#+id/image"
android:src="#drawable/brochure"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:id="#+id/title"
android:textColor="#262626"
android:layout_below="#id/image"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:text="Brochure"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/desc"
android:layout_below="#id/title"
android:layout_marginTop="3dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:maxLines="3"
android:drawablePadding="10dp"
android:ellipsize="end"
android:text="Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
CardsFragment.java
package com.example.musicassistant;
import android.animation.ArgbEvaluator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class CardsFragment extends Fragment {
ViewPager viewPager;
Adapter adapter;
List<Model> models;
Integer[] colors = null;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
models = new ArrayList<>();
models.add(new Model(R.drawable.brochure, "Brochure", "oooooo"));
models.add(new Model(R.drawable.sticker, "Sticker", "oooooo"));
models.add(new Model(R.drawable.poster, "Poster", "oooooo"));
models.add(new Model(R.drawable.namecard, "Namecard", "oooooo"));
adapter = new Adapter(models, getActivity());
viewPager = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
Integer[] colors_temp = {
getResources().getColor(R.color.color1),
getResources().getColor(R.color.color2),
getResources().getColor(R.color.color3),
getResources().getColor(R.color.color4)
};
colors = colors_temp;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position < (adapter.getCount() -1) && position < (colors.length -1)) {
viewPager.setBackgroundColor(
(Integer) argbEvaluator.evaluate(
positionOffset,
colors[position],
colors[position + 1]
)
);
} else {
viewPager.setBackgroundColor(colors[colors.length - 1]);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return inflater.inflate(R.layout.fragment_cards, container, false);
}
}
Adapter.java
package com.example.musicassistant;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class Adapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}
#Override
public int getCount() {
return models.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.item, container, false);
ImageView imageView;
TextView title, desc;
imageView = view.findViewById(R.id.image);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.desc);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDesc());
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
}
Model.java
package com.example.musicassistant;
public class Model {
private int image;
private String title;
private String desc;
public Model(int image, String title, String desc) {
this.image = image;
this.title = title;
this.desc = desc;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
It is worth noting that the fragment itself is working, as the button in it is appearing correctly.
The only issue is that my cards are not showing up at all.
Any help would be greatly appreciated as this is driving me absolutely insane!
Thank you very much for your time I greatly appreciate it,
Roman

Inflate will create a new layout object(view object) eveytime you use it so this is one view layout
viewPager = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
and you are returning a new view object which has nothing
return inflater.inflate(R.layout.fragment_cards, container, false);
so you need to store a single reference of view after inflating then use it and return it, as
View root = inflater.inflate(R.layout.fragment_cards, container, false).findViewById(R.id.viewPager);
viewPager = root.findViewById(R.id.viewPager);
// ... code
return root;

Related

Intro slide helper by viewPager

i wanted to use intro slider to my App
i learned it by this video https://www.youtube.com/watch?v=byLKoPgB7yA&t=22s
I do like video tutorial but I have a problem with dots
when I slide to any page the dots(. ) will be copy and increase
where is my problem and what should I do ?
package time.one.just.show.introslyder;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ViewPager mSlideViewPager;
private LinearLayout mDotsLayout;
private SlyderAdapter slyderAdapter;
//dots of any Slide pages
private TextView[] mDots;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSlideViewPager = findViewById(R.id.viewPager);
mDotsLayout = findViewById(R.id.dots);
slyderAdapter = new SlyderAdapter(this);
mSlideViewPager.setAdapter(slyderAdapter);
addDotsIndiccator(0);
mSlideViewPager.addOnPageChangeListener(viewListener);
}
private void addDotsIndiccator(int position) {
mDots = new TextView[3];
for (int i = 0; i < mDots.length; i++) {
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("&#8226"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.colorWhiteNearToGray));
mDotsLayout.addView(mDots[i]);
}
if (mDots.length > 0) {
mDots[position].setTextColor(getResources().getColor(R.color.colorWite));
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i1) {
}
#Override
public void onPageSelected(int i) {
addDotsIndiccator(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
};}
And this is my SideAdapter class
package time.one.just.show.introslyder;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlyderAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SlyderAdapter(Context context) {
this.context = context;
}
public int[] slide_imagesArray = {
R.drawable.eat,
R.drawable.men,
R.drawable.sleep};
public String[] slide_headerArray = {
"EAT", "men", "code"};
public String[] slide_descriptionArray = {
"this is 1st", "this is 2nd", "this is 3rd"
};
#Override
public int getCount() {
return slide_headerArray.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layoout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slideImage);
TextView slideheader = (TextView) view.findViewById(R.id.slideheader);
TextView slidedescription = (TextView) view.findViewById(R.id.slideDescription);
slideImageView.setImageResource(slide_imagesArray[position]);
slideheader.setText(slide_headerArray[position]);
slidedescription.setText(slide_descriptionArray[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((RelativeLayout) object);
}
}
and this is my SliderLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">
<ImageView
android:id="#+id/slideImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"
app:srcCompat="#drawable/eat" />
<TextView
android:id="#+id/slideheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="122dp"
android:layout_marginBottom="287dp"
android:text="بدون نیاز به اینترنت"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginBottom="171dp"
android:text="آرشیو کامل این خواننده محبوب همیشه در جیب شم ، ، هر کدام ز آهنگ ها را خواستید می توانید دانلود کنید و هر زمان دلتون خوست به آن ها گوش بدهید حتی بدون نیز به اینترنت" />
</RelativeLayout>
and activtymain xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/main_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
I found a solution
just by add this inside my App
mDotsLayout.removeAllViews();
thank you all

How to make custom adapter?

I'm trying to make custom adapter for my small app, I got stuck in filling my rows with data, I can't find a good tutorial for that. I know how to use Simple Adapter and Array Adapter, but custom ones have some trouble.
I don't know how to implement it, and the online articles confused me, Please help me with the logic and what steps should I do implement.
package com.example.administrator.healthyfood;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] food = {"Dog","Cat","Cow","Fish","Frog","Bird","Rabbit","Horse","Chikcen"};
#Override
protected void onCreate(Bundle savedInstanceState) {
list = (ListView)findViewById(R.id.listView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class MyListViewAdapter extends BaseAdapter {
#Override
public int getCount() {
return food.lenght();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
}
activity_main.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
custom.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
Your MainActivity.java -
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
Hope this is enough for you.
You can create a simple Adapter class using these simple steps
make a java class
public class Wallet_Adapter extends RecyclerView.Adapter<Wallet_Adapter.ViewHolder>
{
LayoutInflater inflater;
List<Wallet_Model> modelclasslists;
public Wallet_Adapter(Context ctx, List<Wallet_Model> modelclasslists)
{
this.inflater=LayoutInflater.from(ctx);
this.modelclasslists=modelclasslists;
}
#NonNull
#Override
public Wallet_Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.layout_wallet,parent,false);
return new Wallet_Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Wallet_Adapter.ViewHolder holder, int position) {
holder.date.setText(modelclasslists.get(position).getDate());
holder.amount.setText(modelclasslists.get(position).getAmount());
holder.description.setText(modelclasslists.get(position).getDescription());
holder.type.setText(modelclasslists.get(position).getType());
}
#Override
public int getItemCount() {return modelclasslists.size(); }
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView date,amount,description,type;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
date=itemView.findViewById(R.id.dateans);
amount=itemView.findViewById(R.id.amountans);
description=itemView.findViewById(R.id.descriptionans);
type=itemView.findViewById(R.id.typeans);
}
}
}

java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v7.widget.CardView

I am making a project for the first, I followed a Youtube tutorial on RecycleView but when I compile my project and launch the app on Android Studio my app crashes. I followed exactly what the video showed but it's possible that some components are not working the same way since.
This is the Output I get :
java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v7.widget.CardView
at e.user.popcorn.RecyclerViewAdapter$MyViewHolder.<init>(RecyclerViewAdapter.java:78)
at e.user.popcorn.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:33)
at e.user.popcorn.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:16)
And this is the code
RecyclerViewAdapter.java
package e.user.popcorn;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext ;
private List<movie_page> mData ;
public RecyclerViewAdapter(Context mContext, List<movie_page> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.layout_boxoffice_movie,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_movie_title.setText(mData.get(position).getTitle());
holder.img_movie_thumbnail.setImageResource(mData.get(position).getThumbnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,SelectedMovie.class);
// passing data to the book activity
intent.putExtra("Title",mData.get(position).getTitle());
intent.putExtra("Description",mData.get(position).getDescription());
intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
intent.putExtra("Categorie",mData.get(position).getCategory());
// start the activity
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_movie_title;
ImageView img_movie_thumbnail;
CardView cardView ;
public MyViewHolder(View itemView) {
super(itemView);
tv_movie_title = (TextView) itemView.findViewById(R.id.movie_title) ;
img_movie_thumbnail = (ImageView) itemView.findViewById(R.id.movie_poster);
cardView = (CardView) itemView.findViewById(R.id.movie_id);
}
}
}
And there is movie_page.java
package e.user.popcorn;
public class movie_page {
private String Title;
private String Category ;
private String Description ;
private int Thumbnail ;
public movie_page() {
}
public movie_page(String title, String category, String description, int thumbnail) {
Title = title;
Category = category;
Description = description;
Thumbnail = thumbnail;
}
public String getTitle() {
return Title;
}
public String getCategory() {
return Category;
}
public String getDescription() {
return Description;
}
public int getThumbnail() {
return Thumbnail;
}
public void setTitle(String title) {
Title = title;
}
public void setCategory(String category) {
Category = category;
}
public void setDescription(String description) {
Description = description;
}
public void setThumbnail(int thumbnail) {
Thumbnail = thumbnail;
}
}
Here is Layout_boxoffice_movie.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/movie_id"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_margin="5dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<ImageView
android:id="#+id/movie_poster"
android:layout_width="match_parent"
android:layout_height="170dp"
android:scaleType="centerCrop"
android:background="#color/colorNavBackground"/>
<TextView
android:id="#+id/movie_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Movie Title"
android:gravity="center"
android:textColor="#color/colorNavBackground"/>
</LinearLayout>
You need to replace ConstraintLayout to CardView.
This will be your corrected code .
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/movie_id"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_margin="5dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<ImageView
android:id="#+id/movie_poster"
android:layout_width="match_parent"
android:layout_height="170dp"
android:scaleType="centerCrop"
android:background="#color/colorNavBackground"/>
<TextView
android:id="#+id/movie_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Movie Title"
android:gravity="center"
android:textColor="#color/colorNavBackground"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You care casting ConstraintLayout to CardView.
Change this line
<android.support.constraint.ConstraintLayout
with
android.support.v7.widget.CardView
Problem with below line
cardView = (CardView) itemView.findViewById(R.id.movie_id);
As per your layout xml file, You are using ConstraintLayout whereas You are using CardView in Your Java file. This is the real problem. You need to use CardView instead of ConstraintLayout.
Replace
<android.support.constraint.ConstraintLayout
With
<android.support.v7.widget.CardView
Also take care at closing tags

custom listview with images

Hello guys im new to android dev and im having an unknown error with my code,i finished the code but when i run it sends me back to "dataprovider". can anyone please help me?
My fragment.
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Places_Fragment extends ListFragment {
ListView listView;
int[] images ={R.drawable.cake_1,R.drawable.cake_2,R.drawable.cake_3,R.drawable.cake_4,R.drawable.cake_5,R.drawable.cake_6};
String[] places;
PlaceAdapter adapter;
public static Places_Fragment newInstance() {
Places_Fragment fragment = new Places_Fragment();
return fragment;
}
public Places_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_places_, container, false);
listView =(ListView)rootView .findViewById(R.id.listView);
places = getResources().getStringArray(R.array.places);
int i=0;
adapter = new PlaceAdapter(getActivity(),R.layout.row_layout);
listView.setAdapter(adapter);
for(String item: places)
{
PlaceDataProvider dataProvider = new PlaceDataProvider(images[i],places[i]);
adapter.add(dataProvider);
i++;
}
return rootView;
}
}
My custom Adapter.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bonfire-ADv on 4/23/2016.
*/
public class PlaceAdapter extends ArrayAdapter {
List list =new ArrayList();
public PlaceAdapter(Context context, int resource) {
super(context, resource);
}
static class DataHandler
{
ImageView images;
TextView Places;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView== null)
{
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate (R.layout.row_layout, parent, false);
handler = new DataHandler();
handler.images = (ImageView) row.findViewById(R.id.image_view);
handler.Places = (TextView) row.findViewById(R.id.place_title);
row.setTag(handler);
}
else
{
handler = (DataHandler) row.getTag();
}
PlaceDataProvider dataProvider;
dataProvider = (PlaceDataProvider) this.getItem(position);
handler.images.setImageResource(dataProvider.getImages());
handler.Places.setText(dataProvider.getPlaces());
return row;
}
}
My dataprovider.
package com.selfstudios.cakeplanet;
public class PlaceDataProvider {
private int images;
private String places;
public PlaceDataProvider(int images,String places)
{
this.setImages(images);
this.setPlaces(places);
}
public int getImages() {
return images;
}
public void setImages(int images) {
this.images = images;
}
public String getPlaces() {
return places;
}
public void setPlaces(String places) {
this.places = places;
}
}
my frag xml.
<LinearLayout 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="com.selfstudios.cakeplanet.Places_Fragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_weight="1" />
</LinearLayout>
my row xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp">
<ImageView
android:layout_width="100dp"
android:layout_height="75dp"
android:id="#+id/image_view"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="2dp"
android:paddingLeft="2dp"
android:paddingEnd="2dp"
android:src="#drawable/cake_1"
/>
<TextView
android:id="#+id/place_title"
android:layout_width="175dp"
android:layout_height="75dp"
android:text="place name"
android:gravity="center"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/image_view">
</View>
</RelativeLayout>
Try replacing
for(String item: places)
with the more traditional
for(int i; i < places.size(); i++

StaggeredGrid RecyclerView not showing anything

I'm trying to inflate a RecyclerView which has as StaggeredGrid Layout, but it is not showing anything. I've pretty much copied previous code I've used before for the RecyclerView so I'm kind of stumped.
In MuseumStoriesViewHolder.onCreateViewHolder() the return of holder has the following value ViewHolder{337ec22b position=-1 id=-1, oldPos=-1, pLpos:-1 unboundundefined adapter position no parent} I'm not sure if this is realated, but it was something that seemed off to me.
It also might help to know that the fragment I'm inflating this RecyclerView is a nested Fragment.
Any help would be greatly appreciated.
MuseumFragment
package com.example.android.radiobuttontestproject.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.adapters.MuseumStoriesAdapter;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import com.example.android.radiobuttontestproject.test.SampleDataFactory;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MuseumFragment extends Fragment {
private List<StoryObject> storyObjectList;
private StaggeredGridLayoutManager storyGridLayoutManager;
private MuseumStoriesAdapter storyAdapter;
#Bind(R.id.stories_recycler_view) RecyclerView storiesRecyclerView;
public static MuseumFragment newInstance() {
MuseumFragment fragment = new MuseumFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public MuseumFragment() {
// Required empty public constructor
}
#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_museum, container, false);
ButterKnife.bind(this, view);
//Sets up the stories
SampleDataFactory sampleDataFactory = new SampleDataFactory();
storyObjectList = sampleDataFactory.getSampleStories(
getResources().getStringArray(R.array.test_titles_for_grid_museum1),
getResources().getStringArray(R.array.test_desc_for_grid_museum1));
storyGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
storiesRecyclerView.setLayoutManager(storyGridLayoutManager);
storyAdapter = new MuseumStoriesAdapter(getActivity().getApplicationContext(), storyObjectList);
storiesRecyclerView.setAdapter(storyAdapter);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
MuseumStoriesAdapter
package com.example.android.radiobuttontestproject.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import java.util.List;
public class MuseumStoriesAdapter extends RecyclerView.Adapter<MuseumStoriesAdapter.MuseumStoriesViewHolder> {
private List<StoryObject> itemList;
private LayoutInflater inflater;
private Context context;
public MuseumStoriesAdapter(Context context, List<StoryObject> itemList) {
this.itemList = itemList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public MuseumStoriesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = inflater.inflate(R.layout.view_box_small, viewGroup, false);
MuseumStoriesViewHolder holder = new MuseumStoriesViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MuseumStoriesViewHolder holder, int position) {
holder.title.setText(itemList.get(position).getTitle());
holder.desc.setText(itemList.get(position).getDescription());
}
#Override
public int getItemCount() {
return itemList.size();
}
class MuseumStoriesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView type,title,desc;
public MuseumStoriesViewHolder(View itemView) {
super(itemView);
//Tried Butterknife, but it doesn't seem like it was working in the view holder. - Peter
type = (TextView) itemView.findViewById(R.id.small_box_type);
title = (TextView) itemView.findViewById(R.id.small_box_title);
desc = (TextView) itemView.findViewById(R.id.small_box_desc);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
}
fragment_museum.xml
<ScrollView
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="com.example.android.radiobuttontestproject.fragments.MuseumFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:id="#+id/museum_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/header_margin"
android:gravity="center"
android:textSize="#dimen/font_larger"
android:text="#string/museum_header" />
<android.support.v7.widget.RecyclerView
android:id="#+id/stories_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
view_box_small.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_box_margin"
android:background="#color/small_box_background_color">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--TODO Make layout_height wrap contenet -->
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#color/test_color2"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/abc_btn_rating_star_off_mtrl_alpha"
/>
</FrameLayout>
<TextView
android:id="#+id/small_box_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_small"
android:textColor="#color/font_red"
android:text="Object"
/>
<TextView
android:id="#+id/small_box_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_large"
android:textColor="#color/font_black"
android:text="Sample Text Here"
/>
<TextView
android:id="#+id/small_box_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_normal"
android:textColor="#color/font_black"
android:textStyle="italic"
android:text="Sample Text Here"
/>
</LinearLayout>

Categories

Resources