android中spinner怎样绑定数据
1个回答
2017-08-14
展开全部
/**
*
* 重写ArrayAdapter
*
*/
public class SpinnerAdapter extends ArrayAdapter<Product> {
private int resourceId;
public SpinnerAdapter(Context context, int textViewResourceId,
List<Product> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
Product prod = getItem(position);
LinearLayout userListItem = new LinearLayout(getContext());
TextView tv1 = new TextView(getContext());
TextView tv2 = new TextView(getContext());
// 判断
if (prod instanceof Prod_Level2) {
tv1.setText(String.valueOf(((Prod_Level2) prod)
.getProd_Level2_Cd()));
tv2.setText(((Prod_Level2) prod).getProd_Level2_NM());
} else if (prod instanceof Prod_Level3) {
tv1.setText(((Prod_Level3) prod).getProd_Level3_Cd() + "");
tv2.setText(((Prod_Level3) prod).getProd_Level3_NM());
} else if (prod instanceof Prod_Level4) {
tv1.setText(((Prod_Level4) prod).getProd_Level4_Cd() + "团核");
tv2.setText(((Prod_Level4) prod).getProd_Level4_NM());
} else if (prod instanceof Producted_Type) {
tv1.setText(((Producted_Type) prod).getProducted_Type_Cd() + "");
tv2.setText(((Producted_Type) prod).getProducted_Type_NM());
}
tv1.setVisibility(View.GONE);
userListItem.addView(tv1);
tv2.setTextSize(20);
tv2.setTextColor(Color.BLACK);
userListItem.addView(tv2);
return userListItem;
}
}
/**
* spinner的加载
*
* @param list
* :要加载的数据源
* @param sp
* :加载控件和穗
* @param context
* :activity
*/塌棚掘
public void loadSpinner(List<Product> list, Spinner sp, Context context) {
SpinnerAdapter adapter = new SpinnerAdapter(context,
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询