I have made a recyclerview and populating it with data from server with volley, but this error keep coming back, I have tried all the solutions available but it won't work. Some help will be really appreciated.

I am using MySQL database my api for getting data from database is working fine, but recyclerview is giving this error "android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText"

Data Class:

public class bus_data {


    private int id;
    private String bus_number;
    private String bus_total_seats;
    private String bus_available_seats;
    private String bus_route;
    private String bus_leaving_time;
    private String bus_reaching_time;
    private String bus_driver_name;
    private String bus_ticketchecker_name;
    private String bus_rating;
    private String bus_break_time;
    private String bus_company;

    public bus_data(int id,String bus_number,String bus_total_seats,String bus_available_seats,
                    String bus_route,String bus_leaving_time,String bus_reaching_time,
                    String bus_driver_name,String bus_ticketchecker_name,String bus_rating,String bus_break_time,String bus_company) {
        this.id = id;
        this.bus_number = bus_number;
        this.bus_total_seats = bus_total_seats;
        this.bus_available_seats = bus_available_seats;
        this.bus_route = bus_route;
        this.bus_leaving_time = bus_leaving_time;
        this.bus_reaching_time = bus_reaching_time;
        this.bus_driver_name = bus_driver_name;
        this.bus_ticketchecker_name = bus_ticketchecker_name;
        this.bus_rating = bus_rating;
        this.bus_break_time = bus_break_time;
        this.bus_company = bus_company;
    }

    public int getId() {
        return id;
    }

    public String getbus_number() {
        return bus_number;
    }

    public String getbus_total_seats() {
        return bus_total_seats;
    }

    public String getbus_available_seats() {
        return bus_available_seats;
    }
    public String getbus_route() {
        return bus_route;
    }
    public String getbus_leaving_time() {
        return bus_leaving_time;
    }
    public String getbus_reaching_time() {
        return bus_reaching_time;
    }
    public String getbus_driver_name() {
        return bus_driver_name;
    }
    public String getbus_ticketchecker_name() {
        return bus_ticketchecker_name;
    }
    public String getbus_rating() {
        return bus_rating;
    }
    public String getbus_break_time() {
        return bus_break_time;
    }
    public String getbus_company() {
        return bus_company;
    }
}

Adapter Class:

public class adapter_class extends RecyclerView.Adapter<adapter_class.bus_dataViewHolder> {


    private Context mCtx;
    private List<bus_data> productList;

    public adapter_class(Context mCtx, List<bus_data> productList) {
        this.mCtx = mCtx;
        this.productList = productList;
    }

    @Override
    public bus_dataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.bus_detail_list, null);
        return new bus_dataViewHolder(view);
    }


    @Override
    public void onBindViewHolder(bus_dataViewHolder holder, int position) {
        bus_data busDetail = productList.get(position);

        //loading the image

        holder.bus_number.setText(busDetail.getbus_number());
        holder.total_seats.setText(busDetail.getbus_total_seats());
        holder.available_seats.setText(String.valueOf(busDetail.getbus_available_seats()));
        holder.bus_route.setText(String.valueOf(busDetail.getbus_route()));
        holder.bus_leaving_time.setText(String.valueOf(busDetail.getbus_leaving_time()));
        holder.bus_reaching_time.setText(String.valueOf(busDetail.getbus_reaching_time()));
        holder.bus_driver_name.setText(String.valueOf(busDetail.getbus_driver_name()));
        holder.bus_ticketchecker_name.setText(String.valueOf(busDetail.getbus_ticketchecker_name()));
        holder.bus_rating.setText(String.valueOf(busDetail.getbus_rating()));
        holder.bus_break_time.setText(String.valueOf(busDetail.getbus_break_time()));
        holder.bus_company.setText(String.valueOf(busDetail.getbus_company()));
    }

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

    class bus_dataViewHolder extends RecyclerView.ViewHolder {

        EditText bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
                bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;


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

            bus_number = itemView.findViewById(R.id.bus_number);
            total_seats = itemView.findViewById(R.id.total_seats);
            available_seats = itemView.findViewById(R.id.available_seats);
            bus_route = itemView.findViewById(R.id.route);
            bus_leaving_time = itemView.findViewById(R.id.leaving_time);
            bus_reaching_time = itemView.findViewById(R.id.reaching_time);
            bus_driver_name = itemView.findViewById(R.id.driver_name);
            bus_ticketchecker_name = itemView.findViewById(R.id.tk_checker_name);
            bus_rating = itemView.findViewById(R.id.rating);
            bus_break_time = itemView.findViewById(R.id.break_time);
            bus_company = itemView.findViewById(R.id.bus_company);
        }
    }
}

Main Class:

public class Bus_Details extends AppCompatActivity {

    private static final String URL_PRODUCTS = "http://192.168.10.17/AutoBus/api.php";

    //a list to store all the products
    List<bus_data> productList;

    //the recyclerview
    RecyclerView recyclerView;

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

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);


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

        //initializing the productlist
        productList = new ArrayList<>();

        //this method will fetch and parse json
        //to display it in recyclerview
        loadProducts();
    }

    private void loadProducts() {

        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject data = array.getJSONObject(i);

                                //adding the product to product list
                                productList.add(new bus_data(
                                        data.getInt("id"),
                                        data.getString("bus_number"),
                                        data.getString("bus_total_seats"),
                                        data.getString("bus_available_seats"),
                                        data.getString("bus_route"),
                                        data.getString("bus_leaving_time"),
                                        data.getString("bus_reaching_time"),
                                        data.getString("bus_driver_name"),
                                        data.getString("bus_ticketchecker_name"),
                                        data.getString("bus_rating"),
                                        data.getString("bus_break_time"),
                                        data.getString("bus_company")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            adapter_class adapter = new adapter_class(Bus_Details.this, productList);
                            recyclerView.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(Bus_Details.this, "Error"+e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Bus_Details.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();


                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(this).add(stringRequest);
    }
    }

Here are the layout files. recycler_activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Passenger.Bus_Details">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recylcerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="745dp"
        tools:layout_editor_absoluteY="-51dp" />

</RelativeLayout>

List_activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:id="@+id/layout">


        <TextView
            android:id="@+id/bus_company"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Daewo"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/bus_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/bus_company"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="GJN-1234"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

        <TextView
            android:id="@+id/total_seats"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/bus_number"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:background="@color/colorPrimary"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="70"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/available_seats"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/total_seats"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/leaving_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/available_seats"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/reaching_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/leaving_time"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/driver_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/reaching_time"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tk_checker_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/driver_name"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/break_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tk_checker_name"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/break_time"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/route"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/rating"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="50"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold" />


    </RelativeLayout>

</LinearLayout>

This is the error I am getting

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.autobus, PID: 10266
    java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

Screenshot of error: enter image description here

2 Answers

0
Mehul Kabaria On

This issue is because you take TextView in your adapter xml and get it like as a EditText.

So change in your adapter onCreateViewHolder from EditText to TextView .

Change below line in your adapter class.

TextView bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
                bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;
0
Hitesh Tarbundiya On

In your bus_dataViewHolder class replace EditText to TextView. because in layout you can use TextView and inside the program you use Edittext, that's while this issue generated.