2017-03-01 62 views
-3

我正在一個項目中使用一個應用程序中的多個spinners,它從MySQL數據庫中提取數據並顯示在spinners中。在這段代碼中,只使用了一個微調器,它從MySQL數據庫收集數據,我們如何在這個數據庫中使用多個微調器。我們如何在Android中創建具有MySQL數據庫值的多個spinners?

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(); 
    } 
} 
+0

你嘗試過什麼嗎? – Wanderer

+0

發佈你的代碼,你試試這個 –

+0

@ZakiPathan,我發佈了我的代碼..請檢查 – Jincy

回答

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被填充後設置適配器。如果這不起作用,請隨時提問。

+0

好吧,讓我試試這個.. – Jincy

+0

好吧@Jincy慢慢來,隨時問 –

+0

謝謝zaki ..它的工作...... :) – Jincy

0

紗廠是很容易實現 -
1)獲取從數據庫SQLite的一個ArrayList您的數據。
2)創建一個微調器 - 微調器abcd;
3)用你定義的ArrayList創建一個ArrayAdapter。
4)使適配器下拉 - myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
5)將微調器的適配器設置爲ArrayAdapter- abcd.setAdapter(myAdapter)。

相關問題