android recyclerview仅在第一次显示数据

时间:2018-01-02 10:17:51

标签: java android xml android-layout android-recyclerview

我有一个问题,我创建了recyclerview并且它在api的第一个搜索数据中运行良好,它显示正常但是当我尝试使用新数据进行搜索时(第二次)它没有显示任何我尝试测试和调试每一件事工作正常,新数据进入适配器并获得结果正常并将适配器设置为recyclerview但它没有显示任何东西 我尝试了几种方法,例如只使用一个适配器并更改它的日期列表并使用notifyDataSetChange但是仍然只能在第一时间显示

以下活动用于搜索获取日期(用于搜索数据) 从日期到日期

DeliveryReportActivity.java

public class DeliveryReportActivity extends AppCompatActivity
    implements DateDialogFromFragment.SelectDateFromInterface,
    DateDialogToFragment.SelectDateToInterface {

Button btn_from;
Button btn_to;
EditText et_fromDate;
EditText et_toDate;

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


    btn_from=(Button)findViewById(R.id.btn_fromDate);
    btn_to=(Button)findViewById(R.id.btn_toDate);
    et_fromDate = (EditText) findViewById(R.id.from_date);
    et_toDate = (EditText) findViewById(R.id.to_date);
    search_btn=(Button)findViewById(R.id.search_delivery_report_btn);


    et_fromDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new 
Date()));
    et_toDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new 
Date()));

    btn_from.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DateDialogFromFragment dateDialogFragment = new 
DateDialogFromFragment();
            android.app.FragmentTransaction ft = 
getFragmentManager().beginTransaction();
            dateDialogFragment.show(ft, "DatePicker");
        }
    });

    btn_to.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DateDialogToFragment dateDialogFragment = new 
DateDialogToFragment();
            android.app.FragmentTransaction ft = 
getFragmentManager().beginTransaction();
            dateDialogFragment.show(ft, "DatePicker");
        }
    });



    search_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         Bundle bundle=new Bundle();
         bundle.putString("from",et_fromDate.getText().toString()+ " 
00:00:00");
         bundle.putString("to",et_toDate.getText().toString()+" 23:59:59");
            Intent intent =new 
Intent(DeliveryReportActivity.this,DeliveryReportListActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);

        }
    });
}



@Override
public void onGetSelectFromDate(String fromDate) {
    et_fromDate.setText(fromDate);
}


@Override
public void onGetSelectToDate(String toDate) {
    et_toDate.setText(toDate);
}
}

及其查看activity_delivery_report.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:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.exatech.groupsmsandroid.activity.
deliveryReport.DeliveryReportActivity">


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


        <Button
            android:id="@+id/btn_fromDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/From"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/from_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2017-12-26" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_toDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/to" />

        <EditText
            android:id="@+id/to_date"
            android:text="2017-12-26"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_centerInParent="true"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:id="@+id/search_delivery_report_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:background="@color/colorPrimary"
            android:text="@android:string/search_go" />

    </RelativeLayout>




</LinearLayout>

在我按下搜索button之后,它开始显示我的recylerview新活动的新活动

DeliveryReportListActivity .java

public class DeliveryReportListActivity extends AppCompatActivity implements 
DeliveryReportService.DeliveryReportServiceInterface {

private static final String TAG = "GSMS";

private Bundle bundle;

private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_delivery_report_list);
    recyclerView = (RecyclerView) findViewById(R.id.delivery_report_rv);
}

@Override
protected void onResume() {
    super.onResume();

    bundle = getIntent().getExtras();
    String from = bundle.getString("from");
    String to = bundle.getString("to");



DeliveryReportService.getInstance(this).
getDeliveryReportFromDateToDate(from, to);// Call api  get deliver
}

@Override
public void onGetDeliveryReport(Response<List<DeliveryReportResource>> 
listResponse) {// response
    Log.i(TAG, "onGetDeliveryReport: listResponse.body():" + 
listResponse.body());
    DeliveryReportAdapter deliveryReportAdapter = new 
DeliveryReportAdapter(DeliveryReportListActivity.this, listResponse.body());
    recyclerView.setAdapter(deliveryReportAdapter);
    deliveryReportAdapter.notifyDataSetChanged();
    Toast.makeText(DeliveryReportListActivity.this, "Delivery Report Success 
", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeliveryConnectionFailed() {
    Toast.makeText(DeliveryReportListActivity.this, "Connect Error ", 
Toast.LENGTH_SHORT).show();
}


}

及其查看activity_delivery_report_list.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="com.exatech.groupsmsandroid.activity.deliveryReport.
DeliveryReportListActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:text="@string/text"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_weight="4"
        android:text="@string/phone_no"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_weight="4"
        android:text="@string/status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>



<android.support.v7.widget.RecyclerView
    android:id="@+id/delivery_report_rv"
    android:layout_width="match_parent"
    app:layoutManager="LinearLayoutManager"
    android:layout_height="match_parent"
    tools:listitem="@layout/delivery_report_list_content"/>

</LinearLayout>

以下是Myadapter Class

 **DeliveryReportAdapter.java**



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


List<DeliveryReportResource> listDeliveryReport;
Context context;


public DeliveryReportAdapter(Context context, List<DeliveryReportResource> 
listDeliveryReport) {
    this.listDeliveryReport = listDeliveryReport;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = 

LayoutInflater.from(parent.getContext()).
inflate(R.layout.delivery_report_list_content, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.item = listDeliveryReport.get(position);
    holder.text.setText(holder.item.getText());


CustomAdapterFroDeliveryReport adapterFroDeliveryReport = new 
CustomAdapterFroDeliveryReport(context, R.layout.two_text_contect, 
listDeliveryReport.get(position).getSmsSubscribedRecipientsResourceList());

    holder.phoneNoAndStatus.setAdapter(adapterFroDeliveryReport);

    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "click message no=" + 
   holder.item.getText(), Toast.LENGTH_SHORT).show();
        }
    });


}

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


class ViewHolder extends RecyclerView.ViewHolder {
    View view;
    TextView text;
    ListView phoneNoAndStatus;
    DeliveryReportResource item;

    public ViewHolder(View itemView) {
        super(itemView);
        view = itemView;
        text = (TextView) itemView.findViewById(R.id.message_tv);
        phoneNoAndStatus = (ListView) 
    itemView.findViewById(R.id.phoneNo_and_status_lv);
    }
   }


}

1 个答案:

答案 0 :(得分:0)

尝试创建一次适配器然后更新项目
将下一个代码添加到适配器类

ArrayList<DeliveryReportResource> listDeliveryReport = new ArrayList<DeliveryReportResource>();

public DeliveryReportAdapter(Context context) {
    this.context = context;
}

public void updateItems(List<DeliveryReportResource> list) {
    listDeliveryReport.clear();
    listDeliveryReport.addAll(list);
    notifyDataSetChanged();
}

然后在onCreate()中创建一次适配器并将其作为全局变量
现在,每次要更改数据时都应调用adapter.updateItems(...)