在我的Android应用程序中,我试图在Spinner的click事件上显示ListView。我试图这样做,但遇到了一些问题。
单击微调器后的ListView,
String[] spinnervalues={"1","2","3","4","5","6"};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.addpassengers);
Spinner myspinner=(Spinner)findViewById(R.id.spinner);
myspinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnervalues));
public class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, int textViewResourceId, String[] objects)
{
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
{
return getCustomView(position, cnvtView, prnt);
}
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt)
{
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View myspinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView spinnertext = (TextView) myspinner
.findViewById(R.id.spinnertext);
spinnertext.setText(spinnervalues[position]);
return myspinner;
}
}
custom_spinner.xml文件,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3px" >
<TextView
android:id="@+id/spinnertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="1dip"
android:padding="3px"
android:text="JMD Grp"
android:textColor="#0022ee"
android:textSize="10dip"
android:textStyle="bold" />
<!-- Rightend Arrow -->
<ImageView
android:id="@+id/downarrowiv"
android:layout_width="15dip"
android:layout_height="15dip"
android:src="@drawable/down_arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Custom_spinnerlist.xml文件,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3px" >
<TextView
android:id="@+id/spinnertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="1dip"
android:padding="3px"
android:text="JMD Grp"
android:textColor="#0022ee"
android:textSize="10dip"
android:textStyle="bold" />
</RelativeLayout>
我经历了很多例子,但没有得到我想要的结果。
这是由于android的版本吗?
请帮忙。
感谢。
答案 0 :(得分:1)
R.layout.custom_spinner是您的微调器或列表中的项目的布局?猜猜你需要换线
View myspinner = inflater.inflate(R.layout.custom_spinner, parent, false);
类似
View myspinner = inflater.inflate(android.R.layout.simple_list_item_2, parent, false);
并更改
TextView spinnertext = (TextView) myspinner
.findViewById(R.id.spinnertext);
到
TextView spinnertext = (TextView) myspinner
.findViewById(android.R.id.text1);