how to intent in recyclerview when we click on image [duplicate] - android

This question already has answers here:
RecyclerView onClick
(49 answers)
Closed 1 year ago.
So,basically in this as soon as I will click on the image view i should go on the next page that is firstpage .IN this for firstpage I have used recyclerview and for the introduction pge i have used gridadapter.but when I click on the imag view its not getting intented to the next page.I tried all possible sols. ut still the same.plzz help.
this is my introduction.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"
android:background="#drawable/ic_launcher_background"
tools:context=".MainActivity2">
<androidx.cardview.widget.CardView
android:layout_width="199dp"
android:layout_height="199dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="35dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="193dp"
android:layout_height="194dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="143dp"
android:layout_height="109dp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="16dp"
tools:srcCompat="#tools:sample/avatars"
android:onClick="image"/>
<TextView
android:id="#+id/textView15"
android:layout_width="158dp"
android:layout_height="33dp"
android:layout_marginTop="4dp"
android:text="text"
android:textColor="#color/DarkBlue"
android:textSize="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
this is my MainActivity2.java code
public abstract class MainActivity2 extends AppCompatActivity implements ClickedListener {
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(R.drawable.latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(MainActivity2.this, MainActivity1.class);
startActivity(intent);
}
}
this is my GridAdapter.java code
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
public GridAdapter(Context context,List<String>titles,List<Integer>images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
}
}
}
this is my firstpage.xml code
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#color/LightGrey"
android:layout_marginTop="5dp"
tools:context=".MainActivity1">
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
tools:srcCompat="#tools:sample/avatars" />
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="-10dp"
android:text="Description"
android:textSize="20sp">
</TextView>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Ratings"
android:textSize="20sp">
</TextView>
</LinearLayout>
</LinearLayout>

This interface will be implemented in class where your adapter is called.
I created an interface and implemented in MainActivity2, that interface has on override() method clickedListener. This clickedListener will be called whenever an image clicked and then the intent inside clickedListener will be called and you will be moved to MainActivity1.java.
This is your working code.
public interface ClickedListener {
void onPictureClicked();
}
GridAdapter.java
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
ClickedListener clickedListener;
public GridAdapter(Context context,List<String>titles,List<Integer>images, ClickedListener clickedListener ){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
this.clickedListener = clickedListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
images.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickedListener.onPictureClicked();
}
});
}
}
}
MainActivity2
public class MainActivity2 extends AppCompatActivity implements ClickedListener{
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(getApplicationContext(), MainActivity1.class);
startActivity(intent);
}
}
MainActivity1.java
public class MainActivity1 extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter MovieAdapter;
RecyclerView.LayoutManager layoutManager;
String[] movieNameList = {"BHOOT","PANGA","PSYCHO","JUMANJI","DIL BECHARA","BAAGHI 3","THE BRIDGE CURSE","LOST GIRLS","BAD BOYS FOR LIFE",
"THAPPAD","SADAK 2","TANHAJI","BHEESHMA" };
String[] movieDescriptionList = {"Horror/Mystery","Sport/Drama","Thriller/Psychological Thriller","Comedy/Action","Romance/Drama","Action/Thriller",
"Horror/Thriller","Mystery/Drama","Action/Comedy","Drama","Drama/Thriller","Action/Historical drama","Romance/Rom-com"};
String[] movieRatings = {"3.5/5","4.5/5","3/5","4.5/5","4/5","4/5","3.5/5","2.5/5","3/5","5/5","3.5/5","4/5","3/5"};
int[] movieImages = {R.drawable.bhoot,R.drawable.panga,R.drawable.psycho,R.drawable.jumanji,R.drawable.dil_bechera,R.drawable.baaghi3,R.drawable.bridgecurse,
R.drawable.lostgirls,R.drawable.bad_boys,R.drawable.thappad,R.drawable.sadak2,R.drawable.tanhaji,R.drawable.bheeshma};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
recyclerView = findViewById(R.id.recycleview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
MovieAdapter= new MovieAdapter(this,movieNameList,movieDescriptionList,movieRatings,movieImages);
recyclerView.setAdapter(MovieAdapter);
}
}

Related

multiple recyclerviews?

I want to make my homepage with two row with recyclerview
with data of books and the events which are display independent of each other how can i accomplish it I want to add event to the my code how can i do it?
MoviesAdapter.java
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.book);
}
}//End of MyViewHolder class
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
//display different items in the data set
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
//display data at specified location
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.title.setText(moviesList.get(position).getTitle());
holder.thumbnail.setImageResource(moviesList.get(position).getImage());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
I want to make my page like this
It should be fine to use a LinearLayout with 3 RecyclerViews in it and create an Adapter for each of them. Nested RecyclerViews would only make sense if you had a lot of categories.
I done it with the three recyclerview and their adapter class here is the code of only MainActivity.java and activity.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Book> bookList = new ArrayList<>();
private List<Author> authorList = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
private RecyclerView recyclerView3;
private BookAdapter bAdapter;
private AuthorAdapter aAdapter;
private EventAdapter eAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.recycler_book);
bAdapter = new BookAdapter(this,bookList);
recyclerView1.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView1.setLayoutManager(mLayoutManager);
recyclerView1.setAdapter(bAdapter);
prepareBookData();
//second recycler
recyclerView2 = findViewById(R.id.recycler_author);
aAdapter = new AuthorAdapter(this,authorList);
recyclerView2.setHasFixedSize(true);
RecyclerView.LayoutManager aLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView2.setLayoutManager(aLayoutManager);
recyclerView2.setAdapter(aAdapter);
prepareAuthorData();
//third recycler
recyclerView3 = (RecyclerView) findViewById(R.id.recycler_event);
eAdapter = new EventAdapter(this,eventList);
recyclerView3.setHasFixedSize(true);
RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// RecyclerView.LayoutManager eLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
recyclerView3.setLayoutManager(eLayoutManager);
recyclerView3.setAdapter(eAdapter);
prepareEventData();
}
private void prepareBookData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Book a=new Book(nameArray[0],drawableArray[0]);
bookList.add(a);
Book b=new Book(nameArray[1],drawableArray[1]);
bookList.add(b);
Book c=new Book(nameArray[0],drawableArray[0]);
bookList.add(c);
bAdapter.notifyDataSetChanged();
}
private void prepareAuthorData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Author a=new Author(nameArray[0],drawableArray[0]);
authorList.add(a);
Author b=new Author(nameArray[1],drawableArray[1]);
authorList.add(b);
Author c=new Author(nameArray[0],drawableArray[0]);
authorList.add(c);
Author d=new Author(nameArray[1],drawableArray[1]);
authorList.add(d);
bAdapter.notifyDataSetChanged();
}
private void prepareEventData(){
String[] desArray = {"new","old"};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Event a=new Event(nameArray[0],desArray[0]);
eventList.add(a);
Event b=new Event(nameArray[0],desArray[0]);
eventList.add(b);
Event c=new Event(nameArray[0],desArray[0]);
eventList.add(c);
bAdapter.notifyDataSetChanged();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="fill_parent"
android:layout_height="wrap_content"
tools:context="com.example.hardik.threerecycler.MainActivity"
android:background="#e6edec">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Books"
android:textSize="20sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_book"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="20dp" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authors"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_author"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_book"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Events"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_event"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_author"
android:layout_marginTop="5dp"/>
</LinearLayout>

Edit text items are being overlapped in recyclerview

The problem is each and every time when I send the edittext field from pressing save button I think the items are being overlapped or it may be every time i add the values from edit text field old one may be replaced by new values.They are not appearing in the list.
This is the class from where i send the edit text fields
public class MyEditor extends AppCompatActivity {
EditText textIn,txtHeading;
Button buttonAdd,btnsave;
ArrayList<String> nameList = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
txtHeading = (EditText)findViewById(R.id.heading);
buttonAdd = (Button)findViewById(R.id.add);
btnsave =(Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newName = txtHeading.getText().toString();
Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
intent.putStringArrayListExtra("key", nameList);
nameList.add(newName);
int listsize = nameList.size();
for (int i=0;i<listsize;i++){
Log.i("Lists are", nameList.get(i)) ;
}
startActivity(intent);
Toast.makeText(MyEditor
.this,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();
}
});
}
}
This is my Layout that contains RecyclerView.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_builderxml"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:animateLayoutChanges="false"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_builderxml"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" >
<EditText
android:layout_width="350dp"
android:id="#+id/edittxtsurvey"
android:layout_height="wrap_content"
android:hint="Enter Your SurveyName"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom" >
<SlidingDrawer android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideHandleButton"
android:content="#+id/contentLayout"
android:padding="10dip"
android:layout_height="120dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideHandleButton"
android:src="#drawable/arrowdown">
</ImageView>
<RelativeLayout
android:layout_width="wrap_content"
android:background="#1a237e"
android:id="#+id/contentLayout"
android:gravity="center|top"
android:padding="10dip"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plaintext"
android:id="#+id/imgviewplaintext"
android:padding="5dp"
android:onClick="oOnClick_PlainText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/checkbox"
android:id="#+id/imgviewcheckbox"
android:onClick="OnClick_CheckBox"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imgviewradiobutton"
android:layout_toStartOf="#+id/imgviewradiobutton"
android:layout_marginRight="21dp"
android:layout_marginEnd="21dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/radio"
android:id="#+id/imgviewradiobutton"
android:padding="5dp"
android:onClick="OnClick_RadioButton"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imgviewtextbox"
android:layout_toStartOf="#+id/imgviewtextbox"
android:layout_marginRight="37dp"
android:layout_marginEnd="37dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/textbox"
android:id="#+id/imgviewtextbox"
android:onClick="onClick_textbox"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp" />
</RelativeLayout>
</SlidingDrawer>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
This is my Builder class that contains Builder xml.
public class BuilderPage extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener {
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.builder_layout);
ButterKnife.bind(this);
setSupportActionBar(toolbar1);
getSupportActionBar().setDisplayShowTitleEnabled(false);
dbHelper = new DbHelper(this);
dbHelper.getWritableDatabase();
ArrayList<String> nameList = getIntent().getStringArrayListExtra("key");
myRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_builderxml);
linearLayoutManager =
new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL,false);
myRecyclerViewAdapter = new RecyclerViewAdapter(this,nameList);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
myRecyclerViewAdapter.setOnItemClickListener(this);
This my Adapter class
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context,ArrayList<String> nameList){
layoutInflater = LayoutInflater.from(context);
itemsName = nameList;
}
#Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.text_view,parent, false);
return new ItemHolder(itemView, this);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
#Override
public int getItemCount() {
return (null != itemsName ? itemsName.size() : 0);
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemName;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.herecomes);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getPosition());
}
}
}}
You are using same textitemname everytime.change
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
to
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.textItemName.setText(itemsName.get(position));
}

RecyclerView Fatal Exception:main

Can some one assist me to know what has been causing this error in My recyclerview i have tried changing the variable name at
Adapter1.java:48
But it still giving me the same error.
java.lang.NullPointerException
at ke.co.clickaway.squirrel.clickawaytechnologies.adpter.Adapter1.getItemCount(Adapter1.java:48)
the code is as follows
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ArrayList<DataList> list;
//in the onCreate Bundle
RecyclerView rview=(RecyclerView) findViewById(R.id.rview);
Adapter1 adapter1=new Adapter1(this,list);
rview.setAdapter(adapter1);
StaggeredGridLayoutManager lm=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
rview.setLayoutManager(lm);
the information class
public class DataList {
public int imageView;
public String title;
public static List<DataList> getData(){
ArrayList<DataList> mList=new ArrayList<>();
int[] imag={R.drawable.web_ic,R.drawable.palette_ic,R.drawable.pencil,R.drawable.sware_ic,R.drawable.phone_ic};
String[] lable={"Web Development","Branding","Graphic Design","Systems","Mobile Apps"};
for(int i=0;i<lable.length&& i<imag.length;i++){
DataList dataList=new DataList();
dataList.title=lable[i];
dataList.imageView=imag[i];
mList.add(dataList);
}
return mList;
}
}
And My adapter
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {
private List<DataList> mList;
private Context mContext;
public Adapter1(Context context,List<DataList> list){
mList=list;
mContext=context;
}
private Context getContext(){
return mContext;
}
#Override
public Adapter1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context=parent.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View customView=inflater.inflate(R.layout.custom_view,parent,false);
ViewHolder viewHolder=new ViewHolder(customView);
return viewHolder;
}
#Override
public void onBindViewHolder(Adapter1.ViewHolder holder, int position) {
DataList dataList=mList.get(position);
holder.imag.setImageResource(dataList.imageView);
holder.lable.setText(dataList.title);
}
#Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imag;
public TextView lable;
public ViewHolder(View itemView) {
super(itemView);
imag= (ImageView)itemView.findViewById(R.id.imageView1);
lable=(TextView)itemView.findViewById(R.id.textView1);
}
}
}
my custom View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/one" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="dummy Text" />
</LinearLayout>
the RecyclerView snippet in activty_main.xml
<android.support.v7.widget.RecyclerView
android:layout_below="#id/appbarlayout"
android:id="#+id/rview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list is null. So initialize your list before you create your adapter or change getItemCount() method in your adapter to this:
#Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
As per your code the list is null, so instead of using ArrayList<DataList> list in MainActivity use ArrayList<DataList> list = new ArrayList<>() or ArrayList<DataList> list = DataList.getData()
try add this code :
list = Datalist.getData();
before this :
Adapter1 adapter1=new Adapter1(this,list);

Can't show images on RecyclerView

I want to create a list of items that have a background image and a TextView. Although it creates the RecyclerView normally and sets the text, the background image doesn't set.
This is my Adapter Class
public class Casino_Adapter extends RecyclerView.Adapter<Casino_Adapter.ViewHolder> {
private Data[] mDataset;
private ClickListener clickListener;
public Context context;
// Provide a suitable constructor (depends on the kind of dataset)
public Casino_Adapter(Data[] myDataset) {
this.mDataset = myDataset;
this.context = context;
}
#Override
public Casino_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.casino_card_row, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final Context context = null;
// each data item is just a string in this case
public TextView mTextView;
public ImageView mImageView;
public CardView cardView;
//Typeface tf;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text);
mImageView = (ImageView) v.findViewById(R.id.image);
cardView = (CardView) v.findViewById(R.id.card_view);
//cardView.setPreventCornerOverlap(false);
}
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getText());
holder.mImageView.setImageResource(mDataset[position].getImage());
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
public interface ClickListener {
public void itemClicked(View view, int pos);
}
}
I have a Fragment Class where I want the RecyclerView to be.
public class CasinoFragment extends Fragment {
protected RecyclerView recyclerView;
protected RecyclerView.Adapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
public CasinoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initial();
}
private void initial() {
final Data datas[] =
{
new Data("This is an item", R.drawable.ic_home_white_36dp),
new Data("This is an item 2", R.drawable.car),
new Data("asdasd`sdads", R.drawable.car),
new Data("This is an item 3", R.drawable.car)
};
mAdapter = new Casino_Adapter(datas);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_casino, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
return view;
}
}
And these are the Setters that I use.
public class Data {
int image;
String text;
public Data(String title, int backimage)
{
this.text = title;
this.image = backimage;
}
public String getText()
{
return text;
}
public int getImage()
{
return image;
}
public void setText()
{
this.text=text;
}
public void setImageID()
{
this.image = image;
}
}
Casino Row XML
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/image_round"
android:src="#drawable/car" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/image"
android:id="#+id/rel_color"
android:background="#4c4c4c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Μπύρα"
android:textColor="#fcfcfc"
android:textSize="30sp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:id="#+id/text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
The result I get is the following.
The reason why you aren't seeing your image, is because it isn't actually visible.
Your rel_color layout has the same size attributes as your ImageView.
If you want to display a layout above the ImageView, you just need to remove the background of it, otherwise the ImageView will be hidden.
Just Like that. but remember your images dimensions must be low. in my case i used 200x200
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/rl_menu_item"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="5dp"
android:background="#android:color/transparent"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/img_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_blrr">
<ImageView
android:id="#+id/iv_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#drawable/gray_circle"
android:padding="15dp"
android:scaleType="centerCrop"
android:src="#drawable/school" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#id/iv_icon"
android:layout_alignLeft="#id/iv_icon"
android:layout_alignRight="#id/iv_icon"
android:layout_alignStart="#id/iv_icon"
android:layout_below="#id/iv_icon"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Your Favourite Place"
android:textColor="#android:color/black" />
</RelativeLayout>
</android.support.v7.widget.CardView>

Android access Listview inside RecyclerView

I want to make a ListView inside a RecyclerView that should display some data in a list form. I currently have a basic Adapter and RecyclerView with no ListView inside of it, so can someone tell me how to modify my code to include a listview inside the RecyclerView?
Adapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AdapterViewHolder> {
public static class AdapterViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView summary;
AdapterViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.recycler_list_title);
summary = (TextView) itemView.findViewById(R.id.recycler_list_summary);
}
}
List<Items> items;
public RVAdapter(List<Items> items){
this.items = items;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
AdapterViewHolder pvh = new AdapterViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
adapterViewHolder.title.setText(item.get(i).title);
adapterViewHolder.summary.setText(item.get(i).summary);
}
#Override
public int getItemCount() {
return item.size();
}
}
Items:
public class Items {
String title;
String summary;
public Items(String title, String summary) {
this.title = title;
this.summary = summary;
}
}
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="0dp" />
</LinearLayout>
list_recycler:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<TextView
android:id="#+id/recycler_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/recycler_list_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColorSub"
android:textSize="14sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
How I'm creating the ListView:
private List item;
private RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rv = (RecyclerView) ((Activity) MainActivity.context).findViewById(R.id.recycler);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
initializeData();
initializeAdapter();
}
private void initializeData(){
item = new ArrayList<>();
item.add(new Items("Title", "Summary"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(item);
rv.setAdapter(adapter);
}
}
ListView listview = (ListView) findViewById(R.id.listview);
ListView parentListView=(ListView)listview.getParent();
you can get parent listView by this way

Categories

Resources