我有一点问题,我正在开发一个Android应用程序,需要一个微调器来更新一个字符串到你的微调器选择,我把它全部工作和一切,问题是那个我需要spinner在另一个活动而不是启动活动但是当我使用与微调器不同的活动启动应用程序并同时激活数组适配器和监听器时我将视图切换到带有微调器的那个,旋转器是空的,没有选择。我不能为我的生活弄清楚这一点。 PS,抱歉我觉得无聊的奇怪的变量名称,而不是做一个正确的名称,我当场想到的东西,然后回来改变它。 PPS带有微调器的内容视图的名称是orderpage,我想要从应用程序开始的名称是activity_main PPPS我删除了一些我认为无关的方法,因此有一些未使用的变量。我在下面附上了我的代码:
package com.NIQUOLI.Deliveryapp;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.NIQUOLI.Deliveryapp.GMailSender;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Debug;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
public static boolean deeznuts = false;
public static String storenamestring;
public static String namesame;
public static String instructionsame;
public int choice = 0;
private Spinner spinner1;
private Button button = null;
String[] stores;
Spinner sp;
public void cont(View View){
deeznuts = true;
setContentView(R.layout.orderpage);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.orderpage);
button = (Button) findViewById(R.id.send_email);
super.onCreate(savedInstanceState);
sp = (Spinner) findViewById(R.id.sp);
stores = getResources().getStringArray(R.array.stores);
if (deeznuts = true){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,stores);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You selected"+stores[index], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
parent.getItemAtPosition(pos);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当然我的XML与s [ommer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.NIQUOLI.Deliveryapp.SendMailTask" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="@string/order_page"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal"
android:hint="@string/enter_your_name"
android:inputType="text" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/sp"
android:layout_width="388dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/instructions"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.34"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/send_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/send_order" />
</LinearLayout>
答案 0 :(得分:0)
在第一个代码示例中onCreate方法的第6行,if(deeznuts = true)
应为if(deeznuts == true)
。