Dinamic ListView problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Dinamic ListView problem

Postby sanyiii » Fri Oct 30, 2009 2:54 pm

Hi everybody,

I have two problems with the ListView component:
1. My ListView is a dynamic component, so i add and remove some items after an user input. But the the component will be updated only after i scroll it. The listview has complex items. I created an object extending from LinearLayout, and i has a load() method. This methods inflates the view from an xml file, and it's called when this component is visible on the listview (i use onscrolllistener). After the listadapter has been initialized, the listview shows the empty linearlayout, and then immediately calls the load() methods, on the visible items. But i can see only the empty view. I have tried a lot of things (see the commented lines below). What can i do? Calling again the setAdapter is not good for me, because in this case the scoll position will be on the top, not where is was.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onScroll(final AbsListView view, final int firstVisibleItem,
  4.  
  5.                 final int visibleItemCount, final int totalItemCount) {
  6.  
  7.         final ListAdapter adapter = myList.getAdapter();
  8.  
  9.         if (adapter instanceof MyListAdapter) {
  10.  
  11.                 final MyListAdapter mla = (MyListAdapter)adapter;
  12.  
  13.                 for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
  14.  
  15.                         final Object item = mla.getItem(i);
  16.  
  17.                         if (item instanceof ListViewEntry) {
  18.  
  19.                                 final ListViewEntry lve = (ListViewEntry)item;
  20.  
  21.                                 if (!lve.isLoaded()) {
  22.  
  23.                                         lve.load();
  24.  
  25.                                         // mla.notifyDataSetChanged();
  26.  
  27.                                         // mla.notifyDataSetInvalidated();
  28.  
  29.                                         // me.invalidate();
  30.  
  31.                                         // myList.invalidate();
  32.  
  33.                                 }
  34.  
  35.                         }
  36.  
  37.                 }
  38.  
  39.         }
  40.  
  41. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


2. Sometimes i create a new instance of the my listadapter after a user click on a button. For this, i use ListView.setAdapter() method. I work correctly, but sometimes it does not shows any item (after about 6-8 calls of setAdapter, and it's sure it has a lot if items every time). It's a bug or i did something wrong?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3. public void onClick(View v) {
  4.  
  5.         final LoadTask rTask = new LoadTask(this); // extends AsyncTask
  6.  
  7.         rTask.execute();
  8.  
  9. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. protected void onPostExecute(final Section result) {
  4.  
  5.     final MyListAdapter adapter = new MyListAdapter(context);
  6.  
  7.     adapter.init(result);
  8.  
  9.     myList.setAdapter(adapter);
  10.  
  11.     final int firstVisiblePosition = myList.getFirstVisiblePosition();
  12.  
  13.     final int lastVisiblePosition = myList.getLastVisiblePosition();
  14.  
  15.     for (int i = 0; i < adapter.getCount(); i++) {
  16.  
  17.          final Object item = adapter.getItem(i);
  18.  
  19.          if (item instanceof ListViewEntry) {
  20.  
  21.              final ListViewEntry lve = (ListViewEntry)item;
  22.  
  23.              if (i >= firstVisiblePosition && i <= lastVisiblePosition) {
  24.  
  25.                  lve.load();           
  26.  
  27.              }
  28.  
  29.           }
  30.  
  31.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
sanyiii
Developer
Developer
 
Posts: 42
Joined: Wed Jun 10, 2009 9:58 am

Top

Re: Dinamic ListView problem

Postby sanyiii » Mon Nov 02, 2009 1:58 pm

I solved the first problem, by calling the load() method in the adapter's getView method, but the 2. is still exists. Anyone?
sanyiii
Developer
Developer
 
Posts: 42
Joined: Wed Jun 10, 2009 9:58 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests