57890

To pass only incremented value to listview on nextpage

Question:

I am able to send data on listview in nextpage.But problem is that items with quantity 0 is also passed.I want to show items whose quantity is incremented and not all items present in listview.Here is my code-

<strong>MainActivity.java</strong>

public class MainActivity extends Activity implements SearchView.OnQueryTextListener{ Button show; ListView list_item; ArrayList<Items> itemsArrayList; SearchView searchview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_item = (ListView) findViewById(R.id.listdetails); searchview=(SearchView)findViewById(R.id.searchView); show = (Button) findViewById(R.id.btnview); itemsArrayList=new ArrayList<>(); itemsArrayList.add(new Items(1,"Book",20,0,0)); itemsArrayList.add(new Items(2,"Pen",25,0,0)); itemsArrayList.add(new Items(3,"Scale",10,0,0)); itemsArrayList.add(new Items(4,"Eraser",5,0,0)); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Trial.class); intent.putExtra("data", itemsArrayList); startActivity(intent); } }); Custom c = new Custom(this,itemsArrayList); list_item.setAdapter(c); list_item.setTextFilterEnabled(true); setupSearchView(); } public void setupSearchView() { searchview.setOnQueryTextListener(this); searchview.setQueryHint("Search Here"); } @Override public boolean onQueryTextSubmit(String s) { return false; } @Override public boolean onQueryTextChange(String s) { if (TextUtils.isEmpty(s)) { list_item.clearTextFilter(); } else { list_item.setFilterText(s); } return true; } }

<strong>Custom.java</strong>

public class Custom extends BaseAdapter implements Filterable{ Activity a; ArrayList<Items> itemsArrayList; ArrayList<Items> filtered; public Custom(Activity a, ArrayList<Items> itemsArrayList) { this.a = a; this.itemsArrayList = itemsArrayList; } @Override public int getCount() { return itemsArrayList.size(); } @Override public Object getItem(int i) { return itemsArrayList.get(i); } @Override public long getItemId(int i) { return i; } @Override public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { FilterResults results=new FilterResults(); ArrayList<Items> data=new ArrayList<Items>(); if (filtered==null) filtered=itemsArrayList; if (filtered != null && filtered.size() > 0) { for (final Items g : filtered) { if (g.getItemname().toLowerCase() .contains(charSequence.toString())) data.add(g); } results.values=data; } return results; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { itemsArrayList=(ArrayList<Items>)filterResults.values; notifyDataSetChanged(); } }; } public void notifyDataSetChanged() { super.notifyDataSetChanged(); } public class Holder{ TextView sr,item,qty,price,pl,min,rate; } @Override public View getView(final int position, final View view, ViewGroup viewGroup) { final Holder holder=new Holder(); LayoutInflater li=a.getLayoutInflater(); final View view1=li.inflate(R.layout.customlist,viewGroup,false); holder.sr=(TextView)view1.findViewById(R.id.s_no); holder.item=(TextView)view1.findViewById(R.id.i_name); holder.qty=(TextView)view1.findViewById(R.id.qty); holder.price=(TextView)view1.findViewById(R.id.pr); holder.rate=(TextView)view1.findViewById(R.id.frate); holder.pl=(TextView) view1.findViewById(R.id.pl); holder.min=(TextView) view1.findViewById(R.id.min); final Items model = itemsArrayList.get(position); holder.sr.setText(String.valueOf(itemsArrayList.get(position).getSrno())); holder.item.setText(String.valueOf(itemsArrayList.get(position).getItemname())); holder.rate.setText(String .valueOf(itemsArrayList.get(position).getFixedrate())); holder.qty.setText(String.valueOf(itemsArrayList.get(position).getQuantity())); holder.price.setText(String .valueOf(itemsArrayList.get(position).getRate())); holder.pl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { model.setQuantity(model.getQuantity() + 1); model.setRate(model.getQuantity()*model.getFixedrate()); holder.qty.setText(String.valueOf(model.getQuantity())); holder.price.setText(String.valueOf(model.getRate())); notifyDataSetChanged(); } }); holder.min.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(model.getQuantity()>0) { model.setQuantity(model.getQuantity() - 1); model.setRate(model.getQuantity() * model.getFixedrate()); holder.qty.setText(String.valueOf(model.getQuantity())); holder.price.setText(String.valueOf(model.getRate())); notifyDataSetChanged(); } } }); return view1; } }

<strong>Items.java</strong>

public class Items implements Serializable { int srno,fixedrate,quantity,rate; String itemname; public Items(int srno, String itemname, int fixedrate, int quantity, int rate) { this.srno = srno; this.itemname = itemname; this.fixedrate = fixedrate; this.quantity = quantity; this.rate = rate; } public int getSrno() { return srno; } public void setSrno(int srno) { this.srno = srno; } public String getItemname() { return itemname; } public void setItemname(String itemname) { this.itemname = itemname; } public int getFixedrate() { return fixedrate; } public void setFixedrate(int fixedrate) { this.fixedrate = fixedrate; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public int getRate() { return rate; } public void setRate(int rate) { this.rate = rate; } }

<strong>Custom_Trial.java</strong>

public class Custom_Trial extends BaseAdapter { Activity a; private ArrayList<Items> data = new ArrayList<>(); public Custom_Trial(Activity a, ArrayList<Items> data) { this.a = a; this.data = data; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } public class Holder { TextView sr, it, qty, amt; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = new Holder(); LayoutInflater li = a.getLayoutInflater(); View view = li.inflate(R.layout.customtrial, parent, false); holder.sr = (TextView) view.findViewById(R.id.head1); holder.it = (TextView) view.findViewById(R.id.head2); holder.qty = (TextView) view.findViewById(R.id.head3); holder.amt = (TextView) view.findViewById(R.id.head4); Items m = data.get(position); holder.sr.setText(String.valueOf(m.getSrno())); holder.it.setText(m.getItemname()); holder.qty.setText(String.valueOf(m.getQuantity())); holder.amt.setText(String.valueOf(m.getRate())); return view; } }

<strong>Trial.java</strong>

public class Trial extends Activity{ ListView listnew; private ArrayList<Items> data = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.trial); listnew = (ListView) findViewById(R.id.newlist); data.addAll((ArrayList<Items>) getIntent().getSerializableExtra("data")); Custom_Trial ct = new Custom_Trial(this,data); listnew.setAdapter(ct); } }

Answer1:

It's because you passing original list. You're updating values inside adapter and passing not updated list fromadapter, but original. Write method inside adapter to return your updated list.

<strong>Inside Custom.java adapter:</strong>

public ArrayList<Items> getItems(){ ArrayList<Items> quantityArrayList; Items item; for (int i = 0; i < itemsArrayList.size(); i++){ item = itemsArrayList.get(i); if (item.getQuantity() > 0) quantityArrayList.add(item); } return quantityArrayList; }

<strong>And inside MainActivity onCreate() should look like this. After clicking show button you're going to get Items from Custom Adapter whose quantity >0.</strong>

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_item = (ListView) findViewById(R.id.listdetails); searchview=(SearchView)findViewById(R.id.searchView); show = (Button) findViewById(R.id.btnview); itemsArrayList=new ArrayList<>(); itemsArrayList.add(new Items(1,"Book",20,0,0)); itemsArrayList.add(new Items(2,"Pen",25,0,0)); itemsArrayList.add(new Items(3,"Scale",10,0,0)); itemsArrayList.add(new Items(4,"Eraser",5,0,0)); Custom c = new Custom(this,itemsArrayList); list_item.setAdapter(c); list_item.setTextFilterEnabled(true); setupSearchView(); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Trial.class); List<Items> quantityList = c.getItems(); intent.putExtra("data", quantityList); startActivity(intent); } }); }

Recommend

  • passing a multidimensional array from one activity to another
  • Pass ArrayList element to the next Intent
  • Optional Capture Group Not Capturing
  • Same file with same name getting downloaded even when provided with different URLs
  • Android: Transfering Data via ContentIntent
  • PHP : Json decoding question
  • android test support flavors and build type variants?
  • Titanium - Perform Action when user clicks on Android Notification
  • ContextCompat.getcolor() going to null object reference
  • Android - dlopen failed: file offset for the library
  • Home screen shortcut to activity
  • How to detect airplane mode especially in jelly beam?
  • ListView repeating the data on load more
  • as3 addChild into a generated triangle
  • Android RecyclerView Blank Space
  • JSPM - Are there any advantages/disadvantages in including client side library files using import vs
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Sending email using standard gmail app without chooser
  • Send data from edittext to listview
  • Connecting bluetooth device to windows phone 8 application
  • Splash Screen will not display
  • How can go in last activity with resume button?
  • Android Google Maps API v2 start navigation
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Google Custom Search with transparent background
  • C++ Partial template specialization - design simplification
  • NHibernate Validation Localization with S#arp Architecture
  • Bug in WPF DataGrid
  • Redux, normalised entities and lodash merge
  • how does django model after text[] in postgresql [duplicate]
  • How to stop GridView from loading again when I press back button?