我正在将ADD活动中的项目添加到我的回收者列表中,该活动通过单击recyclerview活动中的按钮打开。但是当我添加并关闭ADD活动时,除非我关闭并再次打开它,否则该项目不会显示在recyclerview活动中。
如果添加网络呼叫成功,我将获得一个ID。然后,我将ID添加到sqlite,完成ADD活动,然后返回我的recyclerview活动。我从sqlite获取ID,并将其传递给网络调用函数以获取我的商品。
这是我的Recyclerview活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_subscriptions);
db = SubscriptionDB.getInstance(this);
sessionDBHelper = new SessionDBHelper(this);
idDB = IDsDB.getInstance(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatAddSub);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Subscriptions.this, AddSubscription.class);
startActivity(intent);
}
});
customerID = idDB.getCustomerID();
progressBar = findViewById(R.id.progress_bar);
recyclerview = (RecyclerView) findViewById(R.id.subscription_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
fetchInfo(customerID);
if (subArrayList.size() > 0) {
adapter = new SubRVAdapter(subArrayList, this, this.getSupportFragmentManager());
subArrayList = db.getAll();
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
}
public void fetchInfo(String customerID) {
apiInterface = RetrofitInstance.getRetrofitInstance().create(APIInterface.class);
Call<ArrayList<SubObject>> call = apiInterface.getSubscription(customerID);
progressBar.setVisibility(View.VISIBLE);
call.enqueue(new Callback<ArrayList<SubObject>>() {
@Override
public void onResponse(Call<ArrayList<SubObject>> call, Response<ArrayList<SubObject>> response) {
adapter = new SubRVAdapter(subArrayList, getApplicationContext(), getSupportFragmentManager());
subArrayList = response.body();
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
int statusCode = response.code();
Log.d("onresponse", "" + statusCode);
}
@Override
public void onFailure(Call<ArrayList<SubObject>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Log.d("onfailure", " " + t.getMessage());
Toast.makeText(Subscriptions.this, "There is a network issue. Try again later.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d("onResume", "RESUME");
fetchInfo(customerID);
if (subArrayList.size() > 0) {
adapter = new SubRVAdapter(subArrayList, this, this.getSupportFragmentManager());
subArrayList = db.getAll();
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
添加活动
APIInterface apiInterface = RetrofitInstance.getRetrofitInstance().create(APIInterface.class);
AddSubscriptionModel addSubscriptionModel = new AddSubscriptionModel();
addSubscriptionModel.setStickerNumber(stickerNumber);
addSubscriptionModel.setPin(Pin);
addSubscriptionModel.setUsername(username);
addSubscriptionModel.setPassword(password);
addSubscriptionModel.setEmail(phone);
Call<ArrayList<SubModel>> activate = apiInterface.addSubscription(addSubscriptionModel);
activate.enqueue(new Callback<ArrayList<SubModel>>() {
@Override
public void onResponse(Call<ArrayList<SubModel>> call, Response<ArrayList<SubModel>> response) {
if (response.isSuccessful()) {
int statusCode = response.code();
subsscriptions = response.body();
String cusID = subsscriptions.get(0).getCustomer_id();
String subID = subsscriptions.get(0).getSubscription_id();
idDB.addCustomerID(cusID, subID);
}
Toast.makeText(AddSubscription.this, "SUCCESS", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
Log.d("onresponse", "" + statusCode);
}
}
@Override
public void onFailure(Call<ArrayList<SubModel>> call, Throwable t) {
Toast.makeText(AddSubscription.this, "Network issues", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
Log.d("onfailure"," " + t . getMessage());
}
});
finish();
}
});
答案 0 :(得分:3)
您可以用这样的结果开始活动
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Subscriptions.this, AddSubscription.class);
startActivityForResult(intent,1);
}
});
完成添加活动后,您将获得结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
customerID = idDB.getCustomerID();
fetchInfo(customerID);
if (subArrayList.size() > 0) {
adapter = new SubRVAdapter(subArrayList, this, this.getSupportFragmentManager());
subArrayList = db.getAll();
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
}
在完成ADD活动之后,您还必须等到获得API响应
Call<ArrayList<SubModel>> activate = apiInterface.addSubscription(addSubscriptionModel);
activate.enqueue(new Callback<ArrayList<SubModel>>() {
@Override
public void onResponse(Call<ArrayList<SubModel>> call, Response<ArrayList<SubModel>> response) {
if (response.isSuccessful()) {
int statusCode = response.code();
subsscriptions = response.body();
String cusID = subsscriptions.get(0).getCustomer_id();
String subID = subsscriptions.get(0).getSubscription_id();
idDB.addCustomerID(cusID, subID);
}
Toast.makeText(AddSubscription.this, "SUCCESS", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
Log.d("onresponse", "" + statusCode);
finish(); <-- add this
}
@Override
public void onFailure(Call<ArrayList<SubModel>> call, Throwable t) {
Toast.makeText(AddSubscription.this, "Network issues", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
Log.d("onfailure"," " + t . getMessage());
finish(); <-- add this
}
});
//finish(); <-- remove this
答案 1 :(得分:0)
这样做我刚刚编辑了您的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_subscriptions);
db = SubscriptionDB.getInstance(this);
sessionDBHelper = new SessionDBHelper(this);
idDB = IDsDB.getInstance(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatAddSub);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Subscriptions.this, AddSubscription.class);
startActivity(intent);
}
});
customerID = idDB.getCustomerID();
progressBar = findViewById(R.id.progress_bar);
recyclerview = (RecyclerView) findViewById(R.id.subscription_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
fetchInfo(customerID);
}
private void loadDataInRecyclerView(){
//if you have already data loaded from API then why are you again loading data from db.getAll();
//if this is not necessary try commenting it
subArrayList = db.getAll();
adapter = new SubRVAdapter(subArrayList, this, this.getSupportFragmentManager());
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void fetchInfo(String customerID) {
apiInterface = RetrofitInstance.getRetrofitInstance().create(APIInterface.class);
Call<ArrayList<SubObject>> call = apiInterface.getSubscription(customerID);
progressBar.setVisibility(View.VISIBLE);
call.enqueue(new Callback<ArrayList<SubObject>>() {
@Override
public void onResponse(Call<ArrayList<SubObject>> call, Response<ArrayList<SubObject>> response) {
adapter = new SubRVAdapter(subArrayList, getApplicationContext(), getSupportFragmentManager());
subArrayList = response.body();
progressBar.setVisibility(View.GONE);
int statusCode = response.code();
if(subArrayList.size()==0){
Log.i("No data" , "Inside Response : " + "");
}else{
loadDataInRecyclerView();
}
Log.d("onresponse", "" + statusCode);
}
@Override
public void onFailure(Call<ArrayList<SubObject>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Log.d("onfailure", " " + t.getMessage());
Toast.makeText(Subscriptions.this, "There is a network issue. Try again later.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d("onResume", "RESUME");
fetchInfo(customerID);
if (subArrayList.size() > 0) {
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}}
让我知道这是否可行,仅供参考,我已经在onCreate()
方法中进行了一些编辑,而您的fetchInfo()
方法也请注意我在fetchInfo()
方法中的评论