I'm having trouble when I choose an item in a spinner it just won't selecting the item.
this is what it looks like when it loaded pict 1
then this is what it shown when I'm going to choose an item pict 2
but when I choose an item it's not chosen (appear like pict 1) here's my code
activity_grade_chooser.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".GradeChooserActivity">
<TextView
android:id="@+id/txtClass"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/spClass"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:text="Class" />
<Spinner
android:id="@+id/spClass"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/txtClass"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:prompt="@string/spinner_class_name"
android:entries="@array/array_semester"
android:spinnerMode="dropdown" />
<TextView
android:id="@+id/txtSemester"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/spClass"
android:layout_gravity="left"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:text="Semester" />
<Spinner
android:id="@+id/spSemester"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spClass"
android:layout_alignStart="@+id/spClass"
android:layout_below="@+id/spClass"
android:layout_marginTop="5dp"
android:prompt="@string/spinner_class_name"
android:entries="@array/array_semester"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/btnGenerate"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_below="@+id/spSemester"
android:text="Generate"
android:textSize="6pt"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textColor="@color/White"
android:background="@color/GentleBlue"
android:layout_gravity="center"/>
</RelativeLayout>
And this is the activity
GradeChooserActivity.java
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class GradeChooserActivity extends AppCompatActivity {
static ProgressDialog loading;
private static final String LogTAG = GradeChooserActivity.class.getSimpleName();
static ArrayList<StudentAndClass> ClassList = new ArrayList<>();
ArrayAdapter<String> spinnerAdapter;
Spinner spClass, spSemester;
List<String> labels = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade_chooser);
Log.e(LogTAG, "Loaded");
spClass = (Spinner) findViewById(R.id.spClass);
spSemester = (Spinner) findViewById(R.id.spSemester);
JSON_POST_onLoad(this);
populateSpinner();
}
private void populateSpinner() {
spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spClass.setAdapter(spinnerAdapter);
}
void JSON_POST_onLoad(final Context ctx){
ClassList.clear();
labels.clear();
String url = GlobalVariable.URL + "Criteria.php";
Log.e(LogTAG, "JSON_POST_onLoad Start. URL : " + url);
loading = ProgressDialog.show(ctx,"Loading Data...","Please wait...",false,false);
RequestQueue queue = Volley.newRequestQueue(ctx);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.e(LogTAG, "JSON POST onResponse : " + response);
try {
Log.e(LogTAG, "Start Try JSON - " + response.toString());
JSONObject jsObj = new JSONObject(response);
String result = jsObj.getString("result");
if (!result.equals("Valid")) {
Log.e(LogTAG, "Failed");
} else {
JSONArray jsArr = jsObj.getJSONArray("query_result");
Log.e(LogTAG, "Valid 0");
for (int i = 0; i < jsArr.length(); i++) {
JSONObject jsObj2 = jsArr.getJSONObject(i);
Log.e(LogTAG, "Valid 1");
labels.add(jsObj2.optString("class_name"));
Log.e(LogTAG, "lstUserIndex JSON value : " + jsObj2.optString("class_name"));
}
Log.e(LogTAG, "JSON Ended");
}
loading.dismiss();
} catch (JSONException e) {
e.printStackTrace();
loading.dismiss();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.e(LogTAG, "JSON POST onResponse : " + error.toString());
loading.dismiss();
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("command","Class By Student ID");
params.put("student_id",GlobalVariable.StudentID);
return params;
}
};
postRequest.setRetryPolicy(new DefaultRetryPolicy(
(int) TimeUnit.SECONDS.toMillis(20),
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(postRequest);
}
}
The second spinner (semester) underneath first spinner work finely because I set Entries beforehand. Can anyone help me with this? Or does anyone has any tips for what to use other than spinner for dropdown?
Try putting your populateSpinner() method after labels.add line as
labels.add(jsObj2.optString("class_name"));
populateSpinner();
Why your code is not working is because JSON_POST_onLoad(this)
is async and your are populating you spinner before your data arrives from server.
OR ALTERNATIVELY
keep your code as it is and just add this line after your labels.add
labels.add(jsObj2.optString("class_name"));
spinnerAdapter.notifyDataSetChanged();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.