【问题标题】:Retrofit with recyclerview issue使用 recyclerview 问题进行改造
【发布时间】:2018-02-17 06:23:28
【问题描述】:

我没有在回收站视图中获取数据,但没有收到任何错误。 这是主要活动类

package in.co.getonlinerecharge.cab.bw_cab.activity;    
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;    
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;    
import in.co.getonlinerecharge.cab.bw_cab.AppController;
import in.co.getonlinerecharge.cab.bw_cab.R;
import in.co.getonlinerecharge.cab.bw_cab.adepter.CarTypeAdepter;
import in.co.getonlinerecharge.cab.bw_cab.helper.ToastHelper;
import in.co.getonlinerecharge.cab.bw_cab.model.Cartype;
import in.co.getonlinerecharge.cab.bw_cab.model.CartypeArray;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;    
public class Slect_Car_Activity extends AppCompatActivity {    
    RecyclerView rvGetCartype;
    private ArrayList<Cartype> data;
    private CarTypeAdepter carTypeAdepter;
    private TextView txt_data;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slect__car_);
        rvGetCartype = (RecyclerView)findViewById(R.id.RV_list);
        txt_data = (TextView)findViewById(R.id.txt_data);
        getAndSetCarType();   
    }    
    private void getAndSetCarType() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.please_wait));
        progressDialog.show();
        Call<CartypeArray> call = AppController.getInstance().getApiInterface().getCartype();
        call.enqueue(new Callback<CartypeArray>() {
            @Override
            public void onResponse(Call<CartypeArray> call, Response<CartypeArray> response) {
                progressDialog.dismiss();
                CartypeArray cartypeArray = response.body();      

                Collections.reverse(cartypeArray.getCtypes());
                carTypeAdepter = new CarTypeAdepter(cartypeArray,getApplicationContext());

                rvGetCartype.setAdapter(carTypeAdepter);   

            }

            @Override
            public void onFailure(Call<CartypeArray> call, Throwable t) {
                progressDialog.dismiss();
            }

        });

    }
}    

这是我的改造模型类,它为 Json 数组提供状态

 package in.co.getonlinerecharge.cab.bw_cab.model;        
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;       

    public class Cartype {

        @SerializedName("id")
        @Expose
        private String id;

        @SerializedName("vehicle_type")
        @Expose
        private String Vehicale_type;

        @SerializedName("vehicle_name")
        @Expose
        private String Vehicale_name;

        @SerializedName("vehicle_no")
        @Expose
        private String Vehicale_no;

        @SerializedName("vimage")
        @Expose
        private String Vimage;

        @SerializedName("status")
        @Expose
        private String status;

        @SerializedName("created_at")
        @Expose
        private String created_at;

        @SerializedName("updated_at")
        @Expose
        private String updated_at;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getVehicale_type() {
            return Vehicale_type;
        }

        public void setVehicale_type(String vehicale_type) {
            Vehicale_type = vehicale_type;
        }

        public String getVehicale_name() {
            return Vehicale_name;
        }

        public void setVehicale_name(String vehicale_name) {
            Vehicale_name = vehicale_name;
        }

        public String getVehicale_no() {
            return Vehicale_no;
        }

        public void setVehicale_no(String vehicale_no) {
            Vehicale_no = vehicale_no;
        }

        public String getVimage() {
            return Vimage;
        }

        public void setVimage(String vimage) {
            Vimage = vimage;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getCreated_at() {
            return created_at;
        }

        public void setCreated_at(String created_at) {
            this.created_at = created_at;
        }

        public String getUpdated_at() {
            return updated_at;
        }

        public void setUpdated_at(String updated_at) {
            this.updated_at = updated_at;
        }
    }   

这是我的适配器类,带有用于回收器视图的视图持有者

   package in.co.getonlinerecharge.cab.bw_cab.adepter;        
    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.ImageView;
    import android.widget.TextView;        
    import com.squareup.picasso.Picasso;        
    import java.util.ArrayList;
    import java.util.List;

    import in.co.getonlinerecharge.cab.bw_cab.R;
    import in.co.getonlinerecharge.cab.bw_cab.model.Cartype;
    import in.co.getonlinerecharge.cab.bw_cab.model.CartypeArray;
    import in.co.getonlinerecharge.cab.bw_cab.utils.Constans;       



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

        private CartypeArray cartypeArray;
        private Context mcontext;



        public CarTypeAdepter(CartypeArray cartypeArray, Context mcontext) {
            this.cartypeArray = cartypeArray;
            this.mcontext = mcontext;
        }        

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = (LayoutInflater.from(parent.getContext()).inflate(R.layout.list_car,parent, false));
            CartypeHolder cartypeHolder = new CartypeHolder(view);
            return cartypeHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder Holder, int position) {
            CartypeHolder cartypeHolder = (CartypeHolder) Holder;

            cartypeHolder.txt_cartype.setText(cartypeArray.getCtypes().get(position).getVehicale_name());

        }

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

        private class CartypeHolder extends RecyclerView.ViewHolder {
            ImageView img_car;
            TextView txt_cartype;
            public CartypeHolder(View itemview) {
                super(itemview);
                img_car = (ImageView)itemview.findViewById(R.id.CarImageView);
                txt_cartype = (TextView)itemview.findViewById(R.id.titleTextView);
            }
        }
    }       

我的第二个模型类用于获取 JSON 数据的改造

   package in.co.getonlinerecharge.cab.bw_cab.model;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;        
    import java.util.List;       


    public class CartypeArray {      

        @SerializedName("status")
        @Expose
        private Boolean status;
        @SerializedName("Cartype")
        @Expose
        private List<Cartype> ctypes;

        public Boolean getStatus() {
            return status;
        }

        public void setStatus(Boolean status) {
            this.status = status;
        }



        public List<Cartype> getCtypes() {
            return ctypes;
        }

        public void setCtypes(List<Cartype> ctypes) {
            this.ctypes = ctypes;
        }
    }

这是 Activity 主 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"        tools:context="in.co.getonlinerecharge.cab.bw_cab.activity.Slect_Car_Activity">
    <android.support.design.widget.AppBarLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="Select Your Car" />

    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/RV_list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txt_data"/>
</LinearLayout>   

列表视图的显示数据列表

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        card_view:cardCornerRadius="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/card_height"
            android:orientation="vertical"
           >    
                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/CarImageView"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:layout_gravity="center"
                    android:scaleType="centerCrop"
                    android:src="@drawable/suv"
                    />    
                    <TextView
                        android:layout_gravity="center"
                        android:id="@+id/titleTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="16dp"
                        android:text="SUV"
                        android:textSize="@dimen/card_text_size"
                        android:textColor="@color/colorPrimary"/>

        </LinearLayout>
    </android.support.v7.widget.CardView>

【问题讨论】:

    标签: android-recyclerview retrofit


    【解决方案1】:

    在 onCreate 中将布局管理器设置为您的回收站:

    rvGetCartype = (RecyclerView)findViewById(R.id.RV_list);
    rvGetCartype.setLayoutManager(new LinearLayoutManager(this));
    

    如果没有帮助,请尝试下一步: 1.确保您的响应中的 ArrayList 不是空的。

    2.如果不是,请尝试下一步:

    public class CarTypeAdepter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        ArrayList<Cartype> cartypeArray;
        private Context mcontext;
        public CarTypeAdapter(Context context){
            this.mcontext = context;
            cartypeArray = new ArrayList<>();
        }
        public void setList(ArrayList<Cartype> list){
            this. cartypeArray = list;
            notifyDataSetChanges();
        }
    //other methods
    }
    

    然后在活动中创建此适配器的变量并在 onCreate 中对其进行初始化,并将其设置为您的回收站视图。然后在 onResponse 中使用 setList(cartypeArray) 方法将数组设置为适配器。

    希望您理解我及其帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2020-02-27
      • 2015-05-26
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多