Salem
Salem

Reputation: 11

android recyclerview show data only at first time

I have a problem ,I create recyclerview and its work fine at the first search data from api , it display fine but when I try to search with new data ( second time ) it is not display anything i try to test and debug every every thing work fine and new data enter to adapter and get the result fine and set adapter to recyclerview but it is not showing any thing I try several method like use only one adapter and change it's list of Date and use notifyDataSetChange but not work still only show at the first time

Below activity is use to search get date ( use in searching data ) fromDate to toDate

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);
}
}

and it's view 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>

after I press the search button it's start new activity that show my recylerview the new activity is

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();
}


}

and it's view 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>

Below is 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);
    }
   }


}

Upvotes: 1

Views: 1088

Answers (1)

panda_eye
panda_eye

Reputation: 166

Try to create an adapter once and then update items
Add next code to your adapter class

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();
}

Then create adapter once in onCreate() and place it as global variable
And now you should call adapter.updateItems(...) every time you want to change data

Upvotes: 1

Related Questions