ListView using CardView + RecyclerView is not working - android

I am new to android ,Here i trying to bind ListView by using RecyclerView and Cardview .In my case I didn't get any errors but I could not get the ListView items.
This is my adapter class
RecyclerviewAdapter_list.java
public class RecyclerviewAdapter_list extends RecyclerView.Adapter<RecyclerviewAdapter_list.MyViewHolder> {
private Context mContext;
private List<myAlarms> myAlarmsdata;
public RecyclerviewAdapter_list(Context mContext, List<myAlarms>myAlarms){
this.mContext = mContext;
this.myAlarmsdata =myAlarms;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.cardview_alarm, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myAlarms myAlarms =myAlarmsdata.get(position);
holder.mTime.setText(myAlarmsdata.get(position).getALARM_TIME());
holder.mTitle.setText(myAlarmsdata.get(position).getALARM_TITLE());
}
#Override
public int getItemCount() {
return myAlarmsdata.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTime,mTitle,mAmPm;
SwitchButton mbtn;
public MyViewHolder(View itemView) {
super(itemView);
mTime = (TextView) itemView.findViewById(R.id.mTime);
// mAmPm = (TextView) itemView.findViewById(R.id.mAmPm);
mTitle = (TextView) itemView.findViewById(R.id.mTitle);
// mbtn = (SwitchButton) itemView.findViewById(R.id.mTime);
}
}
}
AlarmActivity.java
public class AlarmActivity extends Activity {
private ImageView mAddIcon;
List<myAlarms>myAlarms;
RecyclerView recyclerView;
Database_Helper database_helper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
Utils.darkenStatusBar(this, R.color.StatusbarColor);
database_helper = new Database_Helper(this);
mAddIcon = (ImageView) findViewById(R.id.mAddIcon);
myAlarms = new ArrayList<>();
recyclerView=(RecyclerView)findViewById(R.id.mRecyclerView_id);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerviewAdapter_list recyclerviewAdapter_list =new RecyclerviewAdapter_list(this,myAlarms);
recyclerView.setAdapter(recyclerviewAdapter_list);
mAddIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
List<myAlarms> myAlarms = database_helper.getMyAlarms();
Intent intent = new Intent(AlarmActivity.this, CreateAlarm.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_up, R.anim.slide_out);
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
Can anyone tell me where I need to do the changes and where I did the mistakes .
Thanks.

You put this code in your project
//when you want horizontal
layoutManager.setOrientation(context,LinearLayoutManager.HORIZONTAL,false);
//when you want vertical
layoutManager.setOrientation(context,LinearLayoutManager.VERTICAL,false);

I add the same problem before, a solution is to remove your CardView and just add this line to a Layout:
android:background="#android:drawable/dialog_holo_light_frame"
This drawable has exactly the same design than a CardView. Besides, you don't need any implementation to use it so you can remove your CardView implementation from Gradle.
You can also check this : How to use RecyclerView and CardView

Related

Get selected item position in Recyclerview

I am using Recyclerview. I added a menu with Recyclerview. I want to make a switch case in Recyclerview. So when I click on item 1, when I click on item 2, I click on step 3. When I click on my item .. I want to call a function. Currently I tried to read the position of the items with getAdapterPosition, but the output is always -1. For example, when I click on model, when I click on model2, I want to call a function.
Main2Activity
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView navRecyclerView;
LinearLayoutManager layoutManager;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
NavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
navRecyclerView = findViewById(R.id.navRecyclerView);
navRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
navRecyclerView.setLayoutManager(layoutManager);
DividerItemDecoration divider = new DividerItemDecoration(
navRecyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.diveder_recycler));
navRecyclerView.addItemDecoration(divider);
initArray();
adapter = new NavigationAdapter(this, arrayList);
navRecyclerView.setAdapter(adapter);
private void initArray() {
NavigationDataModel model = new NavigationDataModel();
model.setIcon(R.mipmap.avatar);
model.setTitle("Hesabım");
arrayList.add(model);
NavigationDataModel model2 = new NavigationDataModel();
model2.setIcon(R.mipmap.sss3);
model2.setTitle("S.S.S");
arrayList.add(model2);
}
NavigationAdapter
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
System.out.println("asdasdadasd" + position);
}
});
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon, navIconNew;
TextView navTitle;
LinearLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navIconNew = itemView.findViewById(R.id.navIconNew);
navTitle = itemView.findViewById(R.id.navTitle);
Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// use position herer
}
});
}

ImageView in RecyclerView

In my MainActivity.java I have a sort of button to login. I also have 4 fragments, each of them containing a recycler view (imageview + textview).
The imageview is hidden, but when I press the button it becomes visibile. How can I do that? The notifyDataSetChanged() doesn't work in the adapter and I can't call it on the adapter in the fragment page, because is outside the onCreateView method.
Here's my code:
MainActivity: contains the button
Fragment Page:
public class Classici_Tab extends Fragment {
LinkedList<Book> catalog = new LinkedList<>();
RecyclerView mRecyclerView;
BookListAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.classici_tab, container, false);
fillCatalog("catalog.csv"); //fill the LinkedList
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.classici_rv);
mAdapter = new BookListAdapter(getContext(), catalog);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return fragmentView;
}
}
Adapter:
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.BookViewHolder> {
// View Holder
class BookViewHolder extends RecyclerView.ViewHolder
{
public final ImageView cover;
public final TextView title;
public final TextView author;
public final ImageView availability;
final BookListAdapter mAdapter;
public BookViewHolder(View itemView, BookListAdapter adapter) {
super(itemView);
cover = (ImageView) itemView.findViewById(R.id.cover);
title = (TextView) itemView.findViewById(R.id.title);
author = (TextView) itemView.findViewById(R.id.author);
availability = (ImageView) itemView.findViewById(R.id.availability);
MainActivity m = new MainActivity();
if(m.loggedin) availability.setVisibility(View.VISIBLE);
else availability.setVisibility(View.INVISIBLE);
this.mAdapter = adapter;
}
}
private LinkedList<Book> mBookList = new LinkedList<>();
private LayoutInflater mInflater;
boolean isVisible = false;
public BookListAdapter() {}
public BookListAdapter(Context context, LinkedList<Book> bookList) {
mInflater = LayoutInflater.from(context);
this.mBookList = bookList;
}
//IT DOESN'T WORK
public void setPicturesVisibility(boolean isVisible){
this.isVisible = true;
notifyDataSetChanged();
}
#Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mItemView = mInflater.inflate(R.layout.tab_list_item, parent, false);
return new BookViewHolder(mItemView, this);
}
#Override
public void onBindViewHolder(BookViewHolder holder, int position) {
holder.cover.setImageResource(mBookList.get(position).getCover());
holder.title.setText(mBookList.get(position).getTitle());
holder.author.setText(mBookList.get(position).getAuthor());
holder.availability.setImageResource(mBookList.get(position).getAvailability());
if(isVisible){
holder.availability.setVisibility(View.VISIBLE);
}else{
holder.availability.setVisibility(View.INVISIBLE);
}
}
#Override
public int getItemCount() {
return mBookList.size();
}
}
Not sure why you need to recreate adapter. Without recreating adapter, you can add listener to the button and in the onclick event handler, make image visible in recyclerview adapter's viewholder constructor.
public ViewHolder(View view) {
super(view);
textview = view.findViewById(R.id.textview);
imgView = view.findViewById(R.id.storeImg);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgView.setVisibility(View.VISIBLE);
}
});
}
You can find recyclerview examples here http://www.zoftino.com/android
There is no need to recreate adapter. You can add a boolean arePicturesVisible = false; in your RecyclerViewAdapter.
And make a method in adapter:
public void setPicturesVisibility(boolean areVisible){
arePicturesVisible = areVisible;
notifyDataSetChanged();
}
Then add:
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
.....
if(arePicturesVisible){
holder.image.setVisibility(View.VISIBLE);
}else{
holder.image.setVisibility(View.INVISIBLE);
}
.....
}
Then in your button onClick() method call setPicturesVisibility() from RecyclerView adapter.

Removing recyclerview items from the Adapter

I have two tabs (fragments), NewOrders and FinishedOrders, I'm populating the orders via Volley requests, now each item inside the New Orders tab has a SwipeLayout which show a clickable textview that makes the order finished, and move it to the other tab (backend stuff..), and I got this working perfectly,
The problem is when I click to finish, the recyclerview isn't updated once the request sent successfully, I have to do pull-to-refresh so it would update..! it seems easy to solve, but the issue is handling the swipelayout listener done inside onBindView method inside the adapter..!! that's only place to access it according to the library I'm using (I guess)..! on the other hand refreshing and populating the list happens in the NewOrder tab fragment..!
So how can I make the item to be removed from the list after the click and becomes updated..?!
Any thoughts..!?
My Adapter Class + ViewHolder
Note: the implemented methods in the adapter are required because of the interface of SwipeLayout library
public class OrdersDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements SwipeAdapterInterface, SwipeItemMangerInterface {
protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
public Context context;
ArrayList<OrderPresenter> orders;
public OrdersDataAdapter(ArrayList<OrderPresenter> orders, Context context) {
this.orders = orders;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_card, parent, false);
return new NewOrderVH(v);
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final OrderPresenter order = this.orders.get(position);
final NewOrderVH vh1 = (NewOrderVH) holder;
vh1.setData(orders.get(position));
mItemManger.bindView(vh1.itemView, position);
vh1.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
vh1.swipeLayout.addDrag(SwipeLayout.DragEdge.Left,
vh1.swipeLayout.findViewById(R.id.bottom_wrapper));
if (order.isFinished()) {
vh1.swipeLayout.setSwipeEnabled(false);
vh1.setBadge("DONE");
vh1.setBadgeColor(order.getBadgeColor());
} else {
vh1.finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// get the clicked item position?
final int position = vh1.getAdapterPosition();
// these responsible for the request which make the order finished
OrderPresenter order = orders.get(position);
OrderRepository.setOrderFinURL(order.getID());
OrderRepository.FinishOrder(order.getID(), context);
/*the commented three lines below didn't help with the problem*/
// notifyItemChanged(position);
// notifyItemRemoved(position);
// notifyDataSetChanged();*/
order.setStatus(order.getStatusText(Order.FINISHED));
}
});
}
}
#Override
public int getItemCount() {
return orders.size();
}
public class NewOrderVH extends RecyclerView.ViewHolder {
SwipeLayout swipeLayout;
private TextView finish;
private CardView orderCard;
TextView Badge;
private ImageView cusPic;
private TextView cusName;
private TextView CusAdress;
private TextView vendorsNum;
private TextView itemsNum;
private TextView time;
private TextView emptyView;
public NewOrderVH(View itemView) {
super(itemView);
Badge = (TextView) itemView.findViewById(R.id.badge);
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
finish = (TextView) itemView.findViewById(R.id.finish);
orderCard = (CardView) itemView.findViewById(R.id.OrderCard);
cusPic = (ImageView) itemView.findViewById(R.id.cusPic);
cusName = (TextView) itemView.findViewById(R.id.cusName);
CusAdress = (TextView) itemView.findViewById(R.id.CusAdress);
vendorsNum = (TextView) itemView.findViewById(R.id.vendorsNum);
itemsNum = (TextView) itemView.findViewById(R.id.itemsNum);
time = (TextView) itemView.findViewById(R.id.time);
emptyView = (TextView) itemView.findViewById(R.id.empty_view);
orderCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), OrderDetails.class);
v.getContext().startActivity(intent);
}
});
}
public void setData(final OrderPresenter data) {
time.setText(data.getOrderTime());
cusName.setText(data.getFullName());
vendorsNum.setText(data.getVendorsCount());
itemsNum.setText(data.getItemsCount());
CusAdress.setText(data.getFullAddress());
Picasso.with(context).load(data.getCustomerPicture()).into(cusPic);
}
public void setBadgeColor(int drawable) {
this.Badge.setBackgroundResource(drawable);
}
public void setBadge(String badge) {
this.Badge.setText(badge);
}
}
#Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
#Override
public void openItem(int position) {
}
#Override
public void closeItem(int position) {
}
#Override
public void closeAllExcept(SwipeLayout layout) {
}
#Override
public void closeAllItems() {
}
#Override
public List<Integer> getOpenItems() {
return null;
}
#Override
public List<SwipeLayout> getOpenLayouts() {
return null;
}
#Override
public void removeShownLayouts(SwipeLayout layout) {
}
#Override
public boolean isOpen(int position) {
return false;
}
#Override
public Attributes.Mode getMode() {
return null;
}
#Override
public void setMode(Attributes.Mode mode) {
}
}
My NewOrder Fragment
Note: the FinishedOrders tab (fragment) does the same thing as new order but filters the current the Finished status.
public class NewOrdersTab extends Fragment {
RecyclerView recyclerView;
OrdersDataAdapter adapter;
private SwipeRefreshLayout swiperefresh;
private TextView emptyView;
ArrayList<OrderPresenter> modelData;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.new_orders_tab_frag, container, false);
modelData = new ArrayList<>();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
swiperefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
swiperefresh.setColorSchemeResources(R.color.colorPrimary, R.color.color_error, R.color.colorInfo);
adapter = new OrdersDataAdapter(modelData, getActivity());
emptyView = (TextView) rootView.findViewById(R.id.empty_view);
recyclerView.setAdapter(adapter);
adapter.setMode(Attributes.Mode.Single);
OrderRepository.fetchOrders("awaiting-shipment", getActivity(), new DataFetch() {
#Override
public void onResponse(ArrayList<OrderPresenter> data) {
swiperefresh.setRefreshing(true);
if (data.size() != 0) {
swiperefresh.setRefreshing(true);
emptyView.setVisibility(View.GONE);
modelData.clear();
modelData.addAll(data);
adapter.notifyDataSetChanged();
} else {
emptyView.setVisibility(View.VISIBLE);
emptyView.setText(getString(R.string.No_New_Orders));
}
swiperefresh.setRefreshing(false);
}
});
return rootView;
}
}
I figured it out, I just added these two lines after I make the request..!
orders.remove(position);
notifyItemRemoved(position);
//notifyDataSetChanged(position);

Recycler View Not Refreshing

I'm doing a RecyclerView example. The thing is that when I add the first item on the list it shows the item correctly on the recyclerview but the next items I add aren't showing on the recycle view.
Please help me.
Adapter code is:
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
private ArrayList<Person> mData;
LayoutInflater inflater;
public PersonAdapter() {
}
public PersonAdapter(Context context, ArrayList<Person> data) {
inflater = LayoutInflater.from(context);
update(data);
}
public void update(ArrayList<Person> data) {
mData = data;
notifyDataSetChanged();
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
PersonViewHolder holder = new PersonViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
Person person = mData.get(position);
holder.ivImagen.setImageResource(R.drawable.ic_action_android);
holder.tvNombre.setText(person.getName());
holder.tvApellido.setText(person.getLast_name());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
ImageView ivImagen;
TextView tvNombre;
TextView tvApellido;
public PersonViewHolder(View itemView) {
super(itemView);
ivImagen = (ImageView) itemView.findViewById(R.id.cr_iv_imagen);
tvNombre = (TextView) itemView.findViewById(R.id.cr_tv_name);
tvApellido = (TextView) itemView.findViewById(R.id.cr_tv_last_name);
}
}}
Main Activity Code is:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ArrayList<Person> persons = new ArrayList<>();
EditText et_name;
EditText et_last_name;
RecyclerView rv_names;
PersonAdapter adapter;
Button btAgregar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.tv_name);
et_last_name = (EditText) findViewById(R.id.tv_last_name);
btAgregar = (Button) findViewById(R.id.bt_agregar);
rv_names = (RecyclerView) findViewById(R.id.rv_nombres);
LinearLayoutManager manager = new LinearLayoutManager(this);
rv_names.setLayoutManager(manager);
adapter = new PersonAdapter(this, persons);
rv_names.setAdapter(adapter);
btAgregar.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Person person = new Person(et_name.getText().toString(), et_last_name.getText().toString());
persons.add(person);
Toast.makeText(this, "Items: " + String.valueOf(persons.size()), Toast.LENGTH_SHORT).show();
} }
Try this code for onClick function
#Override
public void onClick(View v) {
Person person = new Person(et_name.getText().toString(), et_last_name.getText().toString());
persons.add(person);
adapter.update(persons);
Toast.makeText(this, "Items: " + String.valueOf(persons.size()), Toast.LENGTH_SHORT).show();
}
UPDATE:
In Adapter code, try the following changes..
public void update(ArrayList<Person> data) {
mData.clear();
mData.addAll(data)
notifyDataSetChanged();
}
In the onClick you are adding a person, but you are not passing the updated list to the adapter by calling the adapter's update method. Might this be the reason?
i just added called adapter.notifyDataSetChanged();

Android - Recycler View onClick Listener not Working

I have made a RecyclerView adapter and using that adapter to inflate data in recycler view , I previously used the general Method of ListView onItemClickListner but it doesn't seems to work with RecyclerView , Plese anyone help me out with this
My RecyclerView Adapter class is :
public class TimeAdaptra extends
RecyclerView.Adapter<TimeAdaptra.RecyclerViewHolder> {
ArrayList<String> time, status;
public TimeAdaptra(ArrayList<String> title, ArrayList<String> dec) {
this.time = title;
this.status = dec;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_slice, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.myTitle.setText(time.get(position));
holder.mydec.setText(status.get(position));
}
#Override
public int getItemCount() {
return time.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
}
}
}
And I am doing inflation like this :
public class Slots extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
private ArrayList<String> Time = new ArrayList<String>();
private ArrayList<String> Status = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slots);
Time.add("18:00");
Status.add("Available");
recyclerView = (RecyclerView) this.findViewById(R.id.timeslice);
manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new TimeAdaptra(Time, Status);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Make the RecyclerViewHolder implement View.OnClickListener like this:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
myTitle.setOnClickListener(this);
mydec.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.timee:
// do task
break;
case R.id.status:
//do task
break;
}
}
}
If you want to click on each item of the list then you should get the id of the root layout of the xml your are inflating and setOnClickListener() to it.

Categories

Resources