Home > database >  how to get data from mysql database into recycler view?
how to get data from mysql database into recycler view?

Time:07-28

I am making an app that let users create meetings and I would like the details of the meeting (eg name, type, location) in a recycler view.I ran the php code by itself and was able to get the values from the database.

Currently, I am getting an error of

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

at in recyclerviewAdapter

 holder.Name.setText(users.getName());
 holder.Type.setText(users.getType());
 holder.Location.setText(users.getLocation());

which is linked to

public class Users {
    private String name,type,location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

recyclerviewAdapter

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
    Context context;
    List<Users> usersList;

    public UserAdapter(Context context, List<Users> usersList) {
        this.context = context;
        this.usersList = usersList;
    }

    @NonNull
    @Override
    public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
        return new UserHolder(userLayout);
    }

    @Override
    public void onBindViewHolder(@NonNull UserHolder holder, int position) {
        Users users=usersList.get(position);
        holder.Name.setText(users.getName());
        holder.Type.setText(users.getType());
        holder.Location.setText(users.getLocation());
    }

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

    public class UserHolder extends RecyclerView.ViewHolder{
        TextView Name,Type,Location;

        public UserHolder(@NonNull View itemView){
            super(itemView);
            Name=itemView.findViewById(R.id.textTitle);
            Type=itemView.findViewById(R.id.textType);
            Location=itemView.findViewById(R.id.textLocation);
        }
    }

}

main activity code

public class MainActivity2 extends AppCompatActivity {
    private static final String URL = "http://10.0.2.2/mad/activitydisplay.php";
    RecyclerView recyclerView;
    UserAdapter userAdapter;
    List<Users> usersList;

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

        recyclerView = findViewById(R.id.recylcerList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        usersList=new ArrayList<>();

        LoadAllUser();

    }

    private void LoadAllUser() {
        JsonArrayRequest request =new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray array) {
                for (int i=0;i<array.length();i  ){
                    try {
                        JSONObject object=array.getJSONObject(i);
                        String name=object.getString("eventname").trim();
                        String type=object.getString("type").trim();
                        String location=object.getString("location").trim();

                        Users user= new Users();
                        user.setName(name);
                        user.setType(type);
                        user.setLocation(location);
                        usersList.add(user);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
                userAdapter=new UserAdapter(MainActivity2.this,usersList);
                recyclerView.setAdapter(userAdapter);

            }
        },new Response.ErrorListener(){
            @Override
            public void one rrorResponse(VolleyError error){
                Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue=Volley.newRequestQueue(MainActivity2.this);
        requestQueue.add((request));
    }
}

php code to get data from table

$sql="SELECT * FROM `activitytable` ";
$result=mysqli_query($conn,$sql);
$user=array();

while($row = mysqli_fetch_assoc($result)){
    $index['eventname']=$row['eventname'];
    $index['type']=$row['type'];
    $index['location']=$row['location'];
    array_push($user, $index);
}
echo json_encode($user);

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/recylcerList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

activity_list resource file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@ id/textTitle"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Type:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@ id/textType"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Location:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@ id/textLocation"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

CodePudding user response:

check your xml(R.layout.activity_main2) looks like id of your view are from another layout

CodePudding user response:

instead of

public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
        return new UserHolder(userLayout);
    }

use this

public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list ,parent,false);
        return new UserHolder(userLayout);
    }

because activity_main2 doesn't contain R.id.textTitle,R.id.textType,R.id.textLocation

  • Related