I want to put an array of names(NomUser) in the first item of the RecyclerView. So I know I need to change the String of the constructor putting String[] but then how I can put the information when I do a new PendingTrajectPOJO?
I want the names of Joan and Ousmane on the first item
Like this
Here is where I put the information
listPendingTraject.add(new PendingTrajectPOJO(nomUser,"Surname","Origin","Destination",Data));
adapter = new AdapterPendingTraject(listPendingTraject);
recyclerPendingTraject.setAdapter(adapter);
This is my constructor
public PendingTrajectPOJO(String name, String surName, String origin, String destination, String date) {
Name = name;
SurName = surName;
Origin = origin;
Destination = destination;
Date = date;
}
My adapter
public class AdapterPendingTraject extends RecyclerView.Adapter<AdapterPendingTraject.ViewHolderPendingTraject>{
ArrayList<PendingTrajectPOJO> listPendingTraject;
public AdapterPendingTraject(ArrayList<PendingTrajectPOJO> listPendingTraject) {
this.listPendingTraject = listPendingTraject;
}
#Override
public ViewHolderPendingTraject onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.pending_traject_item,null,false);
return new ViewHolderPendingTraject(view);
}
#Override
public void onBindViewHolder(ViewHolderPendingTraject holder, int position) {
holder.Name.setText(listPendingTraject.get(position).getName());
holder.Surname.setText(listPendingTraject.get(position).getSurName());
holder.Origin.setText(listPendingTraject.get(position).getOrigin());
holder.Destination.setText(listPendingTraject.get(position).getDestination());
holder.Date.setText(listPendingTraject.get(position).getDate());
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, Journey.class);
//intent.putExtra("FileName", list.get(position));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return listPendingTraject.size();
}
public class ViewHolderPendingTraject extends RecyclerView.ViewHolder {
TextView Name,Surname,Origin,Destination,Date;
public ViewHolderPendingTraject(View itemView) {
super(itemView);
Name=(TextView)itemView.findViewById(R.id.tvptName);
Surname=(TextView)itemView.findViewById(R.id.tvptSurName);
Origin=(TextView)itemView.findViewById(R.id.tvptOrigin);
Destination=(TextView)itemView.findViewById(R.id.tvptDestination);
Date=(TextView)itemView.findViewById(R.id.tvptDate);
}
}
}
What you want to do is create a public method in your adapter to pass in a new item. In this method, you need to notify the adapter that you have updated the items. I would recommend using the DiffUtil to find out which items have changed like this:
public void addNewItem(PendingTrajectPOJO p) {
ArrayList<PendingTrajectPOJO> old = new ArrayList<PendingTrajectPOJO>(listPendingTraject)
this.listPendingTraject = new ArrayList<PendingTrajectPOJO>()
this.listPendingTraject.add(p)
this.listPendingTraject.addAll(old)
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffCallback(old, this.listPendingTraject));
diffResult.dispatchUpdatesTo(this);
}
This way, when you add items, it will animate them and slide the old ones down (Since you mentioned you wanted the top item to be the new one)
Note: I have to test this later, I wrote this on the fly
In your adapter, create a custom method that accepts PendingTrajectPOJO as parameter, then update your listPendingTraject
Example:
public void addNewItem(PendingTrajectPOJO p) {
listPendingTraject.add(p);
notifyDataSetChanged();
}
Related
In the activity when user click on add client button I want to add new view to the screen which contains a spinner with list of client names retrieved from api and a button that will do some action on click.
So I thought I would use a recycleview and adapter for this but I think I'm wrong
in the activity I have the adapter
private ClientAdapter clientAdapter;
When I retrieve clients name from API I set the adapter as
clientRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
clientAdapter= new clientAdapter(clientList , this , this);
clientRecyclerView.setAdapter(podAdapter);
At this point I don't want the recycle view to render anything until user click on add new client button then I want to display one item that has spinner with client names and a button.
Then if he clicks again on add client button I want to show another spinner and button and so on.
However now I'm having 3 clients so recycleview render 3 view items which make sense.
But what the trick that I should do to achieve my goal?
Here's my adapter
public class ClintsAdapter extends RecyclerView.Adapter<ClintsAdapter.ViewHolder> {
private List<Clients> clientsList;
private EventListener listener;
public ClintsAdapter(List<Clients> clientsList, EventListener listener , Context context) {
this.clientsList = clientsList;
this.EventListener = listener;
}
#NonNull
#Override // To inflate view
public ClintsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_client, parent, false);
ViewHolder viewHolder = new ViewHolder(view, listener);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ClintsAdapter.ViewHolder holder, int position) {
ClintsAdapter = new ArrayAdapter<Client>(context, R.layout.spinner_text_view, clientsList);
ClintsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.clientSpinner.setAdapter(ClintsAdapter);
holder.clientSpinner.setTitle("Choose client");
}
#Override
public int getItemCount() {
if (clientsList == null)
return 0;
return clientsList.size();
}
public interface PODListener {
void onClick(int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SearchableSpinner clientSpinner , collectMethodSpinner;
EventListener listener;
public ViewHolder(View itemView, final EventListener listener) {
super(itemView);
this.listener = podListener;
clientSpinner = itemView.findViewById(R.id.spinner_client);
btnComment = itemView.findViewById(R.id.btn_comment);
btnComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( listener != null ) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onClick(position);
}
}
}
});
}
#Override
public void onClick(View v) {
}
}
}
and here's my list item
From comments:
The problem is I'm passing list of clients to the adapter (size of 3) then the adapter render 3 items. I don't want this behavior I want to have 0 item if user click on add I will render one item and so on
You are using a single ArrayList<Client> for two different purposes:
The list of clients to choose from in the spinner
The number of spinners to display in the RecyclerView.
These are two separate things, so you need two separate lists.
You can do that with just adding integer value for your ClientsAdapter. Set its default value as 0 and create a method for changing it's value. When you want to add new item (new Spinner and Button) use that method and notify your adapter.
Add a new field called count for your ClientsAdapter.
private int count;
Inside constructor assign its value to 0. So on start its value will be 0 and RecyclerView will show nothing.
public ClintsAdapter(List<Clients> clientsList, EventListener listener , Context context){
this.clientsList = clientsList;
this.EventListener = listener;
count = 0;
}
Change getItemCount method's return value. According to your code getItemCount returns size of your List. That List is for Spinner and has no relation with this method. Instead of returning your List's size return count.
#Override
public int getItemCount() {
return count;
}
Create a method for changing count's value. count starts with 0 (assigned it 0 inside constructor) and when you click Button (add new Spinner and Button) this method will change its value.
public void addItem(int count) {
this.count = count;
}
Whenever you click Button simply call addItem method and pass new count value and notify your clientAdapter.
addClient.setOnClickListener(v -> {
int count = clientRecyclerView.getChildCount();
clientAdapter.addItem(count+1);
clientAdapter.notifyItemInserted(count);
});
NOTE: I don't get it why you're setting podAdapter for RecyclerView.
clientRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
clientAdapter= new clientAdapter(clientList , this , this);
clientRecyclerView.setAdapter(podAdapter);
You're creating clientAdapter reference for your ClientsAdapter but while setting adapter for RecyclerView, you're using different reference (podAdapter).
Full code for ClientsAdapter:
public class ClintsAdapter extends RecyclerView.Adapter<ClintsAdapter.ViewHolder> {
private List<Clients> clientsList;
private EventListener listener;
private int count;
public ClintsAdapter(List<Clients> clientsList, EventListener listener , Context context) {
this.clientsList = clientsList;
this.EventListener = listener;
count = 0;
}
#NonNull
#Override // To inflate view
public ClintsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_client, parent, false);
ViewHolder viewHolder = new ViewHolder(view, listener);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ClintsAdapter.ViewHolder holder, int position) {
ClintsAdapter = new ArrayAdapter<Client>(context, R.layout.spinner_text_view, clientsList);
ClintsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.clientSpinner.setAdapter(ClintsAdapter);
holder.clientSpinner.setTitle("Choose client");
}
public void addItem(int count) {
this.count = count;
}
#Override
public int getItemCount() {
return count;
}
public interface PODListener {
void onClick(int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SearchableSpinner clientSpinner , collectMethodSpinner;
EventListener listener;
public ViewHolder(View itemView, final EventListener listener) {
super(itemView);
this.listener = podListener;
clientSpinner = itemView.findViewById(R.id.spinner_client);
btnComment = itemView.findViewById(R.id.btn_comment);
btnComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( listener != null ) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onClick(position);
}
}
}
});
}
#Override
public void onClick(View v) {
}
}
}
I have ActivityCourses and ActivityNewCourse. ActivityCourses include a list (recyclerview) of custom courses that user make, and ActivityNewCourse obviously is the activity where the user makes those custom courses.
ActivityNewCourse contains primitive data (course name and number of holes)
those are the data, which I pass to ActivityCourses, and make new item there in its recyclerview (so the new items name is that course name which is passed from ActivityNewCourse).
The problem I have is that ActivityNewCourse also contains recyclerview, and that contains obviously unique items. I need to get all those items, from ActivityNewCourse, and get them STORED (not shown) in that same item, where I send primitive data in ActivityCourses.
I've tried to use interface in my NewCourseAdapter, to pass those items from ActivityNewCourse recyclerview, to my ActivityCourses item, but the problem is that I need all of them items, not just 1. and also that button "Save Course" which user clicks to obviously save all the data from ActivityNewCourse, is outside of the recyclerview, where those items are located.
HERE IS MY NEW COURSE ADAPTER
public class NewCourseAdapter extends RecyclerView.Adapter<NewCourseAdapter.NewCourseViewHolder> {
private ArrayList<NewCourseItem> mNewCourseList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onMinusClick(int position);
void onPlusClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class NewCourseViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView1, mTextView2;
public ImageView mImageView1, mImageView2;
public NewCourseViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mTextView1 = itemView.findViewById(R.id.hole_number);
mTextView2 = itemView.findViewById(R.id.par_number);
mImageView1 = itemView.findViewById(R.id.item_minus_btn);
mImageView2 = itemView.findViewById(R.id.item_plus_btn);
mImageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onMinusClick(position);
}
}
}
});
mImageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onPlusClick(position);
}
}
}
});
}
}
public NewCourseAdapter(ArrayList<NewCourseItem> newCourseList) {
mNewCourseList = newCourseList;
}
#NonNull
#Override
public NewCourseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_course_item, parent, false);
NewCourseViewHolder evh = new NewCourseViewHolder(v, mListener);
return evh;
}
#Override
public void onBindViewHolder(#NonNull NewCourseViewHolder holder, int position) {
NewCourseItem currentItem = mNewCourseList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mImageView1.setImageResource(currentItem.getImageMinus());
holder.mImageView2.setImageResource(currentItem.getImagePlus());
}
#Override
public int getItemCount() {
return mNewCourseList.size();
}
}
HERES HOW I PASS PRIMITIVE DATA FROM ACTIVITYNEWCOURSE TO ACTIVITYCOURSES
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
courseName = findViewById(R.id.course_name_input);
number = findViewById(R.id.number_of_holes_number);
String intentCourseName = courseName.getText().toString().trim();
String holeNumber = number.getText().toString().trim();
Intent intent = new Intent(ActivityNewCourse.this, ActivityCourses.class);
intent.putExtra("COURSENAME", intentCourseName);
intent.putExtra("HOLENUMBER", holeNumber);
startActivity(intent);
}
});
HERES HOW ACTIVITYCOURSES RESIVE THE DATA
public void addItem() {
if (getIntent().getStringExtra("COURSENAME") != null) {
mCourselist.add(new CoursesItem(getIntent().getStringExtra("COURSENAME"), "Holes:", getIntent().getStringExtra("HOLENUMBER"), R.drawable.ic_delete));
}
}
I'm not sure if this code helped or not...
Am I somehow be able to send the whole arraylist from ActivityNewCourse, when save button been clicked? I don't know, I'm kinda dead end and I have no clue what to do, so any suggestions on what to do in this situation would help...
Have you tried using intent.putStringArrayListExtra("ARRAYNAME", arrayName);
try putting your data in an ArrayList and send it like this:
Intent intent = new Intent(ActivityNewCourse.this, ActivityCourses.class);
intent.putStringArrayListExtra("ARRAYNAME", arrayName);
startActivity(intent);
and the retrieve it like this:
ArrayList<String> listOfData = getIntent().getStringArrayListExtra("ARRAYNAME");
Hope it helps.
updated
Using Serializable:
first extend NewCourseItem to Serializable then:
Bundle bundle = new Bundle();
bundle.putSerializable("KEYNAME", new NewCourseItem("str1", "str2", "imglink", "imglink"));
intent.putExtras(bundle);
startActivity(intent);
retrieve it like this:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
NewCourseItem item = (NewCourseItem)bundle.getSerializable("KEYNAME");
I have a RecyclerView as given in picture below.
I get the food item data from the server and bind them to RecyclerView as above.
Food items are assigned in foodItemList array list which has foodItemTypeList array. In foodItemTypeList array list, values of types and their corresponding prices are stored.
What I want is when the user selects a food type (for example medium), the corresponding unit price is updated.
Here is the FoodItemAdapter class:
public class FoodItemAdapter extends RecyclerView.Adapter<FoodItemAdapter.CustomViewHolder> {
private List<FoodItem> foodItemList;
Context context;
String token;
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView food_name, food_unit_price;
Spinner food_item_type;
public ImageView food_item_image;
public Button viewDetail;
public CustomViewHolder(View view) {
super(view);
food_name = (TextView) itemView.findViewById(R.id.food_name);
food_item_type = (Spinner) itemView.findViewById(R.id.food_item_type);
viewDetail = (Button) itemView.findViewById(R.id.viewDetail);
food_unit_price = (TextView) itemView.findViewById(R.id.food_unit_price);
food_item_image = (ImageView) itemView.findViewById(R.id.food_item_image);
}
}
public FoodItemAdapter(Context context, List<FoodItem> foodItemList,String token) {
this.foodItemList = foodItemList;
this.context = context;
this.token = token;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_items_item, parent, false);
return new CustomViewHolder(itemView);
}
#Override
public void onBindViewHolder(final CustomViewHolder holder, int position) {
FoodItem foodItem = foodItemList.get(position);
holder.food_name.setText(foodItem.getFood_name());
holder.viewDetail.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
holder.food_unit_price.setText(foodItem.getFood_unit_price()+" AF");
if(foodItem.getFood_item_image()!=null && !foodItem.getFood_item_image().isEmpty()){
Picasso.get()
.load(foodItem.getFood_item_image())
// To fit image into imageView
.resize(50, 50)
.centerCrop()
.into(holder.food_item_image);
} else {
Log.d("Food Item Image:", "Food Item image is either empty or null");
}
List<FoodItemType> foodItemTypeList = new ArrayList<>();
foodItemTypeList = foodItem.getFoodItemTypeList();
ArrayAdapter userAdapter = new ArrayAdapter(context, R.layout.spinner, foodItemTypeList);
holder.food_item_type.setAdapter(userAdapter);
holder.food_item_type.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
FoodItemType foodItemType = (FoodItemType) (holder.food_item_type).getSelectedItem();
Toast.makeText(context, "Clicked: " +
foodItemType.getFood_unit_price(), Toast.LENGTH_LONG).show();
FoodItem foodItemNew = new FoodItem(foodItemList.get(holder.getAdapterPosition()).getFood_item_id(),foodItemList.get(holder.getAdapterPosition()).getFood_category_id(),foodItemList.get(holder.getAdapterPosition()).getFood_name(),foodItemList.get(holder.getAdapterPosition()).getFood_item_image(),foodItemList.get(holder.getAdapterPosition()).getFood_item_desc(), foodItemType.getFood_item_type_id(),foodItemType.getFood_item_type_name(),foodItemType.getFood_unit_price(), foodItemList.get(holder.getAdapterPosition()).getFoodItemTypeList());
foodItemList.set(holder.getAdapterPosition(),foodItemNew);
notifyItemChanged(holder.getAdapterPosition());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// todo for nothing selected
}
});
}
#Override
public int getItemCount() {
return foodItemList.size();
}
}
A part of my FoodItemsFragment class is as below:
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_food_item);
foodItemAdapter = new FoodItemAdapter(getActivity(),foodItemList, token);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(foodItemAdapter);
foodItemAdapter.notifyDataSetChanged();
My problem is: When I scroll RecyclerView, the view is loaded multiple times (a kind of lagging) and when I change the foody type (for example: to medium), nothing happens.
Any kind of help is appreciated.
I would like to suggest you have another field in your FoodItem class, which is selectedFoodType. This will hold the index of the type selected. By default, initialize the value with 0 and when an item is selected from the drop-down list, just update the corresponding FoodItem's selectedFoodType accordingly.
Another thing is, you do not have to create a new FoodItem each time you are changing the type of the FoodItem. You need to have the selectedFoodType only and then call the notifyDataSetChanged().
So the code should look like the following. Inside your onItemSelected function, do the following.
int foodType = foodItemTypeList.get(position);
FoodItem foodItem = foodItemNew.get(holder.getAdapterPosition());
foodItem.setSelectedFoodType = foodType;
notifyDataSetChanged();
Please note that I have not tested this code. Please modify as per your need. Hope that helps!
Currently had a listview where I showed:
moves
That works fine.
The problem is that now I would like to add pagination, so I show 10 "moves", and then, with an arrow or something, link to the next 10 "moves".
This is my ListView:
RecyclweView Java:
RecyclerView lstMovsWallet = (RecyclerView) findViewById(R.id.lstMovsWallet);
lstMovsWallet.setLayoutManager(new
LinearLayoutManager(MovsMobileWallet.this));
AdapterCobrosPendientesListado adapter = new
AdapterCobrosPendientesListado(MovsMobileWallet.this, items);
lstMovsWallet.setAdapter(adapter);
Adapter for de RecyclerView :
public class AdapterCobrosPendientesListado extends RecyclerView.Adapter<AdapterCobrosPendientesListado.ViewHolder> {
private LayoutInflater mInflater;
protected List<MovimientoCuenta> items;
public AdapterCobrosPendientesListado(Context context, List<MovimientoCuenta> data) {
this.mInflater = LayoutInflater.from(context);
this.items = data;
}
#Override
public AdapterCobrosPendientesListado.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_adapter_billings_listhistory, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(AdapterCobrosPendientesListado.ViewHolder holder, int position) {
DecimalFormat formater = new DecimalFormat("###.00");
String numero = items.get(position).getNumber();
String cantidad = items.get(position).getMonto();
String fecha = items.get(position).getFecha();
String referencia = items.get(position).getReferencia();
String debitoCredito = items.get(position).getDebitoCredito();
holder.number.setText(numero);
holder.mount.setText(cantidad);
holder.date.setText(fecha);
holder.ref.setText(referencia);
if(debitoCredito.compareTo("DBT")==0){
holder.title.setText("Pago");
holder.auxBilling.setImageResource(R.mipmap.signonegativo);
}
else {
holder.title.setText("Cobro");
holder.auxBilling.setImageResource(R.mipmap.signomas);
}
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView number;
public TextView mount;
public TextView date;
public ImageView auxBilling;
public TextView ref;
public TextView title ;
public ViewHolder(View itemView) {
super(itemView);
number = itemView.findViewById(R.id.txtNumberPhoneBilling);
mount = itemView.findViewById(R.id.txtMountBillingNotifications);
date = itemView.findViewById(R.id.txtDateBillingNotifications);
auxBilling = itemView.findViewById(R.id.btnCancelBillingNotifications);
ref = itemView.findViewById(R.id.txtDateBillingRef);
title = itemView.findViewById(R.id.TitleMovs);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
/* // convenience method for getting data at click position
public String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}*/
}
Here I leave the class of movements to replicate:
public class MovimientoCuenta {
private String number;
private String monto;
private String moneda;
private String fecha;
private String ID;
private String referencia ;
private String filtro ;
private String debitoCredito ;
private String nombreMov;
public MovimientoCuenta(String number, String monto, String moneda, String fecha, String ID, String referencia, String filtro, String debitoCredito,String nombreMov) {
this.number = number;
this.monto = monto;
this.moneda = moneda;
this.fecha = fecha;
this.ID = ID ;
this.filtro =filtro;
this.referencia=referencia;
this.debitoCredito =debitoCredito;
this.nombreMov =nombreMov;
}
Any help will be welcome from now thanks.
You can try adding another data model in your list at the end of the current list. Have the view type of this model be a button. On clicking this, you can fetch the next set of moves from your server using offset and limit in your query.
Make the list of a generic type say Parcelable and have all the elements in the list be child via either extending or implementing the parent type.
Eg the list can be of type class A
ArrayList<A>
And the elements in it can be of types
Class B extends A
Class C extends A
Now in your getView , check the type of data using instance of operator and inflate the correct view layout
If(yourlist.get(positionpassedingetview) instanceof B){
//Inflate the view for B item if not already inflated
}else{
//Inflate view of type C
}
For every click on the above said load more item button, use the offset as the current length of list minus one and the limit will always be 10. For the first ever query, use offset as 0 or the sql will throw an exception, rest subsequent queries will have the offset = list size minus one (minus one because the last element of the list is a load more button and not the actual data)
Eg in mysql, this could be:
Select * from yourtable where yourcondition order by yourorder limit youroffset,10;
If there's no more data, just update the model of the load more saying no more items and disabling the button. If you find more items, insert them at the poisition above the last element which is the load more button and notify the list adapter.
You will have to find a tutorial for such a heterogenous list view with more than one viewtypes and viewholders. This is a general idea of how to do it, i myself use this approach with a recyclerview with a similar logic
I am trying to use two different ArrayAdapter(separately in to two Activity) with one Arraylist. The first ArrayAdapter does the Increment for the Quantity and the second does the Decrement. Then all of the Data from the first ArrayAdapter will be transfer to the second ArrayAdapter, but only the Data with Quantity greater than 1 will be displayed. All of the previous task seems to be working fine, until I tried to Decrement a Quantity(It shows that the data decements) and try to check if changes were saved by going back to the first Activity and back again to the second Activity. What happens is that changes made by the Decrement is being ignored and resets the data from when the item was Incremented. I know it's kinda messed up, but I hope you can help me what am I doing wrong.
EDIT: I am using two ArrayAdapter because I want to display the quantity in the second adapter, along with other elements.
Here is my codes:
myProductAdapter.java(Increment Adapter)
public class myProductAdapter extends ArrayAdapter<myProduct> {
public class ViewHolder{
Button addItem;
}
public myProductAdapter(Context context, ArrayList<myProduct> myProducts) {
super(context, 0, myProducts);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final myProduct product = getItem(position);
final ViewHolder viewHolder;
//Some Codes Here..
viewHolder.addItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
product.productQuantity ++;
Toast.makeText(getContext(), "" + product.productQuantity(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
myOrderAdapter.java(Decerement Adapter)
public class myOrderAdapter extends ArrayAdapter<myProduct> {
public class ViewHolder{
Button minusItem;
}
public myOrderAdapter(Context context, ArrayList<myProduct> orders){
super(context, 0, orders);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final myProduct order = getItem(position);
viewHolder.minusItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
order.productQuantity --;
if(order.productQuantity() <= 0){
order.productQuantity = 0;
notifyDataSetChanged();
}
Toast.makeText(getContext(),"" + order.productQuantity(),Toast.LENGTH_SHORT).show();
return convertView;
}
}
myProduct.java
public class myProduct implements Parcelable {
#SerializedName("productID")
public int productID;
#SerializedName("categoryID")
public int categoryID;
#SerializedName("productName")
public String productName;
#SerializedName("productPrice")
public int productPrice;
public int productQuantity = 0;
public myProduct(int productID, int categoryID, String productName, int productPrice){
this.productID = productID;
this.categoryID = categoryID;
this.productName = productName;
this.productPrice = productPrice;
}
public int getProductID(){
return productID;
}
public int getCategoryID(){
return categoryID;
}
public String getProductName(){
return productName;
}
public int getProductPrice(){
return productPrice;
}
public int productQuantity() {
return productQuantity;
}
protected myProduct(Parcel in) {
productID = in.readInt();
categoryID = in.readInt();
productName = in.readString();
productPrice = in.readInt();
productQuantity = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(productID);
dest.writeInt(categoryID);
dest.writeString(productName);
dest.writeInt(productPrice);
dest.writeInt(productQuantity);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<myProduct> CREATOR = new Parcelable.Creator<myProduct>() {
#Override
public myProduct createFromParcel(Parcel in) {
return new myProduct(in);
}
#Override
public myProduct[] newArray(int size) {
return new myProduct[size];
}
};
}
Menu.class
viewOrder = (Button)findViewById(R.id.viewOrder);
viewOrder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Order.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("productList", productList);
i.putExtras(bundle);
startActivity(i);
}
});
Order.class
Bundle bundle = getIntent().getExtras();
productList = bundle.getParcelableArrayList("productList");
filter = new ArrayList<myProduct>();
orderAdapter = new myOrderAdapter(getApplicationContext(),filter);
for(myProduct item : productList){
if(item.productQuantity > 0){
Log.d(TAG,item.getProductName());
Log.d(TAG,"" + item.productQuantity());
filter.add(item);
}
else if(item.productQuantity <= 0){
filter.remove(item);
}
}
orderListView.setAdapter(orderAdapter);
The issue is that you are passing the array to the second adapter as a Parcelable, so it's basically making a deep copy of the array, and has nothing to do with the original array. Therefore changes in second array, don't show in first array.
A simple (but maybe dirty) solution would be to make the array as a static variable (or part of the application class, or a singleton), and use the actual array in both activities.
If you can design, that both lists are in two fragments, and part of one activity, then just have the activity hold on to the shared array for both fragments.