我正在一个项目中使用一个应用程序中的多个spinners,它从MySQL数据库中提取数据并显示在spinners中。在这段代码中,只使用了一个微调器,它从MySQL数据库收集数据,我们如何在这个数据库中使用多个微调器。我们如何在Android中创建具有MySQL数据库值的多个spinners?
public class MainActivity extends ActionBarActivity {
ArrayList listItems=new ArrayList<>();
ArrayAdapter adapter;
Spinner sp,sp1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.spinner);
adapter=new ArrayAdapter(this,R.layout.spinner_layout,R.id.txt,listItems);
sp.setAdapter(adapter);
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute();
}
private class BackTask extends AsyncTask {
ArrayList list;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://192.168.1.2/agriculture.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
}catch(IOException e){
e.printStackTrace();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result+=line;
}
is.close();
//result=sb.toString();
}catch(Exception e){
e.printStackTrace();
}
try{
JSONObject jsono = new JSONObject(result);
JSONArray jArray = jsono.getJSONArray("actors");
for(int i=0;i
JSONObject jsonObject=jArray.getJSONObject(i);
list.add(jsonObject.getString("name"));
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
}
}
2017-03-01
Jincy
+0
你尝试过什么吗? –
+0
发布你的代码,你试试这个 –
+0
@ZakiPathan,我发布了我的代码..请检查 –