Selected Item has to be added to cart in android - android

Already I have some static data in expanded list view and from that list if any customer click on that multiple items they has to be selected and it has to be added into cart.So for that please give me some suggestions and if u have any implemented code please post here.
Thanks in advance.

Assume there is two button for add and remove item on cart screen so both have click event on adapter class below is just sample example
holder.imgAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCartDetail mCartDetail;
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
int finalMmaxBuy = 0;
if (!mCartDetail.categoryProductDetail.max_buy_qty.equalsIgnoreCase(" ")) {
finalMmaxBuy = Integer.parseInt(mCartDetail.categoryProductDetail.max_buy_qty);
}
if (mCartDetail.addQuantity < finalMmaxBuy) {
mCartDetail.addQuantity++;
}
} else {
mCartDetail = new mCartDetail();
mCartDetail.categoryProductDetail = mcategoryProductDetail.productdetails.get(0);
mCartDetail.addQuantity = 1;
Utility.mCartList.put(mcategoryProductDetail.productdetails.get(0).psid, mCartDetail);
}
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
}
});
holder.imgRemoveItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
mCartDetail.addQuantity--;
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
if (mCartDetail.addQuantity == 0) {
Utility.mCartList.remove(mCartDetail.categoryProductDetail.psid);
notifyDataSetChanged();
}
}
});
and below is my model class and hashmap for data storing and send to server
public static HashMap<String, CartDetail> mCartList;
public CartDetail mCartDetail;
Hope this concept will help you to implement in your scenario

Related

Toggle Button Recyclerview Issue

i've this issue made me crazy. When retrieved JSON data i will use this my adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof OriginalViewHolder) {
final DomandaQuizTipo1 domandaQuiz1 = items.get(position);
OriginalViewHolder vItem = (OriginalViewHolder) holder;
/* Getting each answer from array of answer */
Risposta1 risposta1 = domandaQuiz1.getRisposte().get(0);
Risposta1 risposta2 = domandaQuiz1.getRisposte().get(1);
Risposta1 risposta3 = domandaQuiz1.getRisposte().get(2);
/* Get and set text of question */
vItem.domanda.setTypeface(typefaceRegular);
vItem.domanda.setText(domandaQuiz1.getDomanda());
/* Get and set text of answer 1 */
vItem.risposta1.setText(risposta1.getRisposta());
vItem.risposta1.setTextOn(risposta1.getRisposta());
vItem.risposta1.setTextOff(risposta1.getRisposta());
/* Get and set text of answer 2 */
vItem.risposta2.setText(risposta2.getRisposta());
vItem.risposta2.setTextOn(risposta2.getRisposta());
vItem.risposta2.setTextOff(risposta2.getRisposta());
/* Get and set text of answer 3 */
vItem.risposta3.setText(risposta3.getRisposta());
vItem.risposta3.setTextOn(risposta3.getRisposta());
vItem.risposta3.setTextOff(risposta3.getRisposta());
/* When i click the first answer uncheck 2nd and 3rd */
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(0).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta2.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the second answer uncheck 1st and 3rd */
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(1).getPunteggio();
MainActivity.UpdatePunteggioTotale();
Log.d("adapterposition", "POS" + position);
vItem.risposta1.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the third answer uncheck 1st and 2nd */
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(2).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta1.setChecked(false);
vItem.risposta2.setChecked(false);
}
});
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
My issue is this: in a recyclerview of each question that contains 3 answers to choose, when i click on one of answer (ex. first toggle button) is checked correctly but when i scroll recycler view i see other question with answer (ex. first toggle button) checked.
How can i avoid this issue? Take a look from this gif i putted below.
Check gif this please to understand
You should add a field like a selected (local not server) and check that's is selected and change item ui
if(vItem.selected==1)
vItem.risposta1.setChecked(true);
else
vItem.risposta1.setChecked(false);
if(vItem.selected==2)
vItem.risposta2.setChecked(true);
else
vItem.risposta2.setChecked(false);
if(vItem.selected==3)
vItem.risposta3.setChecked(true);
else
vItem.risposta3.setChecked(false);
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=1;
notifyItemChange(adapterPosition);
}
});
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=2;
notifyItemChange(adapterPosition);
}
});
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=3;
notifyItemChange(adapterPosition);
}
});
You have to set the status of each toggle button in onBindViewHolder.
Something like:
vItem.risposta1.setText(risposta1.getRisposta());
//...
vItem.risposta1.setChecked(risposta1.isChecked());
Also when you update the data in the OnClickListener you have to notify the adapter:
adapter.notifyDataSetChanged();

Interface callback after startactivity

I have fragment that contains recyclerview and products inside of it. When click row opens a new activity (SuggestionActivity). This activity also contains recyclerview inside of suggested products. When I click this products selecting or deselecting. Under the recyclerview I have button and when clicked this button it should be listen first recyclerview adapter and notifying data.
Ex:
ProductsFragment -> RecyclerAdapter -> Click Row -> Open Activity -> Click one or multiple rows and select -> Click Button -> Close activity and notify fragment adapter
My first recyclerview inside fragment
public class MyView extends RecyclerView.ViewHolder {
public MyView(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SuggestionActivity x = new SuggestionActivity();
x.setTab2AdapterListener(new SuggestionActivity.Tab2AdapterListener() {
#Override
public void interfaceClicked() {
Log.d("interfaceClicked", "interfaceClicked"); // This line doesn't work!
}
});
Intent i = new Intent(mContext, SuggestionActivity.class);
i.putExtra("first", String.valueOf(tag));
i.putExtra("second", String.valueOf(getAdapterPosition()));
mContext.startActivity(i);
}
});
}
}
My Interface defined on SuggestionActivity
private Tab2AdapterListener tab2AdapterListener;
public interface Tab2AdapterListener {
void interfaceClicked();
}
public void setTab2AdapterListener(Tab2AdapterListener tab2AdapterListener) {
this.tab2AdapterListener = tab2AdapterListener;
}
And Button inside SuggestionActivity
btnSendSuggestions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String suggested = "";
for (int i = 0; i < suggestedProductsList.size(); i++) {
if (suggestedProductsList.get(i).getIsSelected())
suggested += suggestedProductsList.get(i).getProductId() + ",";
}
if ( suggested.length() > 0 ) {
suggested = suggested.substring(0, suggested.length() - 1);
}
if (tab2AdapterListener != null)
tab2AdapterListener.interfaceClicked();
// tab2AdapterListener comes null
}
});

Swipe to Delete unable to bind data

I am using this 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.0' 3rd party library for recyclerview swipe to delete an item
myclick listener is as below:
holder.deleteLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
}
});
Based on my condition i wrote delete logic
public void removeItem(int position) {
Batchinfo batchinfo = abc.get(position);
Iterator it = abcd.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
Batchinfo a = (Batchinfo) pair.getValue();
if(a.getBatchId().equalsIgnoreCase(batchinfo.getBatchId()))
{
ExpandableAdapter.quantityadded.remove(pair.getKey());
break;
}
}
Here ExpandableAdapter.quantityadded is a static hashmap .
but unable to delete the view ...any help ???
try this out!
you just replace your this
holder.deleteLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
notifyDataSetChanged();
}
});

Toggle ImageView Button inside custom listview

I have a List view of ringtones with a play image view in each row for each ringtone ..
This is the view of it..
Now obviously when user clicks on a play button it should switch into pause button.. I have implemented this and it's all good so far :
Interface(In adapter)
public interface PlayPauseClick {
void playPauseOnClick(int position);
}
private PlayPauseClick callback;
public void setPlayPauseClickListener(PlayPauseClick listener) {
this.callback = listener;
}
Adapter(In getView)
Product product = (Product) mDataItems.get(position);
holder.playPause=(ImageView)v.findViewById(R.id.playPause);
holder.playPause.setImageResource(product.getPlayPauseId());
holder.playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (callback != null) {
callback.playPauseOnClick(position);
}
}
});
Activity
#Override
public void playPauseOnClick(int position) {
final Product product = productList.get(position);
if (product.paused) {
product.setPlayPauseId(R.drawable.ic_pause);
product.paused=false;
}else {
product.setPlayPauseId(R.drawable.ic_play);
product.paused = true;
}
adapter.notifyDataSetChanged();
};
Now I have a problem :
I don't know how to track the currently playing row,I mean if user wants to play a song from another row, first I have to change currently pause toggle to play, then play the new one.
Can you help me with this please !?
After some digging I found Two answers..
1: We can store the index of the current song which is played in the adapter.
So we just need to turn on pause when we click on an other button.
In Adapter :
int currentPosition = -1;
...
if (callback != null) {
callback.playPauseOnClick(position);
if (currentPosition == -1){
currentPosition = position;
} else if (currentPosition == position){
currentPosition = -1;
} else if (currentPosition != -1 && currentPosition != position){
callback.playPauseOnClick(currentPosition);
currentPosition = position;
}
}
2: Set resources for every listview data item
inside callback or anywhere can get listview data:
#Override
public void playPauseOnClick(int position) {
final Product product = productList.get(position);
if (product.paused) {
for(int i=0;i< productList.size();i++)
{
movieList.get(i).setPlayPauseId(R.drawable.ic_play);
movieList.get(i).paused = true;
}
product.setPlayPauseId(R.drawable.ic_pause);
product.paused=false;
}else {
product.setPlayPauseId(R.drawable.ic_play);
product.paused = true;
}
adapter.notifyDataSetChanged();
}
You can do this
first, declare a global variable
int lastposition=null;
then
#Override
public void playPauseOnClick(int position) {
final Product product = productList.get(position);
if(lastposition!=null)
{
Product previous = productList.get(lastposition);
previous.setPlayPauseId(R.drawable.ic_pause);
previous.paused=false;
lastposition=position;
} else lastposition=position;
if (product.paused) {
//new loadSingleView().execute(product.image_url);
//Log.d(LOG, product.image_url);
product.setPlayPauseId(R.drawable.ic_pause);
product.paused=false;
}else {
product.setPlayPauseId(R.drawable.ic_play);
product.paused = true;
}
adapter.notifyDataSetChanged();
};
Replace the above code with code in your activity
You could hold a variable "mCurrentPosition" to hold the currently playing track.
Then create two separate methods, one for play and one for pause. Simply pause the current track and play the one that was clicked.
Remember to also set the current position when you play a new track.
Something like this:
#Override
public void onClick(View v) {
if (callback != null) {
callback.pause(mCurrentPosition);
mCurrentPosition = position;
callback.play(position);
}
}
You may need to adjust it a bit as I don't know how you've setup your "callback" or where "position" is coming from. I'm assuming something like "getAdapterPosition()". But hopefully you can see how to do it by this example. If not please comment.
Edit in response to updated question:
For the interface do something like this:
public interface PlayPauseClick {
void pause(int position);
void play(int position);
}
Edit2:
#Override
void pause(int position) {
if(position != null) {
Product product = productList.get(position);
product.setPlayPauseId(R.drawable.ic_play);
product.paused = true;
adapter.notifyDataSetChanged();
}
}
#Override
void play(int position) {
// Homework exercise.
}

how to reload listview from another activity (adapter) class

i have tried all the things but i cant get proper result,i also try
adapter.clearAll()
adapter.notifyDataSetChanged()
but i cant get result when i delete row from list view its deleted from its position but when i change value of another row than the delete process working perfectly but changed value not set.getting value from database perfectly.
Here is my code please help me and tell where i m doing wrong.
Thank you in advance
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Identifier_fev = null;
String Default_Option = null;
String Quantity1 = null;
jffDatabase.open();
Cursor identifie = jffDatabase.getALL();
if (identifie.moveToPosition(position)) {
Identifier_fev = identifie.getString(identifie.getColumnIndexOrThrow("identifier"));
Default_Option = identifie.getString(identifie.getColumnIndexOrThrow("default_option"));
}
jffDatabase.delete(Identifier_fev, Default_Option);
identifier.remove(position);
title.remove(position);
defaultPrice.remove(position);
default_option.remove(position);
price.remove(position);
quantity.remove(position);
Cursor c1 = jffDatabase.getALL();
if (c1.moveToFirst()) {
do {
Quantity1 = c1.getString(c1.getColumnIndexOrThrow("quantity"));
update_quantity.add(Quantity1);
} while (c1.moveToNext());
}
for (int i = 0; i < update_quantity.size(); i++) {
Toast.makeText(ctx, "" + update_quantity, Toast.LENGTH_LONG).show();
Quantitylbl.setText(update_quantity.get(i).toString());
Toast.makeText(ctx, "set " + Quantitylbl.getText().toString(), Toast.LENGTH_LONG).show();
}
Quantity();
notifyDataSetChanged()
AddtoCartActivity.Cartcount.setText(String.valueOf(sum));
}
});
Good practice is use Interface
Create new Interface
public interface MyCustomObjectListener {
public void RefreshList();
//add parameter for delete if required ex-
//public void RefreshList(String Item_id);
}
then implement Activity by this Interface
YourActivityName extends Activity implements MyCustomObjectListener
And Implement Method
#Override
public void RefreshList() {
// Do your delete task and clear current List and get updated list task here
}
and from Base adapter onClick you can call method RefreshList like this
((YourActivityName)mContext).RefreshList();
you can delete and refresh list from #Override RefreshList

Categories

Resources