0

I'm trying to get data from a SimpleAdapter, but I don't know the syntax for it. Example code:

month_selector.xml

<LinearLayout android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:text="Month of birth"
        />

    <Spinner 
        android:id="@+id/spin_birthmonth"
        android:entries="@array/birthmonth"
        />

</LinearLayout>

Activity page

public class SomeActivity extends Activity {

    SimpleAdapter adapter;
    ListView lv;
    protected List<HashMap<String, String>> fillMaps;
    protected String[] from;
    protected int[] to;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lv = (ListView) findViewById(R.id.room_list_view);
        fillMaps = new ArrayList<HashMap<String, String>>();
        from = new String[] {"title", "spinner"};
        to = new int[] {R.id.text, R.id.spin_birthmonth};
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("title", "Your month of birth");
        fillMaps.add(map);      

        adapter = new rSimpleAdapter(this, fillMaps, R.layout.month_selector, from, to);
        lv.setAdapter(adapter);

    }    
}

I've cut out some stuff to make it legible, so let me know if I'm missing anything. What I want to do is something along the lines of...

Spinner month = adapter.getSpinner(R.id.spin_month);

But I have no idea what the syntax is to get the spinner from within the adapter. I'm fine with making it a custom adapter if that would solve the problem.

Muz
  • 5,866
  • 3
  • 47
  • 65

1 Answers1

1

Since this is a ListView you will have a spinner on each ListItem. So you will have to get spinner after getting the row View. I think below code should work. But I have not tried

int first = lv.getFirstVisiblePosition();
View item = lv.getChildAt(first + rowid); //rowid is the row on which the spinner you want.    
Spinner month = (Spinner)item.findViewbyId(R.id.spin_month); 

Edit: From post This looks to work

int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // 
int wantedChild = rowid - firstPosition;
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
  return;
}
View item = lv.getChildAt(wantedChild);
Spinner month = (Spinner)item.findViewbyId(R.id.spin_month); 
Community
  • 1
  • 1
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Thanks, it worked. "getFirstVisiblePosition()" is not a really good way of implementing something like this, though. – Muz Sep 06 '12 at 06:14
  • I think if you dont use getFirstVisibleposition and you add headerView this might change the row id – nandeesh Sep 06 '12 at 06:18
  • Hmm.. spoke too soon. There's a really weird bug if you scroll down the list; it will only pick the first visible one. This is without getFirstVisiblePosition(). getFirstVisiblePosition() would sometimes pick something that isn't even on the screen and crash the app. – Muz Sep 06 '12 at 06:31
  • It selects the correct one without crashing now, but there's a bug in that it doesn't pick a view if it's scrolled off the screen. – Muz Sep 06 '12 at 07:11
  • Maybe creating a new CustomAdapter would be better then – nandeesh Sep 06 '12 at 07:41