在RecyclerView中从mysql获取自定义数据

时间:2019-03-31 13:22:51

标签: java php android json

我启动了一个项目,其中MainActivity使用String(reg_no)Intent putExtra活动发送ViewAttendence
我希望在其中仅显示具有reg_no = reg_no(which I get from intent);

的数据

为此,活动首先向服务器发送POST请求,向服务器发送reg_no

$reg_no = $_POST['reg_no'];

然后显示reg_no=$reg_no
怎么可能?

这是我的ViewAttendence代码:

public class ViewAttendence extends AppCompatActivity {

private  String URL_PROFILE = "http://govtclgjind.supremephysioeducation.com/jm/view_attendence.php";

private JsonArrayRequest request;
private RequestQueue requestQueue;
private List<AttendenceViewModel> listAnime;
private RecyclerView recyclerView;
private ProgressDialog mProgress;
private String reg_no;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_attendence);

    getSupportActionBar().setTitle("View Attendence");
    listAnime = new ArrayList<>();
    mProgress = new ProgressDialog(this);

    recyclerView =(RecyclerView) findViewById(R.id.students_list);

    reg_no = getIntent().getStringExtra("reg_no");


}

private void jsonRequest() {
    mProgress.setMessage("Please Wait...");
    mProgress.show();
    request = new JsonArrayRequest(URL_PROFILE, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            JSONObject jsonObject = null;
            for (int i=0; i < response.length();i++){
                try {
                    jsonObject = response.getJSONObject(i);
                    AttendenceViewModel anime = new AttendenceViewModel();
                    anime.setDate(jsonObject.getString("date"));
                    anime.setAttendance(jsonObject.getString("attendence"));


                    listAnime.add(anime);
                    mProgress.dismiss();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            setuprecyclerview(listAnime);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue = Volley.newRequestQueue(ViewAttendence.this);
    requestQueue.add(request);
}

private void setuprecyclerview(List<AttendenceViewModel> listAnime) {
    AttendenceViewAdapter myAdapter = new AttendenceViewAdapter(this,listAnime);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(myAdapter);
}


}

这是我的php代码:

<? 
$servername = "localhost";
$username = "";
$password = "";
$database = "";



    $conn = new mysqli($servername, $username, $password, $database);


   if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
   }

   $heroes = array(); 


   $sql = "SELECT date,attendence,reg_no FROM jm_attendence ORDER BY date ASC;";
    mysqli_set_charset($conn,'utf8');
   $stmt = $conn->prepare($sql);

   $stmt->execute();

   $stmt->bind_result($date,$attendence,$reg_no);

   while($stmt->fetch()){

 $temp = [
 'date'=>$date,
'attendence'=>$attendence,
'reg_no'=>$reg_no

 ];

 array_push($heroes, $temp);
}

echo json_encode($heroes);

0 个答案:

没有答案