我试图使一个应用程序,从mysql数据库的值列表微调器应该只显示名称,然后我会得到这个名称的id移动到其他活动的问题是,微调器不显示任何东西在运行时,即使它的工作很好,当我只运行php页面请我需要帮助这是我的代码
public class ChooseScanActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String emailToPass;
String organizer_ID;
private Spinner eventSpinner;
private ArrayList <Event> eventsList;
ProgressDialog pDialog;
private String URL_Event = "http://192.168.1.3/Tactic1/list.php?organizer_ID="+organizer_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_scan);
emailToPass = getIntent().getExtras().getString("emailToPass");
eventSpinner = (Spinner)findViewById(R.id.spinnerEventID);
eventsList = new ArrayList<Event>();
eventSpinner.setOnItemSelectedListener(this);
}
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < eventsList.size(); i++) {
lables.add(eventsList.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
eventSpinner.setAdapter(spinnerAdapter);
}
private class GetEvents extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ChooseScanActivity.this);
pDialog.setMessage("get events..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_Event, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray frutas = jsonObj
.getJSONArray("frutas");
for (int i = 0; i < frutas.length(); i++) {
JSONObject catObj = (JSONObject) frutas.get(i);
Event event = new Event(catObj.getInt("id"),
catObj.getString("nombre"));
eventsList.add(event);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "¿No ha recibido ningún dato desde el servidor!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(
getApplicationContext(),
parent.getItemAtPosition(position).toString() + " Seleccionado" ,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
protected void onResume()
{
super.onResume();
getSqlDetails();
}
private void getSqlDetails(){
String url= "http://192.168.1.3/Tactic1/getOrganizerID.php?emailOrg="+emailToPass;
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
organizer_ID = jObj.getString("organizer_ID");
Toast.makeText(getApplicationContext(), organizer_ID, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error != null){
Toast.makeText(getApplicationContext(), "Something went wrong.", Toast.LENGTH_LONG).show();
}
}
}
);
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
}
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
这里是服务处理程序的代码
public class ServiceHandler {
static InputStream is = null;
static String response = null;
public final static int GET = 1;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error: " + e.toString());
}
return response;
}
}
这里是事件类的代码
public class Event {
private int id;
private String name;
public Event(){}
public Event(int id, String name){
this.setId(id);
this.setName(name);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChooseScanActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:text="@string/select_event"
android:textSize="16sp" />
<Spinner
android:id="@+id/spinnerEventID"
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="77dp" />
</RelativeLayout>
php代码
<?php
require "conn.php";
$answer = array() ;
$answer [ "event" ] = array ( ) ;
$organizer_ID =$_GET['organizer_ID'];
$sql = "select name_Event, event_ID from event where organizer_ID = '$organizer_ID'";
$result = mysqli_query ( $conn , $sql ) ;
while ( $row = mysqli_fetch_array ( $result )){
$tmp = array () ;
$tmp ["event_ID"] = $row["event_ID"] ;
$tmp ["name_Event"] = $row["name_Event"] ;
array_push ( $answer ["event"] , $tmp ) ;
}
// Keep the response header to json
header ( 'Content-Type: application / json' ) ;
// Listening to json's result
echo json_encode ( $answer ) ;
?>
1条答案
按热度按时间slwdgvem1#
正如我所看到的
populateSpinner()
方法仅在中调用AsyncTask
的onPostExecute()
方法和AsyncTask
永远不要接到电话。因此,您无法获得任何数据。应该会成功的。