无法解析setOnItemSelectedListener

时间:2017-10-12 13:36:32

标签: java android

我的目标是从微调器列表中创建项目以更改main_activity的背景颜色。

但由于某种原因,我无法工作setOnItemSelectedListener

Spinner工作正常,吐司也是,但setOnItemSelectedListener收到错误:“无法解析符号......”

无法在线找到适用于我的案例的任何帮助。

请参阅以下代码:

  import android.graphics.Color;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Spinner;
  import android.widget.AdapterView;
  import android.widget.TextView;
  import android.widget.Button;
  import android.widget.ToggleButton;
  import android.widget.ArrayAdapter;
  import android.widget.Toast;
  import java.util.*;

 public class MainActivity extends AppCompatActivity implements 
 AdapterView.OnItemSelectedListener {

private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;

public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tekst1 = (TextView) findViewById(R.id.tekst);
    klawisz1 = (Button) findViewById(R.id.button);
    onOff = (ToggleButton) findViewById(R.id.toggleButton);
    spinner1 = (Spinner) findViewById(R.id.spinner);

    spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
    spinner1.setOnItemSelectedListener(this);



    List<String> colors = new ArrayList<String>();
    colors.add("White");
    colors.add("Yellow");




    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    onOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if ((++ile%2)==0) {
                spinner1.setVisibility(View.VISIBLE);
            }
            else{
                spinner1.setVisibility(View.GONE);
            }
        }
    });
}
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{

public void onItemSelected(AdapterView<?> parent, View view, int position, 
long id) {
    // On selecting a spinner item
    //setActivityBackgroundColor(Color.YELLOW);
    String item = parent.getItemAtPosition(position).toString();

    if(selectedItem.equals("White"))
    {
        setActivityBackgroundColor(Color.WHITE);
    }
    else if(selectedItem.equals("Yellow"))
    {
        setActivityBackgroundColor(Color.YELLOW);
    }

    // Showing selected spinner item
    //Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

});
  public void buttonOnClick(View view) {
    if ((++ile%2)==0){
        tekst1.setText(new Date().toString());
        klawisz1.setBackgroundColor(Color.RED);
    }
    else
    {
        tekst1.setText("Next click - TIME");
        klawisz1.setBackgroundColor(Color.GREEN);
    }
}

4 个答案:

答案 0 :(得分:0)

为您的Spinner尝试以下代码:

mTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // Do your stuff at item selection time.
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                  // Intentionally kept blank
            }
        });

答案 1 :(得分:0)

你为旋转器1设置了两次setOnItemSelectedListener

 \\ spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) 
  this);          \\not required

   spinner1.setOnItemSelectedListener(this);

你应该只为spinner1

设置1个setOnItemSelectedListener

答案 2 :(得分:0)

这段代码对我来说很好。试试吧......

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {

List<String> colors;
private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;

public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tekst1 = (TextView) findViewById(R.id.tekst);
    klawisz1 = (Button) findViewById(R.id.button);
    onOff = (ToggleButton) findViewById(R.id.toggleButton);
    spinner1 = (Spinner) findViewById(R.id.spinner);

    klawisz1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonOnClick(view);
        }
    });
    spinner1.setOnItemSelectedListener(this);

    colors = new ArrayList<String>();


    colors.add("White");
    colors.add("Yellow");


    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    onOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if ((++ile % 2) == 0) {
                spinner1.setVisibility(View.VISIBLE);
            } else {
                spinner1.setVisibility(View.GONE);
            }
        }
    });
}

public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    // On selecting a spinner item
    //setActivityBackgroundColor(Color.YELLOW);
    String item = colors.get(position);

    if (item.equals("White")) {
        setActivityBackgroundColor(Color.WHITE);
    } else if (item.equals("Yellow")) {
        setActivityBackgroundColor(Color.YELLOW);
    }

    // Showing selected spinner item
    //Toast.makeText(parent.getContext(), "Selected: " + item,                 
  //Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}


public void buttonOnClick(View view) {
    if ((++ile % 2) == 0) {
        tekst1.setText(new Date().toString());
        klawisz1.setBackgroundColor(Color.RED);
    } else {
        tekst1.setText("Next click - TIME");
        klawisz1.setBackgroundColor(Color.GREEN);
    }
}
}

答案 3 :(得分:0)

}调用之前您还有一个setOnItemSelectedListener(),而且代码不在onCreate()方法之内。像spinner1.setOnItemSelectedListener(...)这样的代码需要在方法中。