我正在尝试创建一个下拉菜单,将文本视图的文本更改为" Player Selected:"然后从下拉菜单中选择一个玩家。然而,当我在Android上测试应用程序时,我可以单击一个微调项目,但它没有注册任何东西。 textview没有改变,也没有记录任何内容,就好像它根本没有注册点击监听器一样。这是我的代码
package com.example.buddyfizzturtles.stattracker;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends AppCompatActivity {
EditText input;
Button showBtn;
TextView showPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Players p = new Players();
// Pop-Up Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter Name");
builder.setIcon(R.drawable.basketball);
builder.setMessage("Enter a name for your player");
input = new EditText(this);
builder.setView(input);
//Set Positive Button
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = input.getText().toString();
Player x = new Player(text);
p.addPlayer(x);
Toast.makeText(MainActivity.this, "Added " + text + " to your player list", Toast.LENGTH_SHORT).show();
}
});
//Set Negative Button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//Create Builder
final AlertDialog ad = builder.create();
//Button
showBtn = (Button) findViewById(R.id.addPlayerButton);
showBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.show();
}
});
//Spinner Items
Spinner dropNames = (Spinner) findViewById(R.id.choosePlayer);
dropNames.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d("CLICKED:", "Successful Click");
showPlayer = (TextView) findViewById(R.id.showPlayer);
showPlayer.setText("Player Selected: " + );
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
ArrayAdapter<Player> adapter = new ArrayAdapter<Player>(this, android.R.layout.simple_spinner_item, p.returnList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropNames.setAdapter(adapter);
}
}
答案 0 :(得分:0)
你可以试试这个。让我们看看它是否解决了
Spinner spinner=(Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("CLICKED:", "Successful Click");
showPlayer = (TextView) findViewById(R.id.showPlayer);
showPlayer.setText("Player Selected: " + );
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
希望这有帮助
答案 1 :(得分:0)
如果微调器正在加载数据,那么我认为这会对你有帮助(在监听器之外定义showPlayer很好)。
Spinner dropNames = (Spinner) findViewById(R.id.choosePlayer);
TextView showPlayer = (TextView) findViewById(R.id.showPlayer);
dropNames.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showPlayer.setText("Player Selected: " + String.valueOf(dropNames.getSelectedItem());
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});