On Nov 18, 4:15 am, Mark Murphy <[email protected]> wrote:
> On Thu, Nov 17, 2011 at 10:01 PM, eehouse <[email protected]> wrote:
> > As far as I can tell, a second call tosetListAdapter( new
> > MyCustomAdapter() ) has no effect: the adapter I originally installed
> > in my ListActivity subclass's onCreate() method continues to get
> > called. Is this a known problem/feature?
>
> No.
>
> Here is an example of an application that successfully callssetListAdapter()
> multiple times, based on the user choosing a Reset
> option menu choice:
>
> https://github.com/commonsguy/cw-android/tree/master/Menus/Inflation
Thanks Mark. I took a look at your code, then modified it to
duplicate the situation I described: added a custom Adapter extending
BaseAdapter and implementing SectionIndexer. As you said, when I
create a new instance of the Adapter and pass it into setListAdapter()
the new instance is used. So the problem I described does not exist.
However, the problem that led to my mistaken assumption is duplicated
in your code (as I've modified it): only the first instance of my
custom Adapter ever gets its getSections() method called. So though
the new Adapter has replaced the original, the Object[] of sections
returned by the original continues to be used.
Does anybody know if there's a way to force the framework to call
getSections() when a new Adapter replaces the one that was called
originally?
I've worked around this problem by creating a new ListActivity
instance each time my data set changes. It's a bit heavy handed but
does the trick.
Thanks,
--Eric
PS I'll append my diffs below for the sake of completeness. The
"reset" menu is what triggers new data.
diff --git a/Menus/Inflation/src/com/commonsware/android/inflation/
InflationDemo.java b/Menus/Inflation/src/com/commonsware/android/
inflation/InflationDemo.java
index 0e182ba..b55a84f 100644
--- a/Menus/Inflation/src/com/commonsware/android/inflation/
InflationDemo.java
+++ b/Menus/Inflation/src/com/commonsware/android/inflation/
InflationDemo.java
@@ -1,3 +1,5 @@
+/* -*- compile-command: "cd ../../../../../; ant debug install"; -*-
*/
+
/***
Copyright (c) 2008-2011 CommonsWare, LLC
@@ -24,23 +26,73 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
+import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
+import android.widget.TextView;
+import android.widget.SectionIndexer;
+
+import android.widget.BaseAdapter;
public class InflationDemo extends ListActivity {
- private static final String[] items={"lorem", "ipsum", "dolor",
- "sit", "amet", "consectetuer", "adipiscing", "elit",
- "morbi", "vel", "ligula", "vitae", "arcu", "aliquet",
- "mollis", "etiam", "vel", "erat", "placerat", "ante",
- "porttitor", "sodales", "pellentesque", "augue", "purus"};
- private ArrayList<String> words=null;
+ private static final int NUM_WORDS = 1000;
+ private static final int NUM_INDICES = 10;
+ private int m_counter = 1;
+
+ private class MyListAdapter extends BaseAdapter
+ implements SectionIndexer {
+ private ArrayList<String> m_words;
+
+ public MyListAdapter() {
+ super();
+ }
+
+ public int getCount() { return NUM_WORDS; }
+
+ public Object getItem( int position )
+ {
+ TextView text = new TextView( InflationDemo.this );
+ text.setText( String.format( "%d",
+ position + (NUM_WORDS *
m_counter) ) );
+ return text;
+ }
+
+ public View getView( int position, View convertView, ViewGroup
parent ) {
+ return (View)getItem( position );
+ }
+
+ public long getItemId( int position ) { return position; }
+
+ public int getPositionForSection( int section )
+ {
+ return section * NUM_WORDS / NUM_INDICES;
+ }
+
+ public int getSectionForPosition( int position )
+ {
+ return position * NUM_INDICES / NUM_WORDS;
+ }
+
+ public Object[] getSections()
+ {
+ String[] sections = new String[NUM_INDICES];
+ for ( int ii = 0; ii < NUM_INDICES; ++ii ) {
+ int section = (NUM_WORDS * m_counter)
+ + (ii * (NUM_WORDS/NUM_INDICES));
+ sections[ii] = String.format( "%d", section );
+ }
+ return sections;
+ }
+
+ }
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
initAdapter();
+ getListView().setFastScrollEnabled( true );
registerForContextMenu(getListView());
}
@@ -59,55 +111,22 @@ public class InflationDemo extends ListActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
+ switch (item.getItemId()) {
case R.id.add:
- add();
- return(true);
+ add();
+ return(true);
case R.id.reset:
- initAdapter();
- return(true);
- }
+ ++m_counter;
+ initAdapter();
+ return(true);
+ }
- return(super.onOptionsItemSelected(item));
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterView.AdapterContextMenuInfo info=
- (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
- ArrayAdapter<String>
adapter=(ArrayAdapter<String>)getListAdapter();
-
- switch (item.getItemId()) {
- case R.id.cap:
- String word=words.get(info.position);
-
- word=word.toUpperCase();
-
- adapter.remove(words.get(info.position));
- adapter.insert(word, info.position);
-
- return(true);
-
- case R.id.remove:
- adapter.remove(words.get(info.position));
-
- return(true);
- }
-
- return(super.onContextItemSelected(item));
+ return(super.onOptionsItemSelected(item));
}
private void initAdapter() {
- words=new ArrayList<String>();
-
- for (String s : items) {
- words.add(s);
- }
-
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, words));
+ setListAdapter(new MyListAdapter());
}
private void add() {
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en