android recyclerview仅在第一时间显示数据

问题描述 投票:0回答:1

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

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

delivery report activity.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);
    }
   }


}
java android xml android-layout android-recyclerview
1个回答
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(...)

© www.soinside.com 2019 - 2024. All rights reserved.