RecyclerView And onClicklistener - android

I use this code in MainActivity But I can not access setOnMenuItemClickListener in MainActivity .
MainActivity class:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Toast toast ;
ImageView hahaha;
protected static int ss=77;
//recyclerview objects
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public int xxx=222;
//model object for our list data
private List<MyList> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
//loading list view item with this function
loadRecyclerViewItem();
}
private void loadRecyclerViewItem() {
//you can fetch the data from server or some apis
//for this tutorial I am adding some dummy data directly
for (int i = 1; i <= 7; i++) {
MyList myList = new MyList(
"Heading " + i,
"id = " + i,
"https://mediaresources.idiva.com/media//content/2014/May/diet_kids_health1.jpg",
"HHHHHH" + i
);
list.add(myList);
}
adapter = new CustomAdapter(list, this);
recyclerView.setAdapter(adapter);
}
}
and this is CustomAdaptor.class
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<MyList> list;
private Context mCtx;
public CustomAdapter(List<MyList> list, Context mCtx) {
this.list = list;
this.mCtx = mCtx;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, final int position) {
MyList myList = list.get(position);
//loading the image
Glide.with(mCtx)
.load(myList.getUrl())
.into(holder.imageView);
holder.textViewHead.setText(myList.getHead());
holder.textViewDesc.setText(myList.getDesc());
//holder.textViewDesc.setText(myList.getIdofdata());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
holder.textViewHead.setText("moshiri" + holder.textViewHead.getText() );
break;
case R.id.menu2:
//holder.textViewHead.setText("new" + holder.textViewDesc.getText() );
holder.textViewHead.setText("mohsen hi dear");
break;
case R.id.menu3:
holder.textViewHead.setText("##################" + list.indexOf(this));
holder.textViewDesc.setText("############" + list.indexOf(this));
//MainActivity me=new MainActivity();
MainActivity.ss=77;
removeItem(3);
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount() );
// notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewHead;
public TextView textViewDesc;
public TextView buttonViewOption;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
}
In CustomAdaptor I access onclick listener in menu and button but I can not manage menu and button in MainActivity. How can I use Listener in mainActivity or how can I send changes from CustomAdaptor to mainActivity?

Use interface for this purpose. In your CustomAdapter declare that interface and in ViewHolder override the click listener method and pass items from adapter you want. Currently I am passing position only you can pass anything here. I have make changes in your code just check below.
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
protected CustomClicklistener mListener;
private List<MyList> list;
private Context mCtx;
private CustomClicklistener clickListener;
public interface CustomClicklistener {
void onItemClick(int position);
}
public CustomAdapter(List<MyList> list, Context mCtx, CustomClicklistener
clickListener) {
this.list = list;
this.mCtx = mCtx;
this.clickListener = clickListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, final int position) {
MyList myList = list.get(position);
//loading the image
Glide.with(mCtx)
.load(myList.getUrl())
.into(holder.imageView);
holder.textViewHead.setText(myList.getHead());
holder.textViewDesc.setText(myList.getDesc());
//holder.textViewDesc.setText(myList.getIdofdata());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
holder.textViewHead.setText("moshiri" + holder.textViewHead.getText() );
break;
case R.id.menu2:
//holder.textViewHead.setText("new" + holder.textViewDesc.getText() );
holder.textViewHead.setText("mohsen hi dear");
break;
case R.id.menu3:
holder.textViewHead.setText("##################" + list.indexOf(this));
holder.textViewDesc.setText("############" + list.indexOf(this));
//MainActivity me=new MainActivity();
MainActivity.ss=77;
removeItem(3);
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public void removeItem(int position) {
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount() );
// notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewHead;
public TextView textViewDesc;
public TextView buttonViewOption;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
#Override
public void onClick(View view) {
if (mListener != null) {
mListener.onItemClick(getAdapterPosition());
notifyDataSetChanged();
}
}
}
}
In MainActivity you can get that clicklistener like below in oncCreate METHOD AT THE END
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
CustomAdapter.CustomClicklistener clickListener;
Toast toast ;
ImageView hahaha;
protected static int ss=77;
//recyclerview objects
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public int xxx=222;
//model object for our list data
private List<MyList> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
//loading list view item with this function
loadRecyclerViewItem();
clickListener = new CustomAdapter.CustomClicklistener() {
#Override
public void onItemClick(int position) {
// Do what you want here
Toast.makeText(MainActivity.this, "This ... " , Toast.LENGTH_LONG).show();
}
};
}
private void loadRecyclerViewItem() {
//you can fetch the data from server or some apis
//for this tutorial I am adding some dummy data directly
for (int i = 1; i <= 7; i++) {
MyList myList = new MyList(
"Heading " + i,
"id = " + i,
"https://media.alalamtv.net/news/image/855x495//2015/02/16/alalam_635596703961833722_25f_4x3.jpg",
"HHHHHH" + i
);
list.add(myList);
}
adapter = new CustomAdapter(list, this, clickListener);
recyclerView.setAdapter(adapter);
}
}

Create an interface in your CustomAdapter class like:
CustomAdapterListener customAdapterListener;
public interface CustomAdapterListener {
void onItemClick();
}
And then in your constructor :
public CustomAdapter(List<MyList> list, Context mCtx, CustomAdapterListener customAdapterListener) {
this.list = list;
this.mCtx = mCtx;
this.customAdapterListener= customAdapterListener;
}
and trigger interface method when click event occured:
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
customAdapterListener.onItemClick();
});
And from your Activity; add your activity as listener like this :
adapter = new CustomAdapter(list, this, this);
Finally you have to implement your interface in your activity like :
public class MainActivity extends AppCompatActivity implements CustomAdapter.CustomAdapterListener
And click on red underlined text and press alt+enter, this will suggest you to implement interface methods, after confirm that warning you have onItemClick method in your activity. This method will trigger from adapter if click event is occured.

Related

Opening a new Activity from a RecyclerView with Firebase data

I'm creating a TripAdvisor clone app that gets data from a Firebase database and shows data from it. I want to add a listener to the RecyclerView that opens a new Activity called locationInDetailActivity that shows informations about the picked element. Here are the codes for the MainActivity and the RecyclerView adapter.
MainActivity.java
package com.example.progettocv19ingsw;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private Button to_advanced_filters;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DatabaseReference reference;
private RecyclerViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to_advanced_filters = (Button) findViewById(R.id.advancedFilters);
recyclerView = findViewById(R.id.locationRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
to_advanced_filters.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openAdvancedFilterActivity();
}
});
reference = FirebaseDatabase.getInstance().getReference();
FirebaseRecyclerOptions<locationClass> options = new FirebaseRecyclerOptions.Builder<locationClass>().setQuery(FirebaseDatabase.getInstance().getReference().child("locations"), locationClass.class).build();
adapter = new RecyclerViewAdapter(options);
recyclerView.setAdapter(adapter);
}
public void openAdvancedFilterActivity(){
Intent open = new Intent(this, AdvancedFilters.class);
startActivity(open);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
RecyclerViewAdapter.java
package com.example.progettocv19ingsw;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class RecyclerViewAdapter extends FirebaseRecyclerAdapter<locationClass, RecyclerViewAdapter.myViewHolder> {
private static final String TAG = "RecyclerView";
public RecyclerViewAdapter(#NonNull FirebaseRecyclerOptions options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder viewHolder, int i, #NonNull locationClass location) {
viewHolder.locationName.setText(location.getLocationName());
viewHolder.locationType.setText(location.getLocationType());
Glide.with(viewHolder.locationImage.getContext()).load(location.getLocationPic()).into(viewHolder.locationImage);
viewHolder.reviewNumber.setText(String.valueOf(location.getReviewNumber()));
viewHolder.averageRatingBar.setRating(location.getAverageRating());
viewHolder.averageRatingBar.setStepSize(location.getAverageRating());
}
#Override
public int getItemCount() {
return super.getItemCount();
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.location_recyclerrow, parent, false);
return new myViewHolder(v);
}
class myViewHolder extends RecyclerView.ViewHolder {
ImageView locationImage;
TextView locationName;
TextView locationType;
RatingBar averageRatingBar;
TextView reviewNumber;
Context context;
public myViewHolder(#NonNull View itemView) {
super(itemView);
locationImage = (ImageView) itemView.findViewById(R.id.locationPicture);
locationName = (TextView) itemView.findViewById(R.id.locationName);
locationType = (TextView) itemView.findViewById(R.id.locationType);
reviewNumber = (TextView) itemView.findViewById(R.id.reviewNumber);
averageRatingBar = (RatingBar) itemView.findViewById(R.id.averageRating);
}
}
}
locationInDetailActivity.java
package com.example.progettocv19ingsw;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class locationInDetailActivity extends AppCompatActivity {
private static final String TAG = "locationDetailActivity";
private Button toReviews;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_data);
Log.d(TAG, "onCreate: Started");
toReviews.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openReviewActivity();
}
});
}
private void openReviewActivity() {
Intent intent = new Intent(this, reviewActivity.class);
}
}
I have tried adding an OnClick listener, but it either gives me "cannot resolve method" or NullPointerException() errors. What do I do?
Colin Degen is right, but you can do something more generic.
Create a interface for the clicks event:
public interface IClickListener {
void onItemClick(int position, View view);
void onItemLongClick(int position, View view);
}
Create a recycler view adapter to suit your needs:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private IClickListener clickListener;
private List<String> models;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#Override
public void onClick(View view) {
clickListener.onItemClick(getAdapterPosition(), view);
}
#Override
public boolean onLongClick(View view) {
clickListener.onItemLongClick(getAdapterPosition(), view);
return false;
}
}
public RecyclerViewAdapter(List<String> models) {
this.models = models;
}
public void setOnItemClickListener(IClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final String model = models.get(position);
TextView textView = holder.textView;
textView.setText(model);
}
#Override
public int getItemCount() {
return models.size();
}}
Then, when you want to use your recycler view you can do something like this:
ArrayList<String> dataSource = new ArrayList<String>()
// Add some data to the dataSource
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataSource);
adapter.setOnItemClickListener(new IClickListener() {
#Override
public void onItemClick(int position, View view) {
String model = dataSource.get(position);
// Do stuff with your model like passing data to the next activity
}
});
You have to define an OnClickListener for each element in your RecyclerView.
private final OnClickListener mOnClickListener = new MyOnClickListener(){
#Override
public void onClick(final View view) {
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
YourItem item = mList.get(itemPosition);
//Do stuff and start your activity
}
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.location_recyclerrow, parent, false);
view.setOnClickListener(mOnClickListener);
return new myViewHolder(v);
}
And then you pass your RecyclerView instance to the constructor of your adapter
private RecyclerView mRecyclerView;
public RecyclerViewAdapter(#NonNull FirebaseRecyclerOptions options, RecyclerView rV) {
super(options);
mRecyclerView = rV;
}

How to get data from one RecyclerView and show it in another Activity's RecyclerView

I have an Android nested recycler view in which I am able to show the data now I want to show that data in another activity recycler view. How can I do that?
This is my nested list view:-
This is where I want to show data in list:-
Here's my code: my Main Activity:-
package com.saikari.vid.activities;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.saikari.vid.R;
import com.saikari.vid.adapters.VerticalRVAdapter;
import com.saikari.vid.models.HorizontalModel;
import com.saikari.vid.models.VerticalModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private DatabaseReference dbCategories;
VerticalRVAdapter adapter;
RecyclerView verticalRecyclerView;
ArrayList<VerticalModel> mListData = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticalRecyclerView = findViewById(R.id.recycler_view);
verticalRecyclerView.setHasFixedSize(true);
setData();
}
private void setData() {
for (int i = 0; i<10; i++){
VerticalModel verticalModel = new VerticalModel();
verticalModel.setCat_title("Title: "+i);
ArrayList<HorizontalModel> arrayListHorizontal = new ArrayList<>();
for (int j = 0; j<10; j++){
HorizontalModel horizontalModel = new HorizontalModel();
horizontalModel.setVid_url("https://picsum.photos/seed/picsum/200/300");
horizontalModel.setVid_title("Name: "+j);
arrayListHorizontal.add(horizontalModel);
}
verticalModel.setVidArrayList(arrayListHorizontal);
mListData.add(verticalModel);
}
adapter.notifyDataSetChanged();
}
}
Vertical RecyclerView adapter class:-
public class VerticalRVAdapter extends RecyclerView.Adapter<VerticalRVAdapter.VerticalRVViewHolder> {
Context context;
ArrayList<VerticalModel> arrayList;
public VerticalRVAdapter(Context context, ArrayList<VerticalModel> arrayList){
this.arrayList = arrayList;
this.context = context;
}
#NonNull
#Override
public VerticalRVViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vertical,parent,false);
return new VerticalRVViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull VerticalRVViewHolder holder, int position) {
final VerticalModel verticalModel = arrayList.get(position);
String title = verticalModel.getCat_title();
final ArrayList<HorizontalModel> singleItem = verticalModel.getVidArrayList();
holder.titleText.setText(title);
HorizontalRVAdapter horizontalRecyclerViewAdapter= new HorizontalRVAdapter(context,singleItem);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
holder.recyclerView.setAdapter(horizontalRecyclerViewAdapter);
holder.buttonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, verticalModel.getCat_title(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, VideoListActivity.class);
i.putExtra("catTitle", verticalModel.getCat_title());
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class VerticalRVViewHolder extends RecyclerView.ViewHolder{
RecyclerView recyclerView;
TextView titleText;
TextView buttonMore;
public VerticalRVViewHolder(#NonNull View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.recycler_view1);
titleText = itemView.findViewById(R.id.titleText);
buttonMore = itemView.findViewById(R.id.btn_more);
}
}
}
Horizontal Recycler View Adapter class:-
public class HorizontalRVAdapter extends RecyclerView.Adapter<HorizontalRVAdapter.HorizontalRVViewHolder> {
Context context;
ArrayList<HorizontalModel> arrayList;
public HorizontalRVAdapter(Context context, ArrayList<HorizontalModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public HorizontalRVViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal,parent,false);
return new HorizontalRVViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final HorizontalRVViewHolder holder, int position) {
final HorizontalModel horizontalModel = arrayList.get(position);
holder.textViewTitle.setText(horizontalModel.getVid_title());
com.bumptech.glide.Glide.with(context).load(horizontalModel.getVid_url()).into(holder.imageViewThumb);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, horizontalModel.getVid_url(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, YoutubePlayActivity.class);
i.putExtra("videoID", horizontalModel.getVid_url());
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class HorizontalRVViewHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
ImageView imageViewThumb;
public HorizontalRVViewHolder(#NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textTitleHorizontal);
imageViewThumb = itemView.findViewById(R.id.ivThumb);
}
}
}
Data in list activity class (this is the activity where I need to show data):-
public class VideoListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getIntent().getStringExtra("catTitle"));
}
}
Implement Parcelable on HorizontalModel class. If you need help with that check here.
In VerticalRVAdapter inside onBindViewHolder method
holder.buttonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, verticalModel.getCat_title(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, VideoListActivity.class);
i.putExtra("catTitle", verticalModel.getCat_title());
// Put horizontal models in extra bundle
i.putParcelableArrayListExtra("horizontal_models", singleItem);
context.startActivity(i);
}
});
You can now get the bundled HorizontalModel list in VideoListActivity inside onCreate as
ArrayList<HorizontalModel> horizontalModels = getIntent().getParcelableArrayListExtra("horizontal_models");
You can use this list to populate the vertical list that you want to show.

notifyItemRemoved() is not updating my recycler view but removes data from the array list

I am using a recycler view. I've set an OnLongClickListener() on it using a custom interface. everything works fine but when I long-press the row the app crashes with Java.lang.IndexOutOfBoundsException. while debugging I found that my array list's data is being deleted while I long-press but the recycler view is just not getting updated.
here's my code.
MainActivity.java
package com.raunak.alarmdemo4;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.raunak.alarmdemo4.Activities.AddAlarm;
import com.raunak.alarmdemo4.Adapters.AlarmAdapter;
import com.raunak.alarmdemo4.HelperClasses.AlarmsDBhelperClass;
import com.raunak.alarmdemo4.Interfaces.AlarmRecyclerViewInterface;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AlarmRecyclerViewInterface {
FloatingActionButton mAlarmAddButton;
RecyclerView mRecyclerView;
SQLiteDatabase db;
AlarmsDBhelperClass mAlarmsDBhelperClass;
ArrayList<String> nameArrayList,modeArrayList,repeatArrayList,hoursArrayList,minArrayList;
Switch mSwitch;
AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Initializing RecyclerView & DatabaseHelperClass and the FAB button
mAlarmsDBhelperClass = new AlarmsDBhelperClass(getApplicationContext());
mAlarmAddButton = findViewById(R.id.btnAlarmADD);
mRecyclerView = findViewById(R.id.alarmList);
mSwitch = findViewById(R.id.switchONOFF);
//Initializing ArrayList
nameArrayList = new ArrayList<>();
modeArrayList = new ArrayList<>();
repeatArrayList = new ArrayList<>();
hoursArrayList = new ArrayList<>();
minArrayList = new ArrayList<>();
DividerItemDecoration itemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
//Getting a writable reference of the Database.
db = mAlarmsDBhelperClass.getWritableDatabase();
//getAlarm(db);
getAlarm(db);
mAlarmAddButton.setImageResource(R.drawable.addalarm);
mAlarmAddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mIntent = new Intent(getApplicationContext(), AddAlarm.class);
startActivity(mIntent);
}
});
/* mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
Toast.makeText(getApplicationContext(),"Checked !",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Unchecked !",Toast.LENGTH_SHORT).show();
}
}
});*/
AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecyclerView.setAdapter(alarmAdapter);
mRecyclerView.setHasFixedSize(true);
}
public void getAlarm(SQLiteDatabase db) {
Cursor cursor = db.rawQuery("SELECT * FROM alarms", new String[]{});
cursor.moveToFirst();
if (cursor != null) {
do {
nameArrayList.add(cursor.getString(2));
modeArrayList.add(cursor.getString(3));
repeatArrayList.add(cursor.getString(4));
hoursArrayList.add(Integer.toString(cursor.getInt(5)));
minArrayList.add(Integer.toString(cursor.getInt(6)));
} while (cursor.moveToNext());
}
cursor.close();
}
#Override
public void onItemClick(int position) {
Toast.makeText(this, "Alarm Clicked !", Toast.LENGTH_SHORT).show();
}
#Override
public void onLongItemClick(int position) {
hoursArrayList.remove(position);
Log.d("d",""+position);
alarmAdapter.notifyItemRemoved(position);
Toast.makeText(this, ""+hoursArrayList.size(), Toast.LENGTH_SHORT).show();
}
}
my Recyclerview adapter.
package com.raunak.alarmdemo4.Adapters;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.raunak.alarmdemo4.Interfaces.AlarmRecyclerViewInterface;
import com.raunak.alarmdemo4.R;
import java.util.ArrayList;
public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.AlarmView> {
//Variables for the main recycler view
private ArrayList<String> hoursArrayList;
private ArrayList<String> minArrayList;
private ArrayList<String> modeArrayList;
private ArrayList<String> repeatArrayList;
private ArrayList<String> nameArrayList;
private AlarmRecyclerViewInterface mInterface;
public AlarmAdapter(ArrayList<String> hours,ArrayList<String> mins,ArrayList<String> mode,ArrayList<String> repeat,ArrayList<String> name,AlarmRecyclerViewInterface mInterface){
this.hoursArrayList = hours;
this.minArrayList = mins;
this.modeArrayList = mode;
this.nameArrayList = name;
this.repeatArrayList = repeat;
this.mInterface = mInterface;
}
#NonNull
#Override
public AlarmView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.alarm_profile,parent,false);
return new AlarmView(view);
}
#Override
public void onBindViewHolder(#NonNull AlarmView holder, int position) {
if(Integer.parseInt(hoursArrayList.get(position)) < 10 ){
holder.hours.setText("0"+hoursArrayList.get(position));
}else {
holder.hours.setText(hoursArrayList.get(position));
}
if (Integer.parseInt(minArrayList.get(position)) < 10){
holder.mins.setText("0"+minArrayList.get(position));
}else {
holder.mins.setText(minArrayList.get(position));
}
holder.repeat.setText(repeatArrayList.get(position));
holder.mode.setText(modeArrayList.get(position));
holder.name.setText(nameArrayList.get(position));
}
#Override
public int getItemCount() {
return nameArrayList.size();
}
/*ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
nameArrayList.remove(viewHolder.getAdapterPosition());
modeArrayList.remove(viewHolder.getAdapterPosition());
repeatArrayList.remove(viewHolder.getAdapterPosition());
hoursArrayList.remove(viewHolder.getAdapterPosition());
minArrayList.remove(viewHolder.getAdapterPosition());
}
};*/
public class AlarmView extends RecyclerView.ViewHolder{
TextView hours,mins,repeat,name,mode;
public AlarmView(#NonNull View itemView) {
super(itemView);
hours = itemView.findViewById(R.id.txtHOUR);
mins = itemView.findViewById(R.id.txtMins);
repeat = itemView.findViewById(R.id.txtRepeatDays);
name = itemView.findViewById(R.id.txtName);
mode = itemView.findViewById(R.id.txtMode);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("d",""+getAdapterPosition());
mInterface.onItemClick(getAdapterPosition());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Log.d("d",""+getAdapterPosition());
mInterface.onLongItemClick(getAdapterPosition());
return true;
}
});
}
}
}
my CustomInterface
package com.raunak.alarmdemo4.Interfaces;
public interface AlarmRecyclerViewInterface {
void onItemClick(int position);
void onLongItemClick(int position);
}
Thanks for help!
You should remove the element also from nameArrayList and inside your Adapter:
#Override
public int getItemCount() {
return nameArrayList.size(); //This return a number that is higher than the number of elements
}
.
//When you call this, you remove the element from the list in the Main Activity, but not in your Adapter
hoursArrayList.remove(position);
.
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
Log.d("d",""+ pos);
hoursArrayList.remove(pos); //I think you'd add
nameArrayList.remove(pos); //these two lines
mInterface.onLongItemClick(pos);
return true;
}
});
.
#Override
public int getItemCount() {
return hoursArrayList.size(); //Or just the first line and modify this
}
And you should modify your MainActiivty fields:
FloatingActionButton mAlarmAddButton;
RecyclerView mRecyclerView;
SQLiteDatabase db;
AlarmsDBhelperClass mAlarmsDBhelperClass;
ArrayList<String> nameArrayList,modeArrayList,repeatArrayList,hoursArrayList,minArrayList;
Switch mSwitch;
AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
//This line has nosense, if you re-declare another alarmAdapter in the onCreate
Your are declaring and assigning different adapter in onCreate. Remove the declaration from there and just initialize it there.
AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
And you need to improve your code for efficiency, create a model class instead of declaring arrrayList of every param.

Cannot resolve method setOnItemClickListener for RecyclerView

I am working with a RecyclerView and I am trying to set OnClick's and OnLongClick's for each of the item's in the RecyclerView. I apologize but I don't think I truly understand what is going on with the code, some of it was taken off slides for my assignment and some patches for stuff that wouldn't work that I found on this site. At the top of my RecyclerView Adapter class I hav
static OnItemClickListener mItemClickListener;` And then I have an interface for my new methods, a method to set my clicklistener, and then my ViewHolder implementation like so:
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView movieImage;
public TextView movieName;
public TextView movieDescription;
public CheckBox movieCheckBox;
public CardView cV;
public ViewHolder(View view) {
super(view);
movieImage = (ImageView) view.findViewById(R.id.movieImg);
movieName = (TextView) view.findViewById(R.id.movieName);
movieDescription = (TextView) view.findViewById(R.id.movieDescription);
movieCheckBox = (CheckBox) view.findViewById(R.id.movieCheckBox);
view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
if (getAdapterPosition() != RecyclerView.NO_POSITION) {
mItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (mItemClickListener != null) {
if (getAdapterPosition() != RecyclerView.NO_POSITION) {
mItemClickListener.onItemLongClick(v, getAdapterPosition());
}
}
return true;
}
});
}
}
My issue comes in the class for my fragment which owns the recycler view, where I am trying to set the onitemclicklistener.
myAdapter = new MyRecyclerAdapter();
myAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.d("TEST", "onItemClick");
}
#Override
public void onItemLongClick(View view, int position) {
Log.d("TEST", "onItemLongClick");
}
});
It says the issue is with myAdapter.setOnItemClickListener it says 'Cannot resolve method 'setOnItemClickListener(anonymous com.example.kevin.assignment4.MyRecyclerAdapter.OnItemClickListener)' But I don't know why it's saying that. It should be able to find the method just fine as far as I can tell. EDIT: My imports for MyRecyclerView class are as follows:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
I tried same thing and its working cool. check this.
public class MyFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener{
private MyAdapter mAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
//init views
final View root = inflater.inflate(R.layout.layout, container, false);
RecyclerView rv = (RecyclerView) root.findViewById(R.id.recyclerview);
mAdapter = new MyAdapter(getActivity(), data);
mAdapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Toast.makeText(getActivity(), "Working...", Toast.LENGTH_LONG).show();
}
});
rv.setAdapter(mAdapter);
rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
return root;
}
public interface OnItemClickListener{
void onItemClick(View view, int position);
}
private class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<Data> data;
private Context mContext;
MyAdapter(Context context, ArrayList<Data> data) {
this.data = data;
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
MyHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(onItemClickListener != null){
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
}
}
I'm way too late but this might help someone; I've just had the same 'cannot resolve symbol' issue while trying to use the Adapter's custom methods from an activity (in order to set up click listeners for RecyclerView in Java, using Android Studio 3.3.2).
Confusingly, despite this, at the same time, for my adapter instance the base class RecyclerView.Adapter's own methods (such as getItemCount and onBindViewHolder ) were resolvable.
My problem was that I had similar to Kevin's code, where Kevin in his fragment has:
myAdapter = new MyRecyclerAdapter();
whereas what solved my problem was that I had neglected to simply specify a type when instantiating the Adapter - e.g.:
MyRecyclerAdapter myAdapter = new MyRecyclerAdapter();
... 'cannot resolve symbol' then became a thing of the past and I was able to access my custom Adapter methods such as setOnItemClickedListener.
So I'm not sure why my original method didn't work, since I've seen similar methods online and even one of the answers posted here worked the same way. I solved my issue by changing the structure of my adapter class so that it takes the listener in as a parameter to the constructor of the RecyclerView adapter. There, I set the listener in the class equal to the listener that came in.
public MyRecyclerAdapter(ClickListener clickListener) {
setClickListener(clickListener);
}
And then in my fragment where I create the recyclerview I have
myAdapter = new MyRecyclerAdapter(this);
My fragment class implements the interface from my adapter class.
Accidentally declaring your adapter as RecyclerView.Adapter instead of your custom adapter MyRecyclerAdapter() is a possible cause.
RecyclerView.Adapter adapter = new MyRecyclerAdapter()
Instead of,
MyRecyclerAdapter adapter = new MyRecyclerAdapter()
I had the similar problem.. This code solved my problem.Its too late for answering, but this code works like magic.This is my java class.
package app.android.dgconnect.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import app.android.dgconnect.R;
import app.android.dgconnect.adapters.AddressBookAdapter;
import app.android.dgconnect.models.CustomersProfileModel;
public class AddressBookMain extends AppCompatActivity implements AdapterView.OnItemClickListener {
private List<CustomersProfileModel> itemList = new ArrayList<>();
private AddressBookAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.address_book_layout);
CustomersProfileModel customersProfileModel=getIntent().getParcelableExtra("customerProfileModel");
TextView title = findViewById(R.id.title);
ImageView toolbar_back = findViewById(R.id.toolbar_back);
title.setText("Address");
toolbar_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
RecyclerView recyclerView = findViewById(R.id.recycler_view);
mAdapter = new AddressBookAdapter(itemList,this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.HORIZONTAL));
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
for(int i=0;i<customersProfileModel.addresses.size();i++){
itemList.add(customersProfileModel);
mAdapter.notifyDataSetChanged();
}
mAdapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
This is my adapter class :
package app.android.dgconnect.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.List;
import app.android.dgconnect.R;
import app.android.dgconnect.models.CustomersProfileModel;
public class AddressBookAdapter extends RecyclerView.Adapter<AddressBookAdapter.SingleCheckViewHolder> {
private List<CustomersProfileModel> itemList;
private int mSelectedItem = -1;
private Context mContext;
private AdapterView.OnItemClickListener onItemClickListener;
public AddressBookAdapter(List<CustomersProfileModel> itemList, Context context) {
this.itemList = itemList;
this.mContext = context;
}
#NonNull
#Override
public SingleCheckViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(R.layout.address_book_list_row, viewGroup, false);
return new SingleCheckViewHolder(view, this);
}
#Override
public void onBindViewHolder(#NonNull final SingleCheckViewHolder holder, final int position) {
CustomersProfileModel items = itemList.get(position);
holder.address.setText(items.addresses.get(position).street.get(0));
String rname = items.addresses.get(position).region.region;
String pcode = items.addresses.get(position).postcode;
if (rname.equals("string")) {
holder.address_line_two.setText(pcode);
} else {
holder.address_line_two.setText(rname + ", " + pcode);
}
try {
holder.setDateToView(items, position);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return itemList.size();
}
public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void onItemHolderClick(SingleCheckViewHolder holder) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(null, holder.itemView, holder.getAdapterPosition(), holder.getItemId());
}
class SingleCheckViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private AddressBookAdapter mAdapter;
private RadioButton radioButton;
private TextView address, address_line_two;
public SingleCheckViewHolder(View itemView, final AddressBookAdapter mAdapter) {
super(itemView);
this.mAdapter = mAdapter;
address = itemView.findViewById(R.id.address);
address_line_two = itemView.findViewById(R.id.address_line_two);
radioButton = itemView.findViewById(R.id.radioButton);
itemView.setOnClickListener(this);
radioButton.setOnClickListener(this);
}
public void setDateToView(CustomersProfileModel item, int position) throws Exception {
radioButton.setChecked(position == mSelectedItem);
// mText.setText(item.getPersonName());
}
#Override
public void onClick(View v) {
mSelectedItem = getAdapterPosition();
notifyItemRangeChanged(0, itemList.size());
mAdapter.onItemHolderClick(SingleCheckViewHolder.this);
}
}
}

Intent is not working in my RecyclerView

I use this code:
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public MyAdapter (Context context, ArrayList<PersonData>peopleDataSet) {
this.context = context;
this.peopleDataSet = peopleDataSet;
}
Context context;
private ArrayList<PersonData> peopleDataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewEmail;
ImageView imageViewIcon;
Button button;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewEmail = (TextView) itemView.findViewById(R.id.textViewEmail);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
this.button = (Button) itemView.findViewById(R.id.button);
}
}
public MyAdapter(ArrayList<PersonData> people) {
this.peopleDataSet = people;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);
view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
final TextView textViewEmail = holder.textViewEmail;
ImageView imageView = holder.imageViewIcon;
Button button = holder.button;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(context, MainActivity2Activity.class);
context.startActivity(myIntent);
}
});
textViewName.setText(peopleDataSet.get(listPosition).getName());
textViewEmail.setText(peopleDataSet.get(listPosition).getEmail());
imageView.setImageResource(peopleDataSet.get(listPosition).getImage());
}
#Override
public int getItemCount() {
return peopleDataSet.size();
}
}
But when I press the button I have the following Error:
Attempt to invoke virtual method java.lang.String android.content.Context.getPackageName() on a null object reference
And one more question, how to identify each buttons in my List, so that they will start different Activities.
MainActivity2Activity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2Activity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4000)
at info.osoap.myapplicationzzz.MyAdapter$1.onClick(MyAdapter.java:70)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
MainActivity.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<PersonData> people;
static View.OnClickListener myOnClickListener;
private static ArrayList<Integer> removedItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOnClickListener = new MyOnClickListener(this);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
people = new ArrayList<PersonData>();
for (int i = 0; i < MyData.nameArray.length; i++) {
people.add(new PersonData(
MyData.nameArray[i],
MyData.emailArray[i],
MyData.drawableArray[i],
MyData.id_[i]
));
}
removedItems = new ArrayList<Integer>();
adapter = new MyAdapter(people);
recyclerView.setAdapter(adapter);
}
private static class MyOnClickListener implements View.OnClickListener {
private final Context context;
private MyOnClickListener(Context context) {
this.context = context;
}
#Override
public void onClick(View v) {
removeItem(v);
}
private void removeItem(View v) {
int selectedItemPosition = recyclerView.getChildPosition(v);
RecyclerView.ViewHolder viewHolder
= recyclerView.findViewHolderForPosition(selectedItemPosition);
TextView textViewName
= (TextView) viewHolder.itemView.findViewById(R.id.textViewName);
String selectedName = (String) textViewName.getText();
int selectedItemId = -1;
for (int i = 0; i < MyData.nameArray.length; i++) {
if (selectedName.equals(MyData.nameArray[i])) {
selectedItemId = MyData.id_[i];
}
}
removedItems.add(selectedItemId);
people.remove(selectedItemPosition);
adapter.notifyItemRemoved(selectedItemPosition);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.action_add_item) {
// check if any items to add
if (removedItems.size() != 0) {
addRemovedItemToList();
} else {
Toast.makeText(this, "Nothing to add", Toast.LENGTH_SHORT).show();
}
}
return true;
}
private void addRemovedItemToList() {
int addItemAtListPosition = 3;
people.add(addItemAtListPosition, new PersonData(
MyData.nameArray[removedItems.get(0)],
MyData.emailArray[removedItems.get(0)],
MyData.drawableArray[removedItems.get(0)],
MyData.id_[removedItems.get(0)]
));
adapter.notifyItemInserted(addItemAtListPosition);
removedItems.remove(0);
}
}
Probably context is null try it as:
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), MainActivity2Activity.class);
v.getContext().startActivity(myIntent);
}
In the following code I feel context is null hence you get the error change the following code:
public void onClick(View v) {
Intent myIntent = new Intent(context, MainActivity2Activity.class);
context.startActivity(myIntent);
}
change the above code to:
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity2Activity.class);
context.startActivity(myIntent);
}
or
public void onClick(View v) {
Intent myIntent = new Intent(getBaseContext, MainActivity2Activity.class);
context.startActivity(myIntent);
}
I feel what you are doing is not correct to use onClicks you need to set an interface as shown below:
#Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.itemClicked(view, getPosition());
}
}
public interface ClickListener {
public void itemClicked(View view, int position);
}
Now you need to implement the interface in your activity that will work fine. Try this.
Hope this helps.
AFAIK, RecyclerView doesn't support click events of child views of items. Any events of child views are shielded. Only RecyclerView.OnItemTouchListener is OK to listen to events of items of RecyclerView.
Hi call your adapter such like this way :
adapter = new MyAdapter(MainActivity.this,people);
recyclerView.setAdapter(adapter);
You can also get the view.getContext() inside you button onClick listener.
I hope it can help you to resolved the problem. thank you.

Categories

Resources