2017-04-14 48 views
0

我有兩個活動A和B在我的應用程序。我從我的列表視圖中使用json加載數據從api和存儲該數據在列表視圖中使用getter setter方法 在活動A. 現在我想維護數據在listview後開始B活動,當我從B回到A活動我不不會再從json下載數據。listview數據維護後開始新的活動

我對現在的字符串,並從B改回A之後sotirng API性反應做我通過JSON字符串適配器,但它仍需要時間來加載列表視圖

任何想法如何防止加載時間和獲取數據立即從我乙回A.當

這是我的activty甲

@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)的代碼; } else { // 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

而不是使用startActivity使用startActivityForResult(yourIntent,REQ_CODE)

Intent startActivity = new Intent(context,ClassName.class); 
    startActivityForResult(startActivity,REQ_CODE); 

後您來自活動B回來,這樣可以從活動發送您的列表,並且可以在活動A.

得到

然後onActivityResult回調方法獲取您的意圖數據

0

我有另一個想法使用後臺任務是在CRE吃和存儲到本地數據庫和本地數據庫顯示在恢復中的數據。你總是從本地數據庫調用列表視圖,所以當你回來時一個恢復會調用,所以它不會調用創建,所以它不會調用背景資料