从MYSQL获取基于用户ID的数据,并显示在Android Studio的recyclerview中

时间:2018-12-13 08:25:58

标签: php android mysql android-studio phpmyadmin

因此,我有一个数据库,其中包含2个表,一个表用于名为tblactivity的用户活动,另一个表名为tblusers

tblactivity包含一个用户的多个数据,我想根据我从tblactivity中获取的登录用户的身份,在recyclerview中的tblusers中显示条目。 tblactivityuserid栏,我想知道如何解决。

编辑:更多详细信息:

所以我正在使用苗条的框架: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);  

});

现在在android studio中

我的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();

}

1 个答案:

答案 0 :(得分:0)

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

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

然后,您要做的下一步是创建一个自定义适配器以填充RecyclerView的项目。但是同样,这在这里无法解释。这将是另一个教程的链接:https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

现在可能看起来很多,但是我也刚开始使用它,您会很快掌握它。