onItemSelected()方法應該返回一個視圖作爲它的一個對象,在這種情況下它是一個textView,通過獲取描述和對象在Logcat中的散列,因此視圖確實是一個TextView。通過此處顯示的方法返回的視圖如何從onItemSelected()方法返回的視圖對象中獲取字符串
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
如何獲取存儲在該視圖中的文本字符串?例如,如果你這樣做,
view.getText();
它應該返回存儲在TextView的字符串,但不工作
但是我已經嘗試了許多不同的東西像鑄造視圖的TextView得到從視圖中存儲的字符串,並沒有這些工作。我失敗的嘗試之一就是這個樣子這裏
((TextView) view).getText()
我怎樣才能從一個由onItemSelected回調方法返回的視圖中的字符串?
一個ArrayList裝有字符串,並放入這裏顯示適配器,
public class SpinnerAdapter extends ArrayAdapter<String>{
ArrayList<String> objects;
Context context;
int textViewResourceId;
public SpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.objects = objects;
}
spinnerOne.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(StandardSelectionSettingsSmallTank.this, "id returned "+ Long.toString(id) , Toast.LENGTH_SHORT).show();
Toast.makeText(StandardSelectionSettingsSmallTank.this, "view returned "+ ((TextView) view).getText() , Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
編輯:我只是嘗試了下面的代碼,它是做什麼我需要做。它獲取存儲在微調器當前位置的字符串。我之前用ArrayList加載的字符串。這是工作,所以我想我會用這個,而不是使用由onItemSelected方法返回的View對象。
String selection = spinnerOne.getSelectedItem()。toString();
我會用這個,除非任何人有如何使用視圖對象
您是否試圖從微調器中選擇字符串? –
是的,.getSelectedItem()爲此目的很好,我決定使用它,它比使用視圖更好。 – Kevik
對於任何混淆抱歉,有時我正在尋找的答案不是預期的答案。如果我仍然有興趣弄清楚如何從視圖對象中獲取字符串內容,但現在滿意我不需要那樣做。我寧願明白如何做到這一點 – Kevik