我正在尝试从微调器中选择的同一项触发事件。我找到了一个名为MySpinner的类,它扩展了Spinner,但显然我的实现有问题。
package com.example.ilija.festivalapp_makedox;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ilija.festivalapp_makedox.MySpinner;
import com.example.ilija.festivalapp_makedox.agendacalendarview.CalendarPickerController;
import java.util.ArrayList;
public class CustomSpinner extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public ArrayList<SpinnerModel> CustomListViewValuesArr=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArr1=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArr2=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArr3=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArrTest=new ArrayList<>();
/*public ArrayList<SpinnerModel> CustomListViewValuesArr1Test=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArr2Test=new ArrayList<>();
public ArrayList<SpinnerModel> CustomListViewValuesArr3Test=new ArrayList<>();*/
TextView output = null;
CustomSpinner activity = null;
NavigationView navigationView;
CustomAdapter adapter,adapter1,adapter2,adapter3,adapterTest,adapter1Test,adapter2Test,adapter3Test;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_spinner);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Movies");
setSupportActionBar(toolbar);
activity = this;
Spinner SpinnerExample = (Spinner) findViewById(R.id.spinner);
Spinner SpinnerExample1 = (Spinner) findViewById(R.id.spinner1);
Spinner SpinnerExample2 = (Spinner) findViewById(R.id.spinner2);
Spinner SpinnerExample3 = (Spinner) findViewById(R.id.spinner3);
MySpinner SpinnerExampleTest = (MySpinner) findViewById(R.id.spinnerTest);
/* Spinner SpinnerExampleTest = (Spinner) findViewById(R.id.spinnerTest);
Spinner SpinnerExampleTest1 = (Spinner) findViewById(R.id.spinner1Test);
Spinner SpinnerExampleTest2 = (Spinner) findViewById(R.id.spinner2Test);
Spinner SpinnerExampleTest3 = (Spinner) findViewById(R.id.spinner3Test);*/
output = (TextView)findViewById(R.id.output);
setListData();
setListData1();
setListData2();
setListData3();
setListDataTest();
/* setListData1Test();
setListData2Test();
setListData3Test();*/
Resources res = getResources();
adapter = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr,res);
adapter1 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1,res);
adapter2 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2,res);
adapter3 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3,res);
adapterTest = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArrTest,res);
/* adapter1Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1Test,res);
adapter2Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2Test,res);
adapter3Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3Test,res);*/
SpinnerExample.setAdapter(adapter);
SpinnerExample.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
if(i>1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExample1.setAdapter(adapter1);
SpinnerExample1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
if(i>2){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExample2.setAdapter(adapter2);
SpinnerExample2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "HERE" , Toast.LENGTH_SHORT).show();
i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
if(i>3){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView arg0) {
Toast.makeText(getApplicationContext(), (CharSequence) "HERE AGAIN", Toast.LENGTH_SHORT).show();
}
});
SpinnerExample3.setAdapter(adapter3);
SpinnerExample3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
if(i>4){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//********************************************************TEST COMPONENTS******************************************************************//
SpinnerExampleTest.setAdapter(adapterTest);
SpinnerExampleTest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), (CharSequence) "HERE", Toast.LENGTH_SHORT).show();
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(), (CharSequence) "HERE AGAIN", Toast.LENGTH_SHORT).show();
}
});
/* SpinnerExampleTest1.setAdapter(adapter1Test);
SpinnerExampleTest1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExampleTest2.setAdapter(adapter2Test);
SpinnerExampleTest2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExampleTest3.setAdapter(adapter3Test);
SpinnerExampleTest3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
*/
//**************************************************TEST COMPONENTS**************************************************************************************//
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
/* fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
public void setListData(){
String moy[] = {"DEN UNGE ZLATAN/BECOMING ZLATAN","THE SWEDISH THEORY OF LOVE","JONAS E O CIRCO SEM LONA/JONAS AND THE BACKYARD CIRCUS","ZID SMRTI, I TAKO TO/WALL OF DEATH, AND ALL",
"ПОСЛАННИКИ БОЛЬШОЙ ЗЕМЛИ/ SIBERIAN FLOATING HOSPITAL","FIGURA/FIGURE","1","2","3","1","2","3"};
for(int i=0;i<moy.length;i++){
final SpinnerModel sched = new SpinnerModel();
sched.setMovieName(moy[i]);
sched.setImage("image1"+i);
CustomListViewValuesArr.add(sched);
}
}
public void setListData1(){
String moy1[] = {"EL BOTÓN DE NÁCAR/PEARL","BRØDRE/BROTHERS 2015 110’","V LUCHAKH SOLNCA/UNDER THE SUN","KANTON JUGOSLAWIEN/LITTLE YUGOSLAVIA","1","2","3","1","2","3","1","2","3","1","2","3"};
for(int i=0;i<moy1.length;i++){
final SpinnerModel sched1 = new SpinnerModel();
sched1.setMovieName(moy1[i]);
sched1.setImage("image2"+i);
CustomListViewValuesArr1.add(sched1);
}
}
public void setListData2(){
String moy2[] = {"JUST ANOTHER DAY IN EGYPT","THE PERFECT CIRCLE","MOJ NAROBE SVET/MY WORLD IS UPSIDE DOWN","1","2","3","1","2","3","1","2","3","1","2","3"};
for(int i=0;i<moy2.length;i++){
final SpinnerModel sched2 = new SpinnerModel();
sched2.setMovieName(moy2[i]);
sched2.setImage("image3"+i);
CustomListViewValuesArr2.add(sched2);
}
}
public void setListData3(){
String moy3[] = {"BREZMEJNO/BEYOND BOUNDARIES","SOMETHING ABOUT LIFE","NA KRAYU/ON THE EDGE","JAMEY’S GEVECHT/JAMEY’S FIGHT","SCHWERELOS/ZERO-G"};
for(int i=0;i<moy3.length;i++){
final SpinnerModel sched3 = new SpinnerModel();
sched3.setMovieName(moy3[i]);
sched3.setImage("image4"+i);
CustomListViewValuesArr3.add(sched3);
}
}
//**************************************************TEST COMPONENTS**************************************************************************************//
public void setListDataTest(){
String moyTest[] = {"MovieTest1","MovieTest2","MovieTest2"};
for(int i=0;i<moyTest.length;i++){
final SpinnerModel schedTest = new SpinnerModel();
schedTest.setMovieName(moyTest[i]);
schedTest.setImage("image"+i);
CustomListViewValuesArrTest.add(schedTest);
}
}
/* public void setListData1Test(){
String moy1Test[] = {"MovieTest1","MovieTest1","Movie1","Movie1"};
for(int i=0;i<moy1Test.length;i++){
final SpinnerModel sched1Test = new SpinnerModel();
sched1Test.setMovieName(moy1Test[i]);
sched1Test.setImage("image"+i);
CustomListViewValuesArr1Test.add(sched1Test);
}
}
public void setListData2Test(){
String moy2Test[] = {"MovieTest1","MovieTest2","Movie3"};
for(int i=0;i<moy2Test.length;i++){
final SpinnerModel sched2Test = new SpinnerModel();
sched2Test.setMovieName(moy2Test[i]);
sched2Test.setImage("image"+i);
CustomListViewValuesArr2Test.add(sched2Test);
}
}
public void setListData3Test(){
String moy3Test[] = {"Movie1","Movie2","Movie3"};
for(int i=0;i<moy3Test.length;i++){
final SpinnerModel sched3Test = new SpinnerModel();
sched3Test.setMovieName(moy3Test[i]);
sched3Test.setImage("image"+i);
CustomListViewValuesArr3Test.add(sched3Test);
}
}
*/
//**************************************************TEST COMPONENTS**************************************************************************************//
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
Intent intent = new Intent(CustomSpinner.this, MainScreen.class);
startActivity(intent);
Toast.makeText(getBaseContext(), "Program", Toast.LENGTH_LONG).show();
//Set the fragment initially
// Handle the camera action
} else if (id == R.id.nav_gallery) {
Intent i = new Intent(CustomSpinner.this, CustomSpinner.class);
startActivity(i);
Toast.makeText(getBaseContext(), "Movies", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_slideshow) {
Intent intent = new Intent(CustomSpinner.this, BasicActivity.class);
startActivity(intent);
Toast.makeText(getBaseContext(), "Timeline", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
//Intent intent = new Intent(MainScreen.this, MapsActivity.class);
// startActivity(intent);
// Toast.makeText(getBaseContext(), "Maps", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
MySpinner类包含以下代码:
public class MySpinner extends Spinner {
OnItemSelectedListener listener;
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(null, null, position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
AdapterView.OnItemSelectedListener listener) {
this.listener = listener;
}
}
打开此活动时出现此错误,应用程序崩溃。
/com.example.ilija.festivalapp_makedox E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ilija.festivalapp_makedox, PID: 12914
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ilija.festivalapp_makedox/com.example.ilija.festivalapp_makedox.CustomSpinner}: java.lang.ClassCastException: android.support.v7.widget.AppCompatSpinner cannot be cast to com.example.ilija.festivalapp_makedox.MySpinner
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatSpinner cannot be cast to com.example.ilija.festivalapp_makedox.MySpinner
at com.example.ilija.festivalapp_makedox.CustomSpinner.onCreate(CustomSpinner.java:56)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我猜layout.activity_custom_spinner.xml是这样的:
<Spinner id="@+id/spinnerTest" ...>
它应该是这样的:
<MySpinner id="@+id/spinnerTest" ...>
而MySpinner可以扩展android.support.v7.widget.AppCompatSpinner。
https://developer.android.com/reference/android/support/v7/widget/AppCompatSpinner.html
在布局中使用Spinner时,将自动使用此选项。 编写自定义时,您只需要手动使用此类 视图。