我对编程相对陌生,最近开始使用android。我整天都在为一个旋转器的问题伤透脑筋。我读了几个类似的问题,找到了几个解决方案和例子,但都不适合我:(
package com.deitel.welcome;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
public class Preferences extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preferences, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_profile) {
Intent intent = new Intent(Preferences.this, Profile.class);
startActivity(intent);
}
else if (id == R.id.action_home) {
Intent intent = new Intent(Preferences.this, HomeScreen.class);
startActivity(intent);
}
else if (id == R.id.action_calendar) {
Intent intent = new Intent(Preferences.this, CalendarActivity.class);
startActivity(intent);
}
else if (id == R.id.action_statistics) {
Intent intent = new Intent(Preferences.this, Statistics.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public class MainActivity extends Activity {
private String[] states;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
states = getResources().getStringArray(R.array.countries_list);
spinner = (Spinner) findViewById(R.id.country_spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
}
这是我的customOnItemSelected java文件:
package com.deitel.welcome;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"On Item Select : n" + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这是我的资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
</resources>
这当然是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/country_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/country_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/country_label" />
</LinearLayout>
有人能认出我做错了什么吗?
问候!Rosa
您的代码片段:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
添加没有任何逻辑的监听器,因为您没有;不要提供任何内部覆盖方法。您应该添加这样的自定义侦听器:spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener())
您在所选的on项目中没有执行任何操作。试着在主活动的选定项上添加一条toast消息,既然可以使用默认侦听器,为什么需要创建一个自定义侦听器?
在你的主要活动中这样尝试:
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(parent.getContext(),"spinner clicked",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
只需尝试在默认侦听器中添加toast,或者设置您制作的自定义侦听器。你没有把它设置为你的听众,因此什么都没有发生。
简单Spinner
示例和Onclick listner
String[] tracks_time = { "3min", "5min", "10min" };
Spinner spinner_time = (Spinner)promptsView.findViewById(R.id.sp_time);
ArrayAdapter<String> sptime = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, tracks_time);
spinner_time.setAdapter(sptime);
spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position ,long arg3)
{
int index = parent.getSelectedItemPosition();
// get stuff as per index
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
谢谢大家!你是对的。。。我发现我实际上在我的活动中创建了另一个类,而在最初的活动中什么也没做。谢谢你的帮助!
我把所有的东西都复制到第一节课上,并添加了一些东西。以下是最终对我有效的代码:
public class Preferences extends Activity {
protected CharSequence[] _options = { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
protected boolean[] _selections = new boolean[_options.length];
protected Button _optionsButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
_optionsButton = (Button) findViewById(R.id.button1);
_optionsButton.setOnClickListener(new ButtonClickHandler());
}
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
showDialog(0);
}
}
@Override
protected Dialog onCreateDialog (int id) {
return new AlertDialog.Builder(this)
.setTitle("Preferred Exercise Days")
.setMultiChoiceItems(_options, _selections,
new DialogSelectionClickHandler())
.setPositiveButton("OK", new DialogButtonClickHandler())
.create();
}
}
public class DialogSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked,
boolean selected) {
Log.i("ME", _options[clicked] + " selected: " + selected);
}
}
public class DialogButtonClickHandler implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int clicked) {
switch (clicked) {
case DialogInterface.BUTTON_POSITIVE:
String selected = "";
int i = 0;
for (boolean b : _selections) {
if (b) {
selected += _options[i] + " ;";
}
i++;
}
printSelectedDays();
Toast.makeText(Preferences.this, "Selected: " + selected,
Toast.LENGTH_SHORT).show();
break;
}
}
}
protected void printSelectedDays() {
for (int i = 0; i < _options.length; i++) {
Log.i("ME", _options[i] + " selected: " + _selections[i]);
}
}
解决方案有点不同,但这实际上是我最初想要实现的,所以我希望有人能使用它。