duanca3415 2015-10-28 05:23
浏览 66

Android:使用带有JOIN查询的php和mysql数据库填充listview

Hello guys i want to populate a listview with the database items. But using any JOIN(inner, left or right) to merge my two tables in order to view the selected items in table 1 and table 2.

Here is my PHP Code:

<?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).'';
?>

This is the JSON:

[

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

]

as you can see the query works and it merge the two tables.

and this is my Java file:

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;
        }
    }

and this is the tutorial that i have followed in populating the list view

My Problem: The items is not showing in the listview

  • 写回答

1条回答 默认 最新

  • dongsutao8921 2015-10-28 05:37
    关注

    Just don't do adapter.notifyDataSetChanged(); after get data from JSONArray you have fill there your adapter check this code. because some time its not notifyDataSetChanged so I think you have to try this. Thanks.

    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;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀