【问题标题】:Fetch data based on user id from MYSQL and display in recyclerview in android studio从 MYSQL 获取基于用户 ID 的数据并显示在 android studio 的 recyclerview 中
【发布时间】:2018-12-13 08:25:57
【问题描述】:

所以我有一个包含 2 个表的数据库,一个用于用户活动,名为 tblactivity,另一个名为 tblusers

tblactivity 包含一个用户的多个数据,我想根据我从tblusers 获取数据的登录用户在回收站视图中显示tblactivity 中的条目。 tblactivityuserid 专栏,我想知道如何去做。

编辑:更多详情:

所以我正在使用 slim 框架:DbConnect.php

//Method to get emails of a particular user

    public function getActivity(){
        $stmt = $this->con->prepare("SELECT id, userid, subject, message FROM tblactivity");
        $stmt->execute(); 
        $stmt->bind_result($id, $userid, $subject, $message);
        $users = array(); 
        while($stmt->fetch()){ 
            $user = array(); 
            $user['id'] = $id; 
            $user['userid'] = $userid; 
            $user['subject'] = $subject; 
            $user['message'] = $message; 


            array_push($users, $user);
        }             
        return $users; 

    }

我的 index.php 代码

$app->get('/allactivity', function(Request $request, Response $response){
$db = new DbOperations; 
$users = $db->getActivity();
$response_data = array();
$response_data['error'] = false; 
$response_data['users'] = $users; 
$response->write(json_encode($response_data));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(200);  
});

现在在安卓工作室:

我的 Activity.java 模型:

public class Activity {

private int id;
private int userid;
private String subject;
private String message;
private String to;


public Activity(String subject, String message) {

    this.id = id;
    this.userid = userid;
    this.subject = subject;
    this.message = message;
    this.to = to;

}

public int getId() {
    return id;
}
public int getUserid() { return userid; }
public String getSubject() { return subject; }
public String getMessage() {
    return message;
}
public String getTo() { return to; }

}

User.java 模型

public class User {
private int id;
private String email;
private String firstname;
private String lastname;
private String companyname;
private String postcode;
private String city;
private String state;
private String phonenumber;
private String address1;
private String country;
private String status;
private int currency;
private String credit;
private String language;
private int email_verified;


public User(int id, String email, String firstname, String lastname, String companyname, String address1, String city,
            String state, String postcode, String country, String phonenumber, String status, int currency, String credit,
            String language, int email_verified) {

    this.id = id;
    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;
    this.companyname = companyname;
    this.address1 = address1;
    this.city = city;
    this.state = state;
    this.postcode = postcode;
    this.country = country;
    this.phonenumber = phonenumber;
    this.status = status;
    this.currency = currency;
    this.credit = credit;
    this.language = language;
    this.email_verified = email_verified;


}

public int getId() {
    return id;
}
public String getEmail() {
    return email;
}
public String getFirstName() {
    return firstname;
}
public String getLastName() {
    return lastname;
}
public String getCompanyName() { return companyname; }
public String getAddress1() {
    return address1;
}
public String getCity() {
    return city;
}
public String getState() { return state; }
public String getPostcode() {
    return postcode;
}
public String getCountry() {
    return country;
}
public String getPhonenumber() {
    return phonenumber;
}
public String getStatus() { return status; }
public int getCurrency() { return currency; }
public String getCredit() {
    return credit;
}
public String getLanguage() {
    return language;
}
public int getEmail_verified() { return email_verified; }


}

我正在使用 Retrofit 客户端从 php 获取 json 输出。

    @GET("allactivity")
Call<List<Activity>> getActivityData();

}

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 请我在回复此线程的答案中提供我的详细信息和我的代码。请看一下,让我知道您是否可以提供帮助。非常感谢

标签: java php android mysql


【解决方案1】:

如果我理解正确,您的问题是关于数据处理的。现在有两种可能性,如果您在设备上有一个本地数据库,那么您将必须创建一个 SQLLiteOpenHelper 并传入一个 SQL Query,在其中您可以阐明您需要的数据示例可能是:

SELECT * FROM tblactivity WHERE userid = 30;

在这种情况下,您将获得有关 id 为 30 的用户的所有信息。现在,如果您有一个在线数据库,则必须编写一个 php 文件,如果我添加此文件,您可以与 sql 数据库进行通信在这里也一样,它会走得太远。这是您可以查看的教程:https://www.w3schools.com/php/php_mysql_connect.asp

通过 ftp 处理程序上传 php 文件后,您需要使用如下链接打开 Internet 连接:www.mydomain.com/database.php?userid=30。

您传入您感兴趣的用户的限制。在您的 php 文件中,您可以通过以下方式获取数据:

$sql = "SELECT * FROM tblactivity WHERE userid = ".$_GET["userid"];

然后你需要读取传入的数据并解析它。我的选择是在您的 php 文件中使用 json_encode 并将传入的数据作为 JSON 存储在应用程序中。您也需要了解这一点,但这并不难。

如果您收到了数据,您需要创建一个自定义对象,在其中存储所有必要的信息:

public class User {
    public User(String name, int _id, String email){
        //save the information in local variables
    }
    public String getName(){
         return mName;
    }
    //Add more methods to get and set the different variables.
}

接下来你要做的就是创建一个自定义的 Adapter 来填充 RecyclerView 的项目。但同样,在这里解释太多了。这是另一个教程的链接:https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

现在可能看起来很多,但我也刚刚开始,你会很快掌握它的窍门。

【讨论】:

  • 非常感谢。我正在使用 slim 框架在 php 中编码并生成 json。在我对这篇文章的回答中查看我的详细信息。
  • 另外,我想使用来自 sharedpref 的登录用户 ID 在 recyclerView 中显示获取的数据。你能帮忙吗?
  • 您到底需要什么?是否需要根据保存的用户ID进行查询?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
相关资源
最近更新 更多