从SimpleAdapter获取数据 [英] Getting data from SimpleAdapter
问题描述
我试图从SimpleAdapter获取数据,但我不知道它的语法。例如:code:
I'm trying to get data from a SimpleAdapter, but I don't know the syntax for it. Example code:
<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);
}
}
我剪了一些东西,使之清晰,所以让我知道如果我错过了什么。我想要做的是沿着...
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.
推荐答案
由于这是一个ListView你将有各自的ListItem好手。所以,你将有越来越排查看后能得到微调。我想下面code应该工作。不过我没试过
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);
编辑:从后这看起来工作。
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);
这篇关于从SimpleAdapter获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!