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

标签 php android mysql listview

大家好,我想用数据库项目填充 ListView 。但是使用任何 JOIN(内部、左侧或右侧)合并我的两个表,以便查看表 1 和表 2 中的选定项目。

这是我的 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我在填充 ListView 时遵循的内容

我的问题:项目未显示在 ListView 中

最佳答案

JSONArray 获取数据后不要执行 adapter.notifyDataSetChanged(); 您已填写适配器并检查此代码。因为有时它没有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;
    }
}

关于php - Android:使用 JOIN 查询使用 php 和 mysql 数据库填充 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383466/

相关文章:

java - 使用 gradle 复制构建 Artifact

android - 在 Android 源版本之间切换的更好方法

android - 对 EditText 施加约束

php - 如何在 PHP 中创建允许我手动输入数据的 GridView ?

MySQL 错误代码 1242

php - CakePHP 2.6.7 hasMany 数据未保存

php - 在标记视频系统上寻找有关 "related videos"查询的建议

php - SQL 查询中的 sprintf( '%d' , $tainted ) 是漏洞吗?

java - 如何在准备好的语句java中使用特殊字符

php - 从多个mysql表中提取数据