我的应用程序中有两个活动A和B.我使用json从listview中的api加载数据,并使用getter setter方法将该数据存储在listview中 在活动A. 现在我想在启动B活动之后在listview中维护数据,当我从B回到A活动时,我不会再次从json下载数据。
我现在正在通过字符串中的sotirng api response来做这件事,然后从B回到A我将json字符串传递给适配器但是仍然需要时间来加载listview
当我从B回到A时,如何防止加载时间和获取数据。
这是我的活动A的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
pref =getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor = pref.edit();
itemCount_database = new ItemCount_database(this);
item_adding_cart = new Item_adding_cart(this);
String fromhome =pref.getString("from_product_view_back_Btn","");
if(fromhome.equals("yes"))
{
editor.putString("from_product_view_back_Btn","no");
editor.commit();
overridePendingTransition(R.anim.right_in, R.anim.right_out);
stored_json();
recycler_adapter = new Product_list_Recycler_Adapter(this,datalist);
recyclerView.setAdapter(recycler_adapter);
recycler_adapter.setListener(ProductList_Activity.this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
else
{
overridePendingTransition(R.anim.left_in, R.anim.left_out);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
jsoncall();
}
initialised_actionbar();
checkout = (TextView)findViewById(R.id.txt_checkout_product_list);
txt_total_amount =(TextView)findViewById(R.id.total_amount_in_checkout_label);
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myint = new Intent(ProductList_Activity.this,Cart.class);
startActivity(myint);
}
});
footer =(LinearLayout) findViewById(R.id.footer);
footer.setVisibility(View.GONE);
cart_item_count =item_adding_cart.total_product_count(1);
if(cart_item_count==0 )
{
footer.setVisibility(View.GONE);
cart_count_background.setVisibility(View.GONE);
}
else
{
footer.setVisibility(View.VISIBLE);
cart_count_background.setVisibility(View.VISIBLE);
}
wishlist_item_count = item_adding_cart.getrowcount_wishlist();
if(wishlist_item_count==0)
{
// wishlist_count_backround.setVisibility(View.GONE); } 其他 { // wishlist_count_backround.setVisibility(View.VISIBLE); }
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
initialization();
hidePDialog();
}
}, 3000);
boolean popup_once=pref.getBoolean("total_item_cart_insert_qery",true);
if(popup_once)
{
editor.putBoolean("total_item_cart_insert_qery",false);
editor.commit();
item_adding_cart.insert_total_item_in_cart(0);
}
}
public void jsoncall() {
final HashMap<String, String> params = new HashMap<String, String>();
params.put("sub_cate_id","24");
params.put("city_id","3");
CustomRequest_JsonObject servicelistrequest = new CustomRequest_JsonObject
(/*Request.Method.POST,*/
url,
params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
stored_json_string = response.toString();
editor.putString("stored_json_prodcutlist",stored_json_string);
editor.commit();
try {
JSONArray data = response.getJSONArray("data");
for (int ie = 0; ie < data.length(); ie++) {
JSONObject dataobje = data.getJSONObject(ie);
ModelData_Product_list_act Model = new ModelData_Product_list_act();
Model.setProduct_id(dataobje.getString("product_id"));
Model.setTitle(dataobje.getString("title"));
Model.setImage(dataobje.getString("product_image"));
Model.setWieght(dataobje.getString("weight"));
Model.setLatest_price(dataobje.getString("mrp"));
Model.setDiscount(dataobje.getString("discount"));
Model.setPre_price(dataobje.getString("retail_price"));
Model.setUnit(dataobje.getString("unit"));
datalist.add(Model);
loadlistview();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
Toast.makeText(getApplicationContext(), "error : " + error.getMessage(), Toast.LENGTH_LONG).show();
}
}
);
AppController.getInstance().addToRequestQueue(servicelistrequest);
}
public void stored_json() {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
String mystored_json = pref.getString("stored_json_prodcutlist","");
try {
JSONObject myjsonojb = new JSONObject(mystored_json);
JSONArray data = myjsonojb.getJSONArray("data");
for (int ie = 0; ie < data.length(); ie++) {
JSONObject dataobje = data.getJSONObject(ie);
ModelData_Product_list_act Model = new ModelData_Product_list_act();
Model.setProduct_id(dataobje.getString("product_id"));
Model.setTitle(dataobje.getString("title"));
Model.setImage(dataobje.getString("product_image"));
Model.setWieght(dataobje.getString("weight"));
Model.setLatest_price(dataobje.getString("mrp"));
Model.setDiscount(dataobje.getString("discount"));
Model.setPre_price(dataobje.getString("retail_price"));
Model.setUnit(dataobje.getString("unit"));
datalist.add(Model);
// loadlistview();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
使用startActivityForResult(yourIntent,REQ_CODE)而不是使用startActivity
Intent startActivity = new Intent(context,ClassName.class);
startActivityForResult(startActivity,REQ_CODE);
从活动B回来后,可以从活动中发送您的列表,然后可以进入活动A.
然后onActivityResult回调方法获取你的意图数据
答案 1 :(得分:0)
我还有另一个想法是使用后台任务创建并将其存储到本地数据库并在恢复时显示来自本地数据库的数据。你总是从本地数据库调用列表视图所以当你回来时b到一个简历会调用因此它不会调用create,因此它不会调用后台数据