Он не отображает записи базы данных в RecyclerView.PHP-коды в порядке, они отлично создают JSONS - PullRequest
0 голосов
/ 27 декабря 2018

Записи базы данных не отображаются как выходные данные.Нет выхода.Android Studio не генерирует никаких ошибок.

MainActivity.java

package app.project.wedding.test3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager layoutManager;

List<PersonUtils> personUtilsList;

RequestQueue rq;

String request_url = "http://localhost/test/test3.php";

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


    rq = Volley.newRequestQueue(this);

    recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
    recyclerView.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(layoutManager);

    personUtilsList = new ArrayList<>();

    sendRequest();

}


public void sendRequest(){

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, request_url, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            for(int i = 0; i < response.length(); i++){

                PersonUtils personUtils = new PersonUtils();

                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    personUtils.setPersonFirstName(jsonObject.getString("firstname"));
                    personUtils.setPersonLastName(jsonObject.getString("lastname"));
                    personUtils.setJobProfile(jsonObject.getString("jobprofile"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                personUtilsList.add(personUtils);

            }

            mAdapter = new CustomRecyclerAdapter(MainActivity.this, personUtilsList);

            recyclerView.setAdapter(mAdapter);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, "Volley Error", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        }
    });

    rq.add(jsonArrayRequest);

}

CustomAdapter.java

package app.project.wedding.test3;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;


public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {

private Context context;
private List<PersonUtils> personUtils;

public CustomRecyclerAdapter(Context context, List personUtils) {
    this.context = context;
    this.personUtils = personUtils;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_list_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.itemView.setTag(personUtils.get(position));

    PersonUtils pu = personUtils.get(position);

    holder.pName.setText(pu.getPersonFirstName()+" "+pu.getPersonLastName());
    holder.pJobProfile.setText(pu.getJobProfile());

}

@Override
public int getItemCount() {
    return personUtils.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView pName;
    public TextView pJobProfile;

    public ViewHolder(View itemView) {
        super(itemView);

        pName = (TextView) itemView.findViewById(R.id.pNametxt);
        pJobProfile = (TextView) itemView.findViewById(R.id.pJobProfiletxt);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PersonUtils cpu = (PersonUtils) view.getTag();

                Toast.makeText(view.getContext(), cpu.getPersonFirstName()+" "+cpu.getPersonLastName()+" is "+ cpu.getJobProfile(), Toast.LENGTH_SHORT).show();

            }
        });

    }
}

PersonUtils.java

package app.project.wedding.test3;

public class PersonUtils {

private String personFirstName;
private String personLastName;
private String jobProfile;

public String getPersonFirstName() {
    return personFirstName;
}

public void setPersonFirstName(String personFirstName) {
    this.personFirstName = personFirstName;
}

public String getPersonLastName() {
    return personLastName;
}

public void setPersonLastName(String personLastName) {
    this.personLastName = personLastName;
}

public String getJobProfile() {
    return jobProfile;
}

public void setJobProfile(String jobProfile) {
    this.jobProfile = jobProfile;
}

test3.php

<?php

    $server="localhost";
    $username="root";
    $password="root";
    $db="userinfo";

    $conn = mysqli_connect($server,$username,$password,$db);
    if(!$conn)
    {
        echo "Not Connected";
    }

    $sql="select * from test3;";
    $result=mysqli_query($conn,$sql);
    $response=array();
    while($row = mysqli_fetch_array($result))
    {
        array_push($response,array("firstname"=>$row["firstname"],"lastname"=>$row["lastname"],"jobprofile"=>$row["jobprofile"]));
    }
    echo json_encode($response);

?>

1 Ответ

0 голосов
/ 27 декабря 2018

USE Request.Method.GET для получения данных.public void sendRequest () {

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, request_url, null, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {

        for(int i = 0; i < response.length(); i++){

            PersonUtils personUtils = new PersonUtils();

            try {
                JSONObject jsonObject = response.getJSONObject(i);

                personUtils.setPersonFirstName(jsonObject.getString("firstname"));
                personUtils.setPersonLastName(jsonObject.getString("lastname"));
                personUtils.setJobProfile(jsonObject.getString("jobprofile"));

            } catch (JSONException e) {
                e.printStackTrace();
            }

            personUtilsList.add(personUtils);

        }

        mAdapter = new CustomRecyclerAdapter(MainActivity.this, personUtilsList);

        recyclerView.setAdapter(mAdapter);

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(MainActivity.this, "Volley Error", Toast.LENGTH_SHORT).show();
        error.printStackTrace();
    }
});

rq.add(jsonArrayRequest);

}

...