我们如何在Android中使用MySQL数据库值创建多个微调器?

时间:2017-03-01 11:51:04

标签: android mysql

我正在使用一个项目,在一个应用程序中有多个微调器,它从MySQL数据库中获取数据并显示在微调器中。在这段代码中,只使用了一个微调器,它从MySQL数据库收集数据,我们如何在这个中使用多个微调器。

public class MainActivity extends ActionBarActivity {
ArrayList<String> listItems=new ArrayList<>();
  ArrayAdapter<String> 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<String>(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<Void,Void,Void> {
    ArrayList<String> 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<jArray.length();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();
    }
}

2 个答案:

答案 0 :(得分:1)

public class MainActivity extends ActionBarActivity {
ArrayList<String> listItems=new ArrayList<>();
  ArrayAdapter<String> adapter;
Spinner sp,sp1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp=(Spinner)findViewById(R.id.spinner);

     }
 public void onStart(){
    super.onStart();
    BackTask bt=new BackTask();
    bt.execute();
}
  private class BackTask extends AsyncTask<Void,Void,Void> {
    ArrayList<String> 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<jArray.length();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=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
    sp.setAdapter(adapter);
    }
}

试试这个。填写listitem后设置适配器。如果这不起作用,请随时询问。

答案 1 :(得分:0)

旋转器非常容易实现 -
1)从SQLite DB中获取ArrayList中的数据 2)创建一个微调器 - Spinner abcd;
3)使用您定义的ArrayList创建ArrayAdapter 4)使适配器成为下拉 - myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
5)将微调器的适配器设置为ArrayAdapter- abcd.setAdapter(myAdapter)。