我正处于制作应用程序的中间位置,通过排球请求获取一些数据,并在ListFragment上的ListView中设置数据。
我的问题是,当我使用我的后退按钮转换回上一个活动并退出应用程序时(onStop),当我从appdrawer重新打开应用程序并转到ListFragment时,它显示的列表是复制。
参考下面的图片:
代码:
ListFragment类:
package lassebjoerklund.easyfridge;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* Created by lassebjorklund on 17/02/16.
*/
public class ProductsList extends ListFragment {
ProductListListnerInteface productListListnerInteface;
public interface ProductListListnerInteface {
public void onProductSelected(int pos);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(getActivity().getClass().getSimpleName(), "onCreate");
super.onCreate(savedInstanceState);
if(savedInstanceState == null) {
setListAdapter(new ProductAdapter(getActivity(), BootActivity.getProductsToDisplayInlist()));
}else{
}
}
@Override
public void onAttach(Context context) {
Log.d(getActivity().getClass().getSimpleName(), "onAttach");
super.onAttach(context);
try {
productListListnerInteface = (ProductListListnerInteface) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement ProductListListnerInteface");
}
}
@Override
public void onStart() {
Log.d(getActivity().getClass().getSimpleName(), "onStart");
super.onStart();
if (getFragmentManager().findFragmentById(R.id.fragment_container_list) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
productListListnerInteface.onProductSelected(position);
}
}
Activity将ListFragment与DetailFragment绑定(不是问题的一部分)
package lassebjoerklund.easyfridge;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
/**
* Created by lassebjorklund on 17/02/16.
*/
public class ProductViewController extends FragmentActivity implements ProductsList.ProductListListnerInteface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products_list_layout);
if (findViewById(R.id.fragment_container_list) != null) {
if (savedInstanceState != null) {
return;
}
ProductsList productsList = new ProductsList();
productsList.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.fragment_container_list, productsList).commit();
}
}
@Override
public void onProductSelected(int pos) {
ProductDetail productDetail = (ProductDetail) getFragmentManager().findFragmentById(R.id.details_fragment_container);
if (productDetail != null) {
productDetail.updateProductOnPos(pos);
} else {
ProductDetail newProductDetail = new ProductDetail();
Bundle args = new Bundle();
args.putInt("Position", pos);
newProductDetail.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container_list, newProductDetail);
fragmentTransaction.addToBackStack("ProductList");
fragmentTransaction.commit();
}
}
}
启动器活动
package lassebjoerklund.easyfridge;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
public class BootActivity extends Activity {
private static ArrayList<Products> productsToDisplayInlist = new ArrayList<>();
private String TAG = BootActivity.class.getSimpleName();
private String url = "XXXXXXXXXXXXXXXXXXXX";
private String jsonRespons = "";
private RequestQueue rQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Layout
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boot_layout);
//Custom Adapter for list view, for ref.
//final ProductAdapter adapter = new ProductAdapter(this, productsToDisplayInlist);
//RequestQueue
rQueue = Volley.newRequestQueue(this);
//Methode to populate Arralist
//Intent to switch to activity
final Intent dispalyProductsIntent = new Intent(BootActivity.this, ProductViewController.class);
getRequest();
final ImageButton updateViewButton = (ImageButton) findViewById(R.id.bViewProducts);
updateViewButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(dispalyProductsIntent);
}
});
}
public void getRequest() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
for (int i = 0; i < response.length(); i++) {
JSONObject productToBuild = (JSONObject) response.get(i);
String name = productToBuild.getString("name");
String type = productToBuild.getString("type");
int id = productToBuild.optInt("id");
String removalDate = productToBuild.getString("removal_date");
String placementDate = productToBuild.getString("placement_date");
Products product = new Products(name, placementDate, removalDate, type);
productsToDisplayInlist.add(product);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Volly", "Error");
error.printStackTrace();
}
});
rQueue.add(jsonArrayRequest);
}
public void postRequest() {
}
public static ArrayList<Products> getProductsToDisplayInlist() {
return productsToDisplayInlist;
}
@Override
protected void onResume() {
Log.d("onResume", "onResume");
/*Toast toast = Toast.makeText(this, "onResume", Toast.LENGTH_LONG);
toast.show();*/
super.onResume();
}
@Override
protected void onRestart() {
Log.d("onRestart", "onRestart");
/*Toast toast = Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT);
toast.show();*/
super.onRestart();
}
@Override
protected void onStop() {
Log.d("onStop", "onStop");
// productsToDisplayInlist.clear();
super.onStop();
}
@Override
protected void onPause() {
Log.d("onPause", "onPause");
super.onPause();
}
}
我似乎无法找到问题..任何人都可以帮忙吗?
答案 0 :(得分:2)
productsToDisplayInlist
是静态的,每次返回BootActivity
时都不会重新创建,直到重新创建应用程序本身。
当您收到回复时,您只会添加到收藏中,因此每次收到回复时您都会添加重复项。
有许多方法可以解决这个问题,最简单的方法就是将productsToDisplayInlist.clear()
添加到onResponse()
的开头,因为无论如何都要完全重新填充列表。
答案 1 :(得分:0)
我认为问题在于,在BootActivity中,您不会在向HTTP响应中添加产品之前清除productsToDisplayInList(productsToDisplay.clear();
)。 productsToDisplayInList是静态的,这意味着它在整个应用程序的生命周期中保持其状态。我对此并不是百分之百确定,但是当你将底部活动留在堆栈中时(退出应用程序),它仍然可以包含数据,因为应用程序仍然在后台运行。