how to reload listview from another activity (adapter) class - android

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

Related

Get value from recycler view Adapter to activity

I have the layout as below. And I want to get all the data from the "quantity view" (from textview between "+" and "-" button) to my activity after the "proceed" button click.
I tried few of the answers but could not get the expected result. Can I get Some Help.
I have added a interface on adapter...
public interface OnItemClickListener {
void onItemClick(String id, String amount);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
and on the quantity change of the "quantity view" i tried to get the data from the adapter to activity
holder.quantityView.setOnQuantityChangeListener(new QuantityView.OnQuantityChangeListener() {
#Override
public void onQuantityChanged(int oldQuantity, int newQuantity, boolean programmatically) {
if (listener != null) {
listener.onItemClick(pricingPOJO.getId(), newQuantity + "");
}
}
#Override
public void onLimitReached() {
}
});
Maintain a Map of selected position to values like:
Map<Integer, Integer> positionToValueMap = new HashMap<>();
Then in OnBindViewHolder do this:
get your value like:
yourValue = positionToValueMap.get(getAdapterPosition());
set your value like:
positionToValueMap.put(getAdapterPosition(), yourValue);
then create getter method for positionToValueMap in adapter class and call the getter from your fragment/activity.
You are all done.

Saving list data composed of individual buttons

I've made a list made of TextViews and Buttons, made that when a person clicks on a button, a fragment opens and there is a list of values he can select. The problem is when i press on another button to select a value again for a different field, the previous value disappears. So the question would be how to save the fragments values, and keep it saved until the app is closed ?
priceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
PriceFragment priceFragment = new PriceFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, priceFragment).commit();
setToHideElements();
}
});
yearButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
YearFragment yearFragment = new YearFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, yearFragment).commit();
setToHideElements();
}
});
this is the year fragment
yearEndListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity().getBaseContext(), MainMenuActivity.class);
String yearTo = yearList[i].toString();
int yearTint = Integer.valueOf(yearTo);
if (combinedYear != null) {
combinedYear = combinedYear + " " + yearTo;
intent.putExtra("Years", combinedYear);
getActivity().startActivity(intent);
} else {
combinedYear = null;
combinedYear = yearTo;
}
}
});
this is the method to retrive data
private void retriveDataFromFragment(){
Intent intent = getIntent();
String fragmentDataPrice = intent.getStringExtra("PriceData");
String fragmentDataYear = intent.getStringExtra("Years");
if(fragmentDataPrice != null){
priceButton.setText(fragmentDataPrice);
} else {}
if (fragmentDataYear != null){
yearButton.setText(fragmentDataYear);
} else {}
}
I use RetriveDataFromFragment method in OnResume method.
Thank you, for your time.
you are initiating every time a new fragment so it will never retain its state. you have to use listener while closing the fragment so you can get back your data.
I got the anwser if someone else needs a similar menu, all you have to do is create a class that extends Application, and include into your manifest (the part with application tags). From there you just use getters and setters and all is well.

Selected Item has to be added to cart in 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

Custom Listener on Button click in ListView Item

I have created a Custom Listener interface for Button click in Adapter class, i followed this tutorial: http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/
Adapter:
holder.btnQtyIncrease.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position);
}
cart = cartArrayList.get(position);
holder.textViewQuantity.setTag(cart);
if(cart.getQuantity() == 9) {
Toast.makeText(context, "Already reached", Toast.LENGTH_LONG).show();
return ;
}
else
{
cart.setQuantity(cart.getQuantity() + 1);
holder.textViewQuantity.setText(String.valueOf(cart.getQuantity()));
totalPrice = cart.getQuantity() * cart.getPrice();
CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
holder.textViewTotal.setText(cart.getTotal());
}
}
});
.....
return convertView;
}
And I have implemented Listener in Activity like this:
public class CartActivity extends Activity implements customButtonListener {
......
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("CartActivity-onCreate", "onCreate");
setContentView(R.layout.activity_cart);
......
}
#Override
public void onResume()
{
super.onResume();
Log.d("CartActivity-onResume", "onResume");
}
#Override
public void onButtonClickListner(int position) {
totalPrice = CartArrayList.cartArraylist.get(position).getQuantity() * CartArrayList.cartArraylist.get(position).getPrice();
CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
subTotal = subTotal + totalPrice;
}
}
As you can see inside for loop i am setting and getting total of each and every list item, and then calculating subTotal of ArrayList items...
But whenever i do tap on btnQtyIncrease it makes change in total of list item price, but it would not effect on subTotal amount
You should change you logic in some way that it calculates the subTotal in adapter only and then just pass the final value to interface which will simply set it in then textview
So your code should look like:
holder.btnQtyIncrease.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cart = cartArrayList.get(position);
holder.textViewQuantity.setTag(cart);
if(cart.getQuantity() == 9) {
Toast.makeText(context, "Already reached", Toast.LENGTH_LONG).show();
return ;
}
else
{
cart.setQuantity(cart.getQuantity() + 1);
holder.textViewQuantity.setText(String.valueOf(cart.getQuantity()));
totalPrice = cart.getQuantity() * cart.getPrice();
CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
holder.textViewTotal.setText(cart.getTotal());
}
}
// calculate total here
if (customListner != null) {
customListner.onButtonClickListner(getTotalValue());
}
});
Define a method getTotalValue in adapter which will iterate through the array and find the subTotal value.
And then simply set it in the textView
#Override
public void onButtonClickListner(float subTotal ) {
txtView.setText(String.valueOf(subTotal));
}
Accordingly, change the interface signature as :
public void onButtonClickListner(float total);
How to update value in Activity when do change in Adapter class
To update data in Activity on btnQtyIncrease Button click. create custom event listener using interface which will trigger in Activity when any change made in Quantity.
See following example for creating custom Listener :
How to create our own Listener interface in android?

How to access values defined in AsyncTask from an activity

I have code like this
#Override
public void onCreate(Bundle savedInstanceState) {
addItemsOnSpinnerOrgaLevel();
btn_getReport.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//How can i access map and list defined in orgaLevelTask(AsyncTask)???
Like
String option = parent.getItemAtPosition(pos).toString();
int orgaCode = orgaLevelMap.get(option);
// Both are defined in AsyncTask ??
}); //end of anonymous class
} //end of onCreate()
public void addItemsOnSpinnerOrgaLevel() {
orgaLevelTask = new OrgaLevelTask(AccountReportActivity.this, spinner_orgaLevel, spinner_branch, txt_extra, txt_extra1);
orgaLevelTask.execute();
} //end of addItemsOnSpinnerOrgaLevel()
In AsyncTask onPostExecute() Method i have
#Override
protected void onPostExecute(ArrayList<OrgaLevel> result) {
super.onPostExecute(result);
if (result != null) {
addItemsOnSpinnerOrgaLevel(result);
}
dialog.dismiss();
} //end of onPostExecute()
public void addItemsOnSpinnerOrgaLevel(ArrayList<OrgaLevel> result) {
orgaLevelElementslist = new ArrayList<String>();
orgaLevelElementslist.add("All");
orgaLevelMap = new HashMap<String, Integer>();
orgaLevelMap.put("All", 0);
for (int i=0; i<result.size(); i++) {
OrgaLevel orgaLevelRecord = (OrgaLevel) result.get(i);
String key = orgaLevelRecord.getOrgaName();
String value = orgaLevelRecord.getOrgaCode();
orgaLevelMap.put(key, Integer.parseInt(value));
orgaLevelElementslist.add(key);
} //end of for()
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(accountReportActivity, android.R.layout.simple_spinner_item, orgaLevelElementslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_orgaLevel.setAdapter(dataAdapter);
setSpinnerOrgaLevelListener();
} //end of addItemsOnSpinnerOrgaLevel()
private void setSpinnerOrgaLevelListener() {
spinner_orgaLevel.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
String option = parent.getItemAtPosition(pos).toString();
int orgaCode = orgaLevelMap.get(option);
subOrgaLevelTask = new SubOrgaLevelTask(accountReportActivity, spinner_branch, orgaCode);
subOrgaLevelTask.execute();
} //end of onItemSelected()
}); //end of anonymous class
} //end of setSpinnerOrgaLevelListener()
In the subOrgaLevelTask i also have the same hash map as in this class. You can see that what i am trying to do is, put a key value in spinner. So when my btn_getReport button get click then i get the value of the selected item. Like if All is slected then i get 0 and so on. This key value thing is working. The problem is when btn_getReport get click then how can i get the value of the selected item. Because i am filling items in a background thread(In OrgaLevelTask and SubOrgaLevelTask) and my button is in Activity. So how can i do that when button get click, then i get the values from the map defined in OrgaLevelTask and SubOrgaLevelTask ?
Thanks
well, make them public in orgaLevelTask, and simply access them. orgaLevelTask should be a variable in you activity class. Have you tried it? any errors?
You must make sure orgaLevelTask members will be accessed in thread safe manner

Categories

Resources