简体   繁体   English

自定义微调器适配器不起作用

[英]Custom spinner adapter not working

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: 启动后它崩溃了。我想在微调器上更改文本的颜色和背景色,所以我创建了一个XML文件my_spinner.xml,它看起来像这样:

 <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>

Then I made a MyAdapter class which looks like this: public class ProjectAdapter extends ArrayAdapter { 然后,我制作了一个如下所示的MyAdapter类:公共类ProjectAdapter扩展了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);
    }

}

and i use the ProjectAdapter there: 我在那里使用ProjectAdapter:

  spinnerProject.setAdapter(new ProjectAdapter(this, R.layout.my_spinner, projectList));

can anybody tell me what am I doing wrong? 有人可以告诉我我在做什么错吗? Thank you for your help 谢谢您的帮助

You need to change this 你需要改变这个

 View row = inflater.inflate(R.layout.my_spinner, parent, false);
 TextView tv = (TextView) findViewById(R.id.spinner_textView);

to

 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. 没有stacktrace,就很难跟踪错误。 If you have further problems suggest you post the stacktrace 如果您还有其他问题,建议您发布stacktrace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM