自定义微调适配器不工作 [英] Custom spinner adapter not working
问题描述
我有一个问题,我的自定义微调和微调适配器。它崩溃后,我启动它。我想改变文字的颜色和背景颜色在我的微调,所以我创建了一个XML文件my_spinner.xml它看起来是这样的:
I have a problem with my custom spinner and Spinner Adapter. It crashes after I launch it.I wanted to change the color and background color of the text at my spinner so I created a XML file my_spinner.xml which looks this way:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/White"
android:background="@color/Blue"
android:id="@+id/spinner_textView" >
</TextView>
</LinearLayout>
然后我做了一个MyAdapter类,如下所示:
公共类ProjectAdapter扩展ArrayAdapter
{
Then I made a MyAdapter class which looks like this: public class ProjectAdapter extends ArrayAdapter {
public ProjectAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
public View getCustomView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView) findViewById(R.id.spinner_textView);
tv.setText(projectList.get(position));
return row;
}
public View getDropDownView(int position, View convertView,ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
}
和我使用ProjectAdapter有:
and i use the ProjectAdapter there:
spinnerProject.setAdapter(new ProjectAdapter(this, R.layout.my_spinner, projectList));
有谁能够告诉我,我究竟做错了什么?感谢您的帮助。
can anybody tell me what am I doing wrong? Thank you for your help
推荐答案
您需要更改此
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView) findViewById(R.id.spinner_textView);
到
View row = inflater.inflate(R.layout.my_spinner, parent, false);
TextView tv = (TextView)row.findViewById(R.id.spinner_textView);
其中的错误。没有堆栈跟踪其难以追踪的错误。如果您还有其他问题,建议您张贴堆栈跟踪
One of the mistakes. Without stacktrace its difficult to track the mistakes. If you have further problems suggest you post the stacktrace
这篇关于自定义微调适配器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!