在我的Android应用程序中,对于国家或城市,我使用了来自android的spinner小部件。在这种情况下,首先,将数据从数据库加载到微调器,它是完美的工作。我在旋转器中有国家列表,它是从数据库加载的。我的问题是,当用户点击微调器进行国家选择时,用户将收到错误。我附错误日志请参考。帮我。提前致谢。我的问题解决了但是我遇到了一个新错误。请参阅我的错误日志 完整源代码
Private class Question2Activity extends AppCompactActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
{
private static final String PATH_TO_SERVER = "http://192.168.1.103/lab/android_login_example/selectcountries.php";
private static final String SELECT_CITI = "http://192.168.1.103/lab/android_login_example/selectciti.php";
AppCompatSpinner spCity, upcountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
mContext = getApplicationContext();
// Get the activity
mActivity = Question2Activity.this;
// activity_question_detail = (RelativeLayout) findViewById(R.id.activity_question_detail);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.raw_question2, null);
mPopupWindow = new android.widget.PopupWindow(
customView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
if (Build.VERSION.SDK_INT >= 21) {
mPopupWindow.setElevation(5.0f);
}
mPopupWindow.setFocusable(true);
mPopupWindow.update();
spCountry = (AppCompatSpinner) customView.findViewById(R.id.spCountry);
spCity = (AppCompatSpinner) customView.findViewById(R.id.spCity);
btnNext = (Button) customView.findViewById(R.id.btnNext);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Question2User(stCountry, stCity);
finish();
}
});
requestJsonObject();
requestJsonObjectforCiti();
spCountry.setOnItemSelectedListener(this);
spCity.setOnItemSelectedListener(this);
findViewById(R.id.activity_question_detail).post(new Runnable() {
public void run() {
mPopupWindow.showAtLocation(findViewById(R.id.activity_question_detail), Gravity.CENTER, 0, 0);
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.spCountry) {
stCountry = spCountry.getSelectedItem().toString();
Log.d("spinner", spCountry.getSelectedItem().toString());
} else if (adapterView.getId() == R.id.spCity) {
stCity = spCity.getSelectedItem().toString();
Log.d("spinner", spCity.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
private void requestJsonObject() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCountry != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCountry.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
private void requestJsonObjectforCiti() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, SELECT_CITI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
if (null != spinnerData) {
assert spCity != null;
adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
spCity.setAdapter(spinnerAdapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
答案 0 :(得分:1)
你必须用AdapterView.OnItemSelectedListener实现你的类并重写方法这个方法下面给出的代码可能对你有帮助 int XML文件
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/ueducation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/education"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/uocuuaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:entries="@array/ustatus"
android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>
查看示例:
public class User_Detail extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
AppCompatSpinner qulification, occupation;
String uqlification="Select",uoccupation="Select";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_basic_details);
//fetch the spinners view
qulification = (AppCompatSpinner) findViewById(R.id.ueducation);
occupation = (AppCompatSpinner) findViewById(R.id.uocuuaption);
//attac the setItemSelected linstener
qulification.setOnItemSelectedListener(this);
occupation.setOnItemSelectedListener(this);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==R.id.ueducation) {
uqlification = qulification.getSelectedItem().toString();
Log.d("spinner", qulification.getSelectedItem().toString());
}
else
if(adapterView.getId()==R.id.uocuuaption) {
uoccupation=occupation.getSelectedItem().toString();
Log.d("spinner", occupation.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}