好的我正在创建一个将使用2个微调器的android项目。第一个是从稳定的状态数组列表中设置的。第二个将列出该州的学校名单。学校列表存储在SQL数据库中,并由用户添加。所以,一旦我通过状态值,我就可以获得学校名单。唯一的问题是我似乎无法避免来自schoolList的空指针。这是页面的代码。任何想法都会有所帮助。
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bCancel, bRegister;
EditText etEmail, etPassword, etUserName;
Spinner stateList, schoolList;
//Scope variables strings
String uName=null,email=null,pass=null,school=null,state=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
bCancel = (Button) findViewById(R.id.bCancel);
bRegister = (Button) findViewById(R.id.bRegister);
etUserName = (EditText) findViewById(R.id.etUserName);
etPassword = (EditText) findViewById(R.id.etPassword);
etEmail = (EditText) findViewById(R.id.etEmail);
stateList = (Spinner) findViewById(R.id.stateList);
schoolList = (Spinner) findViewById(R.id.schoolList);
setState();
ArrayList<Object> defAda= new ArrayList<>();
defAda.add("Select State First");
ArrayAdapter schoollist = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, defAda);
schoolList.setAdapter(schoollist);
schoolList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
stateList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
stateList = (Spinner)parent;
state=parent.getItemAtPosition(position).toString();
if (state !=null) {
setSchool(state);
schoolList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
schoolList = (Spinner) parent;
school = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
bRegister.setOnClickListener(this);
bCancel.setOnClickListener(this);
}
public void setState(){
ArrayList<String> states=CCValueStore.getStatesList();
ArrayAdapter stateslist = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, states);
stateList.setAdapter(stateslist);
}
public void setSchool(String state){
ArrayList<Object> schools;
ServerRequests getSchool;
getSchool = new ServerRequests(this);
schools=getSchool.getSchoolUserBackground(state, new GotSchoolCallBacks() {
@Override
public ArrayList done(ArrayList<Object> returnedSchools) {
return returnedSchools;
}
});
schools.add("Not Selected");
ArrayAdapter schoollist = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, schools);
schoolList.setAdapter(schoollist);
}
@Override
public void onStart(){
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.bCancel) {
startActivity(new Intent(this, logIn.class));
}
if (v.getId() == R.id.bRegister) {
uName = etUserName.getText().toString();
pass = etPassword.getText().toString();
email = etEmail.getText().toString();
state = stateList.getSelectedItem().toString();
school = schoolList.getSelectedItem().toString();
CCuser UserData = new CCuser(uName, pass, email, state, school);
boolean success = registerUser(UserData);
if (success) {
ServerRequests registered;
registered = new ServerRequests(this);
registered.storeUserDataBackground(UserData, new StoreUser() {
@Override
public void done(boolean isOk) {
}
});
startActivity(new Intent(this, logIn.class));
}
}
}
public boolean registerUser(final CCuser userData) {
boolean forward= Boolean.parseBoolean(null);
final String[] chkE = new String[1]; final String[] chkN = new String[1];
final ServerRequests chkUser = new ServerRequests(this);
chkUser.fetchUserDataByName(userData, new CheckUserName() {
@Override
public void done(CCuser returnedUser) {
chkN[0] = returnedUser.uName;
}
});
chkUser.fetchUserDataByEmail(userData, new CheckUserEmail() {
@Override
public void done(CCuser returnedUser) {
chkE[0] =returnedUser.email;
}
});
if (chkN[0].equals(userData.uName))
{
AlertDialog.Builder notFound = new AlertDialog.Builder(Register.this);
notFound.setMessage("User Name already exists");
notFound.setPositiveButton("OK", null);
notFound.show();
forward=false;
}
else if (chkE[0].equals(userData.email)){
AlertDialog.Builder notFound = new AlertDialog.Builder(Register.this);
notFound.setMessage("That email is already assigned to a user");
notFound.setPositiveButton("OK", null);
notFound.show();
forward = false;
}
else if (chkN[0].equals("") && chkE[0].equals("")) {
forward = true;
}
return forward;
}
}
答案 0 :(得分:0)
为什么你不试图将数据加载到一个地图中,该地图将状态作为密钥,相应学校的列表作为该密钥的条目,然后从statesSpinner.setOnItemSelectedListener()
将正确的学校加载到新的列表中并将其发送到SchoolsSpinnerAdapter。
我希望这会对你有所帮助:)。
答案 1 :(得分:0)
好的问题就在于我没有发布的内容。围绕Web界面构建应用程序我创建了一个auth密钥。我在应用程序中命名了密钥auth,并且正在寻找appAuth进行检索以获取对我的Web服务器的访问权限。因此空指针。我只是因为这是对网络的第一次调用而在学校收到错误。一旦我将正在发送的值与正在检索的值对齐,事情就会顺利进行。
我还采用了另一种方法并创建了一个列表视图而不是一个微调器,并使用了碎片数据,因为应用程序的其他部分也将使用相同的方法。