2015-10-31 112 views

回答

7

你必須首先創建一個PHP腳本,將打印你的MySQL數據庫的數據以JSON格式..在這裏,我向您展示一個例子讓我們看看這是我的數據庫 MySQL Database

現在假設我需要加載這個表的所有用戶名到android的微調。因此,這是PHP腳本,這將使我JSON格式的數據

<?php 

$sql = "SELECT * FROM students"; 

require_once('dbConnect.php'); 

$r = mysqli_query($con,$sql); 

$result = array(); 

while($row = mysqli_fetch_array($r)){ 
    array_push($result,array(
     'username'=>$row['username'], 
     'name'=>$row['name'], 
     'course'=>$row['course'], 
     'session'=>$row['session'] 
    )); 
} 

echo json_encode(array('result'=>$result)); 

mysqli_close($con); 

上面的PHP代碼將提供以下JSON

{"result":[{"username":"faiz","name":"Faiz Khan","course":"BCA","session":"2014-2017"},{"username":"belal","name":"Belal Khan","course":"MCA","session":"2015-2018"},{"username":"ramiz","name":"Ramiz Khan","course":"MBA","session":"2015-2017"},{"username":"zafar","name":"Zafar Khan","course":"MBA","session":"2014-2016"}]} 

現在Android的代碼將MainActivity.java

public class MainActivity extends AppCompatActivity{ 
private Spinner spinner; 
private ArrayList<String> students; 
private JSONArray result; 
private TextView textViewName; 
private TextView textViewCourse; 
private TextView textViewSession; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    students = new ArrayList<String>(); 
    spinner = (Spinner) findViewById(R.id.spinner); 
    spinner.setOnItemSelectedListener(this); 
    textViewName = (TextView) findViewById(R.id.textViewName); 
    textViewCourse = (TextView) findViewById(R.id.textViewCourse); 
    textViewSession = (TextView) findViewById(R.id.textViewSession); 
    getData(); 
} 

private void getData(){ 
    StringRequest stringRequest = new StringRequest("your php script address", 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        JSONObject j = null; 
        try { 
         j = new JSONObject(response); 
         result = j.getJSONArray(Config.JSON_ARRAY); 
         getStudents(result); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

private void getStudents(JSONArray j){ 
    for(int i=0;i<j.length();i++){ 
     try { 
      JSONObject json = j.getJSONObject(i); 
      students.add(json.getString(Config.TAG_USERNAME)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students)); 
} 
} 

來源Android Spinner Example with MySQL Database

0
  1. Read the Spinner class documentation
  2. 查詢數據庫
  3. 獲取結果
  4. 編碼數據轉換成JSON格式
  5. 請求,並在您的應用程序解析結果
  6. 填充一個列表陣列微調器
  7. 將適配器設置在微調器上
相關問題