现在页面中用一个spinner. <Spinner
android:id="@+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>我从数据库获取一个list,需要把这个List 放到spinner中。 class SpinnerAdapter extends BaseAdapter{
Context content;
List<String> result;
private SpinnerAdapter(Context content,List<String> list){
this.content=content;
this.result=list;
}
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
onItemSelectedListener = new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
LinearLayout ll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout
TextView tvn=(TextView)ll.getChildAt(0);//获取其中的TextView
String Isource=(String) tvn.getText();
Toast.makeText(MainActivity.this, Isource, 0).show();
Log.i("Isource", Isource);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
};
}
@Override
public int getCount() {
return result.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL); //设置朝向
TextView tv=new TextView(MainActivity.this);
tv.setText(result.get(position));
tv.setTextSize(18);//设置字体大小
tv.setTextColor(R.color.black);//设置字体颜色
ll.addView(tv);//添加到LinearLayout中
return ll;
}
}
List<String> rolors = new ArrayList<String>();
rolors.add("红色");
rolors.add("白色");
final Spinner spinner = (Spinner)this.findViewById(R.id.mySpinner);
SpinnerAdapter alAdapter = new SpinnerAdapter(MainActivity.this, rolors);
spinner.setAdapter(alAdapter);以上代码效果可以实现,但是在点击选项后。并没有执行,监听到数据。//获取当前选中选项对应的 是没有执行的。不知道为什么。
android:id="@+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>我从数据库获取一个list,需要把这个List 放到spinner中。 class SpinnerAdapter extends BaseAdapter{
Context content;
List<String> result;
private SpinnerAdapter(Context content,List<String> list){
this.content=content;
this.result=list;
}
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
onItemSelectedListener = new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
LinearLayout ll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout
TextView tvn=(TextView)ll.getChildAt(0);//获取其中的TextView
String Isource=(String) tvn.getText();
Toast.makeText(MainActivity.this, Isource, 0).show();
Log.i("Isource", Isource);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
};
}
@Override
public int getCount() {
return result.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL); //设置朝向
TextView tv=new TextView(MainActivity.this);
tv.setText(result.get(position));
tv.setTextSize(18);//设置字体大小
tv.setTextColor(R.color.black);//设置字体颜色
ll.addView(tv);//添加到LinearLayout中
return ll;
}
}
List<String> rolors = new ArrayList<String>();
rolors.add("红色");
rolors.add("白色");
final Spinner spinner = (Spinner)this.findViewById(R.id.mySpinner);
SpinnerAdapter alAdapter = new SpinnerAdapter(MainActivity.this, rolors);
spinner.setAdapter(alAdapter);以上代码效果可以实现,但是在点击选项后。并没有执行,监听到数据。//获取当前选中选项对应的 是没有执行的。不知道为什么。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货