0

我正在尝试从 SimpleAdapter 获取数据,但我不知道它的语法。示例代码:

月份选择器.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>

活动页面

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);

    }    
}

我已经剪掉了一些东西以使其清晰易读,所以如果我遗漏了什么,请告诉我。我想做的是类似于...

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

但我不知道从适配器中获取微调器的语法是什么。如果可以解决问题,我可以将其设为自定义适配器。

4

1 回答 1

1

由于这是一个 ListView,您将在每个 ListItem 上都有一个微调器。因此,在获得行视图后,您将不得不获得微调器。我认为下面的代码应该可以工作。但我没试过

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); 

编辑:从帖子这看起来可行

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); 
于 2012-09-06T05:55:07.950 回答