E / RecyclerView:没有连接适配器;获取Json数组时跳过布局

时间:2017-12-24 23:56:16

标签: android android-recyclerview android-arrayadapter retrofit2

我有这个奇怪的问题。当我尝试从数据库中获取我的屏幕上的数据时。它没有显示任何内容,而是在 UsersHomeActivity 类中使用 onFailure 方法。当我尝试使用Postman时,我的Web服务工作正常,我将所有数据作为 Json Array 获取。同样在LogCat的调试屏幕中,我可以看到我的响应和请求,它们都是正确的。但是,当我点击 bDisplayActivity 时,没有任何显示,我看到E / RecyclerView:没有连接适配器;跳过布局。

我的PHP WebService:

<?php
        require_once ('viewConnection.php');

         $status="status";
         $message = "message";

        $sql = "SELECT Activity_Name, Activity_StartDate, Activity_EndDate, Activity_Capacity, Activity_City,
        Activity_Deadline_Abstract, Activity_Deadline_Notice, Activity_Deadline_CameraReady, Activity_Deadline_FullVersion FROM activity";
        $res = mysqli_query($conn,$sql);
        $result = array();

         while($row = mysqli_fetch_array($res)){

         array_push($result, 
         array('Activity_Name'=>$row[0], 'Activity_StartDate'=>$row[1],
         'Activity_EndDate'=>$row[2], 'Activity_Capacity'=>$row[3], 'Activity_City'=>$row[4], 
         'Activity_Deadline_Abstract'=>$row[5], 'Activity_Deadline_Notice'=>$row[6],
         'Activity_Deadline_CameraReady'=>$row[7], 'Activity_Deadline_FullVersion'=>$row[8],));
  }
        echo json_encode(array("Activity"=>$result,$status=>1,$message=>"Success"));
        mysqli_close($conn);
 ?>

这些是我的代码:

我的行列表项目布局:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal"
    android:padding="5dp">
    <TextView
        android:id="@+id/tvActivity_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Material.Body1" />

     <TextView
        android:id="@+id/tvActivity_StartDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tvActivity_EndDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvActivity_Capacity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvActivity_City"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvActivity_Deadline_Abstract"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvActivity_Deadline_Notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvActivity_Deadline_CameraReady"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tvActivity_Deadline_FullVersion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我的活动布局:

    <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="com.furkankaracan.activityplanner.UserHomeActivity">

    <Button
        android:id="@+id/button_display_data_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Aktiviteleri Yükle" />

    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="20dp"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
</LinearLayout>

我的适配器和我的活动类:

    public class ActivityAdapter extends 

RecyclerView.Adapter<ActivityAdapter.ViewHolder> {

    private List<ActivityModel> dataList;
    private Context context;

    public ActivityAdapter(List<ActivityModel> dataList, Context context){
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
      View view = LayoutInflater.from(context).inflate(R.layout.list_view, parent, false);
      return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        ActivityModel model = dataList.get(position);

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        holder.tvActivity_Name.setText(model.getActivityName());
        holder.tvActivity_StartDate.setText(df.format(model.getActivityStartDate()));
        holder.tvActivity_EndDate.setText(df.format(model.getActivityEndDate()));
        holder.tvActivity_Capacity.setText(model.getActivityCapacity());
        holder.tvActivity_City.setText(model.getActivityCity());
        holder.tvActivity_Deadline_Abstract.setText(df.format(model.getActivityDeadlineAbstract()));
        holder.tvActivity_Deadline_Notice.setText(df.format(model.getActivityDeadlineNotice()));
        holder.tvActivity_Deadline_CameraReady.setText(df.format(model.getActivityDeadlineCameraReady()));
        holder.tvActivity_Deadline_FullVersion.setText(df.format(model.getActivityDeadlineFullVersion()));
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{

       public TextView tvActivity_Name, tvActivity_StartDate, tvActivity_EndDate, tvActivity_Capacity, tvActivity_City,
                tvActivity_Deadline_Abstract, tvActivity_Deadline_Notice, tvActivity_Deadline_CameraReady,
                tvActivity_Deadline_FullVersion;

        ViewHolder(View itemView){
            super(itemView);
            tvActivity_Name = (TextView) itemView.findViewById(R.id.tvActivity_Name);
            tvActivity_StartDate = (TextView) itemView.findViewById(R.id.tvActivity_StartDate);
            tvActivity_EndDate = (TextView) itemView.findViewById(R.id.tvActivity_EndDate);
            tvActivity_Capacity = (TextView) itemView.findViewById(R.id.tvActivity_Capacity);
            tvActivity_City = (TextView) itemView.findViewById(R.id.tvActivity_City);
            tvActivity_Deadline_Abstract = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_Abstract);
            tvActivity_Deadline_Notice = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_Notice);
            tvActivity_Deadline_CameraReady = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_CameraReady);
            tvActivity_Deadline_FullVersion = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_FullVersion);
        }
    }
}


public class UserHomeActivity extends AppCompatActivity {

    private ActivityAdapter adapter;
    private RecyclerView recyclerView;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_home);

        Button bDisplayActivity = (Button) findViewById(R.id.button_display_data_activity);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        progressDialog = new ProgressDialog(this);

        bDisplayActivity.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                getAllActivity();
            }
        });
    }

    private void getAllActivity(){
        progressDialog.setTitle("Displaying Data");
        progressDialog.setMessage("Loading...");


        GetActivityDataAPI service = RetrofitService.getClient().create(GetActivityDataAPI.class);
    Call<ActivityListModel> listModelCall = service.getAllDataActivity();

        listModelCall.enqueue(new Callback<ActivityListModel>() {

            @Override
            public void onResponse(Call<ActivityListModel> call, Response<ActivityListModel> response) {
                ActivityListModel activityListModel = response.body();
                if(activityListModel.getStatus() == 1){
                    List<ActivityModel> activityModels = activityListModel.getActivityList();
                    ActivityAdapter activityAdapter = new ActivityAdapter(activityModels, UserHomeActivity.this);
                    recyclerView.setLayoutManager(new LinearLayoutManager(UserHomeActivity.this));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(activityAdapter);
                    progressDialog.dismiss();
                }else {
                    Toast.makeText(UserHomeActivity.this, activityListModel.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<ActivityListModel> call, Throwable t) {
                Toast.makeText(UserHomeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();

            }
        });
    }
}

和我的模型类:

ublic class ActivityModel {

    @SerializedName("Activity_Name")
    private String activityName;
    @SerializedName("Activity_StartDate")
    private Date activityStartDate;
    @SerializedName("Activity_EndDate")
    private Date activityEndDate;
    @SerializedName("Activity_Capacity")
    private Integer activityCapacity;
    @SerializedName("Activity_City")
    private String activityCity;
    @SerializedName("Activity_Deadline_Abstract")
    private Date activityDeadlineAbstract;
    @SerializedName("Activity_Deadline_Notice")
    private Date activityDeadlineNotice;
    @SerializedName("Activity_Deadline_CameraReady")
    private Date activityDeadlineCameraReady;
    @SerializedName("Activity_Deadline_FullVersion")
    private Date activityDeadlineFullVersion;

    public ActivityModel(String activityName,Date activityStartDate, Date activityEndDate, Integer activityCapacity, String activityCity,
                         Date activityDeadlineAbstract, Date activityDeadlineNotice, Date activityDeadlineCameraReady, Date activityDeadlineFullVersion)
    {
        this.activityName = activityName;
        this.activityStartDate = activityStartDate;
        this.activityEndDate = activityEndDate;
        this.activityCapacity = activityCapacity;
        this.activityCity = activityCity;
        this.activityDeadlineAbstract = activityDeadlineAbstract;
        this.activityDeadlineNotice = activityDeadlineNotice;
        this.activityDeadlineCameraReady = activityDeadlineCameraReady;
        this.activityDeadlineFullVersion = activityDeadlineFullVersion;
    }

    public String getActivityName() {
        return activityName;
    }

    public void setActivityName(String activityName) {
        this.activityName = activityName;
    }

    public Date getActivityStartDate() {
        return activityStartDate;
    }

    public void setActivityStartDate(Date activityStartDate) {
        this.activityStartDate = activityStartDate;
    }

    public Date getActivityEndDate() {
        return activityEndDate;
    }

    public void setActivityEndDate(Date activityEndDate) {
        this.activityEndDate = activityEndDate;
    }

    public int getActivityCapacity() {
        return activityCapacity;
    }

    public void setActivityCapacity(int activityCapacity) {
        this.activityCapacity = activityCapacity;
    }

    public String getActivityCity() {
        return activityCity;
    }

    public void setActivityCity(String activityCity) {
        this.activityCity = activityCity;
    }

    public Date getActivityDeadlineAbstract() {
        return activityDeadlineAbstract;
    }

    public void setActivityDeadlineAbstract(Date activityDeadlineAbstract) {
        this.activityDeadlineAbstract = activityDeadlineAbstract;
    }

    public Date getActivityDeadlineNotice() {
        return activityDeadlineNotice;
    }

    public void setActivityDeadlineNotice(Date activityDeadlineNotice) {
        this.activityDeadlineNotice = activityDeadlineNotice;
    }

    public Date getActivityDeadlineCameraReady() {
        return activityDeadlineCameraReady;
    }

    public void setActivityDeadlineCameraReady(Date activityDeadlineCameraReady) {
        this.activityDeadlineCameraReady = activityDeadlineCameraReady;
    }

    public Date getActivityDeadlineFullVersion() {
        return activityDeadlineFullVersion;
    }

    public void setActivityDeadlineFullVersion(Date activityDeadlineFullVersion) {
        this.activityDeadlineFullVersion = activityDeadlineFullVersion;
    }

}


public class ActivityListModel {
    @SerializedName("Activity")
    private ArrayList<ActivityModel> ActivityList ;
    @SerializedName("status")
    private Integer status;
    @SerializedName("message")
    private String message;

    public ActivityListModel() {
    }

    public ArrayList<ActivityModel> getActivityList() {
        return ActivityList;
    }

    public void setActivityList(ArrayList<ActivityModel> ActivityList) {
        this.ActivityList = ActivityList;
    }

    public Integer getStatus() {
        return status;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

我的LoggingInterceptors类:

public class LoggingInterceptors implements Interceptor{

    @Override
    public Response intercept(Chain chain) throws IOException{
        Request request = chain.request();
        long t1 = System.nanoTime();
        String requestLog = String.format("Sending request %s on %s%n%s",
                request.url(), chain.connection(), request.headers());

        if (request.method().compareToIgnoreCase("post") == 0){
            requestLog = "\n" + requestLog + "\n" + bodyToString(request);
        }

        Log.d("TAG", "request" + "\n" + requestLog);
        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        String responseLog = String.format("Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers());

        String bodyString = response.body().string();
        Log.d("TAG", "response" + "\n" + responseLog + "\n" + bodyString);

        return response.newBuilder()
                .body(ResponseBody.create(response.body().contentType(), bodyString))
                .build();
    }

    public static String bodyToString(final Request request){
        try{
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);

            return buffer.readUtf8();
        }catch (final IOException e){
            return "hatalı bodyToString";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试在更新数据源的适配器中调用notifyDataSetChanged()。

答案 1 :(得分:0)

我认为有一个swapCursor方法是个好主意。此方法将设置光标,您可以通知如果数据已更改,如下所示:

public void swapCursor(Cursor cursor) {
    if (mCursor != null) {
        mCursor.close();
    }
    mCursor = cursor;
    notifyDataSetChanged();
}

然后,当你在片段/活动中加载数据时,你可以这样称呼它:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
    mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader)
{
    mAdapter.swapCursor(null);
}

希望这会有所帮助:)