将数据库中的项目显示为listView,其中包含具有不同项目的不同数据

时间:2016-03-15 05:21:20

标签: php android mysql listview

我目前正在制作一个简单的Android论坛。我可以成功地从数据库中保存和检索数据({000}来自000webhost)。我还可以将数据检索显示到phpMyAdmin

由于它是一个论坛,不同的项目(主题)应该有不同的回复。所以我有两个不同的表,一个是listView(用于存储主题),另一个是ForumData(用于存储回复)。两个表都有一个topic_id,用于指代不同的主题,例如ReplyData的回复与topic_id = 3 in ReplyData

相关

通常,只有在单击主题3时才会显示topic_id = 3 in ForumData.的回复。但每次单击不同的主题时,无论topic_id是什么,都会显示所有回复。那么,如何才能在相关主题中显示相关回复?我应该在php文件或应用程序中进行吗?

这是我检索回复数据时的php文件:

topic_id = 3

这是我对listView的适配器:

<?php

//personal data hidden
$con = mysql_connect();
mysql_select_db('db', $con);

$r = mysql_query('SELECT ReplyData.reply_user, ReplyData.reply_content,   ReplyData.reply_datetime FROM
ReplyData INNER JOIN ForumData ON ForumData.topic_id = ReplyData.topic_id');
$out = array();

while($row = mysql_fetch_array($r)){
  $out[] = $row;
}

print(json_encode($out));
mysql_close($con);

?>

这是listView填充方法:

public class MyListAdapter extends ArrayAdapter<ReplyItemList> {
    public MyListAdapter() {
        super(ContentPage.this, R.layout.layout_reply, itemList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // make sure we have a view to work with
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.layout_reply, parent, false);
        }

        // find the activity to work with
        ReplyItemList currentActivity = itemList.get(position);
        //get current time and date

        // fill the view
        TextView makeReplyUser = (TextView) itemView.findViewById(R.id.tvRespondent);
        makeReplyUser.setText("Re: " + currentActivity.getRespondent() + " ");
        TextView makeReplyContent = (TextView) itemView.findViewById(R.id.tvReplyContent);
        makeReplyContent.setText(currentActivity.getReplyContent());
        TextView makeReplyDatetime = (TextView) itemView.findViewById(R.id.tvReplyDatetime);
        makeReplyDatetime.setText("(" + currentActivity.getDateTime() + ")");

        return itemView;
    }
}

Log.i(fetchedTopicID + " " + respondent + " " + content + " " + datetime + " ", "json result");

1 个答案:

答案 0 :(得分:0)

尝试此查询。如果两个表中的字段名称不同,也提供tablename.field_name。例如; ReplyData.reply_user或ForumData.reply_content。

SELECT reply_user, reply_content, reply_datetime FROM
ReplyData INNER JOIN ForumData ON ForumData.topic_id = ReplyData.topic_id