I have been spending time learning RecyclerView and to display data into it. But I am faced with a problem and I wanted to consult with you guys on how to search in RecyclerView.
What I am intending to do is there will be a EditText and Button. When the app loads, there will be no data on the RecyclerView. But only when I type in the EditText on what to search for and then click the button. Then only will the app get the info from the database and show it in the RecyclerView.
Here is my php file
<?php
$page = $_GET['page'];
$superHero= $_GET['superHero'];
$start = 0;
$limit = 3;
require_once('dbConnect.php');
$total = mysqli_num_rows(mysqli_query($con, "SELECT id from evaluated"));
$page_limit = $total/$limit;
if($page<=$page_limit){
$start = ($page - 1) * $limit;
$sql = "SELECT * from evaluated WHERE superHero='".$superHero."' limit $start, $limit";
$result = mysqli_query($con,$sql);
$res = array();
while($row = mysqli_fetch_array($result)){
array_push($res, array(
"username"=>$row['username'],
"rate"=>$row['rate'],
));
}
echo json_encode($res);
}else{
echo "over";
}
And this is my MainActivity
private JsonArrayRequest getDataFromServer(int requestCounts) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCounts),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
return jsonArrayRequest;
}
private void getData() {
requestQueue.add(getDataFromServer(requestCount));
requestCount++;
}
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
SuperHero superHero = new SuperHero();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setName(json.getString(Config.TAG_NAME));
superHero.setPublisher(json.getString(Config.TAG_PUBLISHER));
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
adapter.notifyDataSetChanged();
}
and my Adapter Class
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
SuperHero superHero = Data.get(position);
final String textx = superHero.getPublisher();
float ratingss = Float.parseFloat(textx);
holder.textViewName.setText(superHero.getName());
holder.textViewPublisher.setText(superHero.getPublisher());
holder.rate.setRating(ratingss);
}
@Override
public int getItemCount() {
return Data.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName;
public TextView textViewPublisher;
public RatingBar rate;
public ViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
rate = (RatingBar) itemView.findViewById(R.id.ratingBar);
}
}
Thanks in advance for any help! :D
What I have tried:
I have added
$superHero= $_GET['superHero'];
and
$sql = "SELECT * from evaluated WHERE superHero='".$superHero."' ORDER BY id DESC limit $start, $limit";
but stuck in implementing it in the code.