如何使用RxJava2从数据库获取最新数据并在顶部显示

时间:2019-09-23 07:59:39

标签: android mvvm retrofit2 rx-java2

我正在使用MVVM模式创建应用程序,并使用Retrofit2和rxjava2从服务器获取数据并将其显示在回收器视图中。到目前为止,数据已在回收器视图中成功获取,但最近添加的数据显示在回收器视图的最后位置我想在回收者视图的顶部显示最近添加的数据:

这是我的代码,用于在回收站视图中显示数据:

Contacts.java

public class Contacts extends Fragment {

private RecyclerView recyclerView;
private List<Contact> contactList = new ArrayList<>();
private ContactsAdapter adapter;
private ProgressBar progressBar1;
private ContactViewModel viewModel;


public Contacts() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_contacts, container, false);

    recyclerView=  view.findViewById(R.id.recyclerView);
    progressBar1 = view.findViewById(R.id.progressBar1);

    viewModel = ViewModelProviders.of(this).get(ContactViewModel.class);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    adapter = new ContactsAdapter(getActivity(),contactList);

    recyclerView.setAdapter(adapter);

    viewModel.getAllContacts().observe(this, new Observer<List<Contact>>() {
        @Override
        public void onChanged(List<Contact> contacts) {

            progressBar1.setVisibility(View.INVISIBLE);
            adapter.setContactList(contacts);
        }
    });

    return view;
  }

}

ContactViewModel.java

public class ContactViewModel extends AndroidViewModel {

ContactRepository repo;
MutableLiveData<List<Contact>> mutableLiveData;

public ContactViewModel(@NonNull Application application) {
    super(application);

    repo  = new ContactRepository(application);
    mutableLiveData = new MutableLiveData<>();
  }

public MutableLiveData<List<Contact>> getAllContacts() {
    return repo.getMutableLiveData();
  }
}

ContactRepository.java

public class ContactRepository {

Context context;
MutableLiveData<List<Contact>> contactmutableLiveData = new MutableLiveData<>();

public ContactRepository(Context context){

    this.context = context;
}

public MutableLiveData<List<Contact>> getMutableLiveData() {

    Retrofit retrofit = RetrofitClient.getInstance();
    ApiService apiService = retrofit.create(ApiService.class);

    apiService.getContacts().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Contact>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(List<Contact> contacts) {

                    contactmutableLiveData.setValue(contacts);

                }

                @Override
                public void onError(Throwable e) {

                    TastyToast.makeText(context,e.getMessage(),TastyToast.LENGTH_LONG,TastyToast.ERROR).show();
                }

                @Override
                public void onComplete() {

                }
            });

    return contactmutableLiveData;
  }

}

ContactsAdapter.java

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

Context context;
List<Contact> contactList = new ArrayList<>();

public ContactsAdapter(Context context, List<Contact> contactList) {
    this.context = context;
    this.contactList = contactList;
}

@NonNull
@Override
public ContactsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_row,parent,false);

    ViewHolder viewHolder = new ViewHolder(v);

    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {

    final Contact contact = contactList.get(position);

    final String str1 = contact.getFirstname();
    final String str2 = contact.getLastname();

    holder.userName.setText(str1 + " " +str2);

}

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

public void setContactList(List<Contact> contactList) {
    this.contactList = contactList;
    notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView userName;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        userName = itemView.findViewById(R.id.userName);
      }
   }

}

Contact.java

public class Contact {

String firstname,lastname;

public Contact(){

}

public Contact(String firstname,String lastname){

    this.firstname = firstname;
    this.lastname = lastname;

}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}
}

请让我知道如何获得所需的输出。任何帮助将不胜感激。

谢谢

0 个答案:

没有答案