㈠ android spinner 下拉列表
你想做個spinner用來選擇國家,國家名旁邊顯示國旗?
我做過一個類似的功能,截取一段代碼給你參考一下。
你要自定義一個Adapter。
package xxxx.widget;
import java.util.List;
import java.util.Map;
import xxxx.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CountryAdapter extends BaseAdapter {
private int mResource = 0;
private int mFieldId = 0;
private boolean mNotifyOnChange = true;
private Context mContext = null;
private List<? extends Map<String, Object>> mList = null;
private LayoutInflater mInflater = null;
public CountryAdapter(Context context, List<? extends Map<String, Object>> list, int resource, int resourceId) {
mContext = context;
mList = list;
mResource = resource;
mFieldId = resourceId;
mInflater = LayoutInflater.from(mContext);
}
public CountryAdapter(Context context, List<? extends Map<String, Object>> list, int resource) {
mContext = context;
mList = list;
mResource = resource;
mInflater = LayoutInflater.from(mContext);
}
public void setList(List<? extends Map<String, Object>> list) {
mList = list;
if (mNotifyOnChange) notifyDataSetChanged();
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public Map<String, Object> getItem(int position) {
return mList == null ? null : mList.get(position);
}
@Override
public long getItemId(int position) {
return mList == null ? -1 : position;
}
public int getPosition(String code) {
for (int i = 0; i < mList.size(); i++) {
Map<String, Object>map = mList.get(i);
if (code.equals(map.get(StringUtil.KEY_CODE).toString())) {
return i;
}
}
return -1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (mList == null) return null;
Map<String, Object> map = mList.get(position);
View view;
ImageTextView imageTextView;
ImageView voiceView;
ImageView speechView;
if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
imageTextView = (ImageTextView) view;
} else {
imageTextView = (ImageTextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
throw new IllegalStateException(
"CountryAdapter requires the resource ID to be a CheckedImageTextView", e);
}
imageTextView.setKey(map.get(StringUtil.KEY_CODE).toString());
imageTextView.setImageResource(Integer.parseInt(map.get(StringUtil.KEY_FLAG).toString()));
imageTextView.setText(map.get(StringUtil.KEY_NAME).toString());
try {
voiceView = (ImageView) view.findViewById(R.id.voice);
if (voiceView != null) {
if (Boolean.parseBoolean(map.get(StringUtil.KEY_VOICE).toString()))
voiceView.setVisibility(View.VISIBLE);
else
voiceView.setVisibility(View.GONE);
}
speechView = (ImageView) view.findViewById(R.id.speech);
if (speechView != null) {
if (Boolean.parseBoolean(map.get(StringUtil.KEY_SPEECH).toString()))
speechView.setVisibility(View.VISIBLE);
else
speechView.setVisibility(View.GONE);
}
} catch (ClassCastException ignore) {
//
}
return view;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mNotifyOnChange = true;
}
public void setNotifyOnChange(boolean notifyOnChange) {
mNotifyOnChange = notifyOnChange;
}
@Override
protected void finalize() throws Throwable {
mList.clear();
super.finalize();
}
}
然後在窗體Activity的onCreate里這樣調用。創建一個HashMap的ArrayList,每個HashMap存儲一個選項的內容:國家名,國旗還有一個code。用這個ArrayList作為參數創建一個剛才自定義的Adapter的實例。再把這個Adapter傳給spinner。
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(StringUtil.KEY_CODE, "CN");
map.put(StringUtil.KEY_NAME, "China");
map.put(StringUtil.KEY_FLAG, "@drawable/cn_flag");
list.add(map);
CountryAdapter adapter = new CountryAdapter(this,list,R.layout.country_singlechoice,R.id.checked1);
spnCountry = (Spinner)findViewById(R.id.spn_Country);
if (spnCountry != null) {
spnCountry.(false);
spnCountry.setDrawingCacheEnabled(false);
spnCountry.setAdapter(adapter);
spnCountry.setSelection(adapter.getPosition("CN"));
spnCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Map<String, ?> map = adapter.getItem(position);
// do something
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
}
資源文件R.layout.country_singlechoice:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<jp.co.efficient.mTranslate.widget.ImageTextView
android:id="@+id/checked1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
android:gravity="center_vertical"
android:singleLine="true"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:ellipsize="marquee"
style="?android:attr/spinnerItemStyle" />
<ImageView
android:id="@+id/voice"
android:src="@drawable/mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
<ImageView
android:id="@+id/speech"
android:src="@drawable/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
</LinearLayout>
因為不方便給你源代碼,所以截取了一部分,還作了一些修改,所以恐怕會出錯。你按照這個思路修改一下。
㈡ 安卓開發,logcat報錯,大牛來幫忙看看啊!!~!~!~
你給一個spinner設置了一個 onclick的監聽器 不能這么用,把那個去掉就好了。
㈢ android spinner的用法請教
可以用ArrayAdapter實現。
給你你個思路吧:
List<ShangPin> spList=new ArrayList<ShangPin>(); //商品類中有兩個屬性 :物品的名稱,物 品的url;
List<String> data=new ArrayList<String>(); //這個List用來裝 物品的名稱
for (int i = 0; i < spList.size(); i++) {
data.add(spList.get(i).getSpName()); //getSpName() 物品的名稱的get方法
}
sp.setAdapter(new ArrayAdapter<String>(RegisterActivity.this, android.R.layout.simple_spinner_item, data)); //設置Adapter
//設置監聽事件
sp_sheng.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
TextView tv=(TextView) view; //這里的view是TextView類型的 , 自己可以打出來看看
for (int i = 0; i < spList.size(); i++) { //循環商品類的List
if(spList.get(i).getSpName().equals(tv.getText())){ //判斷選中項在List中對應的商品類
String uri= result.get(i).getUri(); //這樣就可以拿到Uri了
}
。。。。。。 拿到了Uri那就做自己想做的事去吧
㈣ android 的spinner是一個什麼作用的控制項
可以使用Button加ListPopupWindow實現和spinner一樣的效果,並且可以監聽到點擊事件(Spinner是不能設置點擊事件的)
布局文件,只有一個button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
代碼
public class MainActivity extends Activity {
private Button button;
private ListPopupWindow popupWindow;
private List<String> strings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
strings = new ArrayList<String>();
strings.add("item1");
strings.add("item2");
strings.add("item3");
button = (Button)findViewById(R.id.button);
popupWindow = new ListPopupWindow(this);
popupWindow.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings));
popupWindow.setAnchorView(button);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
button.setText(strings.get(position));
popupWindow.dismiss();
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.show();
}
});
}
}
㈤ android spinner怎麼用
android中的spinner控制項是表示下拉菜單的意思,按照如下步驟使用:
1、首先使用Android studio創建一個項目,如下圖:
4、最後去類中綁定控制項,並添加數據即可使用。
㈥ Android 開發 Spinner變化的問題 具體看補充。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, arrList);
sp.setAdapter(adapter);
使用上面的方法顯示列表,你將「打游戲」添加到arrList數組中再刷新就可以了。。。
你現在是將Spinner中數據寫在arrays.xml中了吧、、、
㈦ android中spinner的選中用什麼方法
1,獲取選中值要通過spinner的setOnItemSelectedListener()事件來操作,根據spinner綁定值的方式的不一樣,獲取選中值的方式略有不同。
2,如果沒有用實體層綁定數據的話,諸如通過
SimpleAdapter adapter = new SimpleAdapter(
HRInputBase_Activity.this, items, R.layout.sp_item,
new String[] { "text", "value" }, new int[] {
R.id.sp_text, R.id.sp_value });
sp_privince.setAdapter(adapter);
這種方式綁定的。獲取的方式為:
TextView ProvinceTxt = (TextView) sp_privince.getSelectedView()
.findViewById(R.id.sp_value); // 得到選中的選項Id
String codeString = ProvinceTxt.getText().toString();
如果使用的是實體層的話,在setOnItemSelectedListener()事件中根據點擊的索引值獲取相對應的實體類,然後從實體類中獲取對應得Code和Value值,相對來說,使用實體層更加的方便,代碼整潔。
3,兩者都使用的是Adapter數據集的綁定,主要是考察對adapter的知識,可以對這方面多學習一點。
㈧ 關於android開發spinner顯示的問題
spinner顯示項的數據可以是String[],也可以是arrayList。你要做的就是數據項最開始里加上一列,spinner會默認選中第一項。。。或者使用spinner.setSelection(XXX)為他設置選中項。。。
㈨ android中spinner問題
你可以不滿足條件的時候spinner .setClickable(false);設置點擊無效。
滿足條件設置為ture
㈩ android開發中怎樣獲取spinner選中的內容
1、獲取選中值要通過spinner的setOnItemSelectedListener()事件來操作,根據spinner綁定值的方式的不一樣,獲取選中值的方式略有不同。
2、如果沒有用實體層綁定數據的話,諸如通過
SimpleAdapter adapter = new SimpleAdapter(
HRInputBase_Activity.this, items, R.layout.sp_item,
new String[] { "text", "value" }, new int[] {
R.id.sp_text, R.id.sp_value });
sp_privince.setAdapter(adapter);
這種方式綁定的。獲取的方式為:
TextView ProvinceTxt = (TextView) sp_privince.getSelectedView()
.findViewById(R.id.sp_value); // 得到選中的選項Id
String codeString = ProvinceTxt.getText().toString();
如果使用的是實體層的話,在setOnItemSelectedListener()事件中根據點擊的索引值獲取相對應的實體類,然後從實體類中獲取對應得Code和Value值,相對來說,使用實體層更加的方便,代碼整潔。
3、兩者都使用的是Adapter數據集的綁定,主要是考察對adapter的知識,可以對這方面多學習一點。