2015-10-28 19 views
0

Hello guys我想用數據庫項填充listview。但使用任何JOIN(內,左或右)合併,以表1和表查看所選的項目我的兩個表2.Android:使用JOIN查詢填充帶有php和mysql數據庫的listview

這是我的PHP代碼:

<?php 
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect"); 
mysql_select_db("dbmobile_class_record") or die("Could not select database"); 

$arr = array(); 

$rs = mysql_query("SELECT tb_student.stud_id, tb_student.stud_name, tb_attendance.remark FROM tb_student LEFT JOIN tb_attendance ON tb_student.stud_id=tb_attendance.stud_id"); 

while($obj = mysql_fetch_object($rs)) { 
$arr[] = $obj; 
} 
echo ''.json_encode($arr).''; 
?> 

這是JSON:

[ 

    { 
     "stud_id":"20131299", 
     "stud_name":"Angelo Velayo", 
     "remark":"Present" 
    }, 
    { 
     "stud_id":"20131296", 
     "stud_name":"Jeffrey Oliveras", 
     "remark":"Present" 
    } 

] 

您可以看到查詢工作併合並兩個表。

,這是我的Java文件:

public class ViewAttendance extends AppCompatActivity { 
    private static final String TAG = ViewAttendance.class.getSimpleName(); 
    private static final String url = "http://10.0.2.2/MobileClassRecord/getStudentAttendance.php"; 
    private ProgressDialog pDialog; 
    private List<StudentAttendanceList> studAttList = new ArrayList<>(); 
    private ListView mylistView; 
    private CustomStudAttendanceListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_attendance); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mylistView = (ListView) findViewById(R.id.list); 
     adapter = new CustomStudAttendanceListAdapter(this, studAttList); 
     mylistView.setAdapter(adapter); 
     mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      } 
     }); 

     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
       Log.d(TAG, response.toString()); 
       hidePDialog(); 

       for (int i = 0; i < response.length(); i++) { 
        try { 
         JSONObject jsonObject = response.getJSONObject(i); 
         StudentAttendanceList sAttList = new StudentAttendanceList(); 
         sAttList.setId(jsonObject.getString("stud_id")); 
         sAttList.setName(jsonObject.getString("student_name")); 
         sAttList.setRemark(jsonObject.getString("remark")); 

         studAttList.add(sAttList); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError volleyError) { 
       VolleyLog.d(TAG, "Error: " + volleyError.getMessage()); 
       hidePDialog(); 
      } 
     }); 
     AppController.getInstance().addToRequestQueue(request); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

,這是我所遵循的填充列表視圖

我的問題tutorial:該項目未在列表視圖顯示

回答

3

只要不要做adapter.notifyDataSetChanged();JSONArray獲得數據 你有填充你的適配器檢查這個代碼。因爲有一段時間它不是notifyDataSetChanged,所以我認爲你必須嘗試這個。謝謝。

public class ViewAttendance extends AppCompatActivity { 
private static final String TAG = ViewAttendance.class.getSimpleName(); 
private static final String url = "http://10.0.2.2/MobileClassRecord/getStudentAttendance.php"; 
private ProgressDialog pDialog; 
private List<StudentAttendanceList> studAttList = new ArrayList<>(); 
private ListView mylistView; 
private CustomStudAttendanceListAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_attendance); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    mylistView = (ListView) findViewById(R.id.list); 
    mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 
    }); 

    pDialog = new ProgressDialog(this); 
    pDialog.setMessage("Loading..."); 
    pDialog.show(); 

    JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 
      Log.d(TAG, response.toString()); 
      hidePDialog(); 

      for (int i = 0; i < response.length(); i++) { 
       try { 
        JSONObject jsonObject = response.getJSONObject(i); 
        StudentAttendanceList sAttList = new StudentAttendanceList(); 
        sAttList.setId(jsonObject.getString("stud_id")); 
        sAttList.setName(jsonObject.getString("student_name")); 
        sAttList.setRemark(jsonObject.getString("remark")); 

        studAttList.add(sAttList); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

      adapter = new CustomStudAttendanceListAdapter(this, studAttList); 
      mylistView.setAdapter(adapter); 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      VolleyLog.d(TAG, "Error: " + volleyError.getMessage()); 
      hidePDialog(); 
     } 
    }); 
    AppController.getInstance().addToRequestQueue(request); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    hidePDialog(); 
} 

private void hidePDialog() { 
    if (pDialog != null) { 
     pDialog.dismiss(); 
     pDialog = null; 
    } 
} 
+0

但有一段時間它不刷新所以我建議這樣 –

相關問題