android - How to maintain scroll position of listview when the adapter update -


i bind adapter listview , @ first have fetch 10 records when scroll ends again items added in listview , works perfect.

but when added items after scroll end @ time items added listview position set top. want set position new added items.

here codes:

onscroll function:

public void onscroll(abslistview view, int firstvisibleitem,                     int visibleitemcount, int totalitemcount) {                    new items().execute(); } 

async function:

class items extends asynctask<string, string, string> {      @override     protected void onpreexecute() {         ...     }      protected string doinbackground(string... args) {           list<namevaluepair> params = new arraylist<namevaluepair>();         params.add(new basicnamevaluepair("id", string.valueof(id)));          jsonobject json = jparser.makehttprequest(url, "get",                 params);           try {               (int = 0; < products.length(); i++) {                  jsonobject c = products.getjsonobject(i);                  // storing each json item in variable                 string id = c.getstring(tag_pid);                 string name = c.getstring(tag_name);                  product item = new product(name, imageurl);                 product_name.add(item);              }          }     } catch (jsonexception e) {         e.printstacktrace();     }     return null; }  protected void onpostexecute(string file_url) {     // dismiss dialog after getting products     pdialog.dismiss();       adapter = new categoryadapter(context, product_name);      category_linear.setadapter(adapter);  } 

baseadapter:

public class categoryadapter extends baseadapter {      context context;      // protected list<string> m_list;     protected list<product> rowitems;      public categoryadapter(context context, list<product> items) {         this.context = context;         this.rowitems = items;     }      public void additemall(list<product> item) {         //         rowitems.addall(item);         notifydatasetchanged();     }       @override     public int getcount() {         // todo auto-generated method stub         return rowitems.size();     }      @override     public object getitem(int position) {         return rowitems.get(position);     }      @override     public long getitemid(int position) {         // todo auto-generated method stub         return 0;     }      @override     public view getview(final int position, view convertview,                         viewgroup parent) {          // todo auto-generated method stub          if (convertview == null) {             convertview = layoutinflater.from(getactivity()).inflate(                     r.layout.row, null);          }          product rowitem = (product) getitem(position);          textview text = viewholderpattern.get(convertview, r.id.item_name);          text.settext(rowitem.gettitle());          return convertview;     } } 

now how can set position of scroll while new items added.

currently when new items added go top of listview.

but want set position new items starting.

how can this?

you creating new instance of adapter every time execute code in asynctask. i.e in onpostexecute function. instead of doing

adapter = new categoryadapter(context, product_name); category_linear.setadapter(adapter); 

you should doing

adapter.additemall(product_name); adapter.notifydatasetchanged(); 

also write following

adapter = new categoryadapter(context, new list<product>()); category_linear.setadapter(adapter); 

just after instantiate listview make work.

the data updated in list , position remain same.

cheers!


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -