Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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
Java
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) {
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(MainActivity.this, "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    //Returning the request
    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

Java
@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
PHP
$superHero= $_GET['superHero'];

and
PHP
$sql = "SELECT * from evaluated WHERE superHero='".$superHero."' ORDER BY id DESC limit $start, $limit";


but stuck in implementing it in the code.
Posted
Updated 5-Apr-16 1:09am

1 solution

Where is your setAdaper method ? You need to call setAdapater method and where you would associate your recyclerview with adapter and datasource, and resource. You are missing that. Rest all looks good (though I have not executed your code).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900