我创建了3个arraylist,它有另一种类型的数组:
ArrayList<String[]> arrayListString = new ArrayList<String[]>();
ArrayList<int[]> arrayListInt = new ArrayList<int[]>();
ArrayList<double[]> arrayListDouble = new ArrayList<double[]>();
我从api调用中获取信息,我通过调用以下函数来获取信息。我需要做的就是附加arraylist,以便可以使用新项目和之前的项目重新绘制以实现无限滚动。但是现在,当我进行第二次api调用时,我得到了新项目,我甚至可以将它们存储在新的String数组中,但我无法将它们附加到arraylist。我不知道为什么会这样。 arraylist总是包含第一个api调用字符串数组,永远不会更新。你能告诉我我哪里错了吗?
public void productListingApiCall(int i) {
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(base_url).setLogLevel(RestAdapter.LogLevel.FULL).build();
final ProductListingApi productListingApi =
restAdapter.create(ProductListingApi.class);
productListingApi.getFeed(file, operation_condition, search_string_condition, minprice_condition, maxprice_condition, mincusratings_condition,
maxcusratings_condition, discount_condition, catids_condition, brands_condition, affids_condition, start_row_condition, "5",
orderby_condition, sortby_condition, new Callback<ProductListingPojo>() {
@Override
public void success(ProductListingPojo productListingPojo, Response response) {
final ProductListingPojo product = productListingPojo;
new Thread(new Runnable() {
@Override
public void run() {
String[] t = Arrays.copyOf(product.getTitle(),
product.getTitle().length);
int[] p = Arrays.copyOf(product.getSellingprice(),
product.getSellingprice().length);
int[] m = Arrays.copyOf(product.getMrp(),
product.getMrp().length);
int[] d = Arrays.copyOf(product.getDiscountpercent(),
product.getDiscountpercent().length);
String[] i = Arrays.copyOf(product.getProductimageSmall1(),
product.getProductimageSmall1().length);
String[] title = new String[5];
String[] image = new String[5];
int[] price = new int[5];
int[] mrp = new int[5];
int[] discount = new int[5];
title = t;
price = p;
mrp = m;
discount = d;
image = i;
arrayListString.add(title);
arrayListString.add(image);
arrayListInt.add(price);
arrayListInt.add(mrp);
arrayListInt.add(discount);
Log.e("t", Arrays.toString(t));
Log.e("pp", Arrays.toString(p));
Log.e("m", Arrays.toString(m));
Log.e("d", Arrays.toString(d));
Log.e("i", Arrays.toString(i));
Log.e("ttt", Arrays.toString(title));
Log.e("pppp", Arrays.toString(price));
Log.e("mmm", Arrays.toString(mrp));
Log.e("ddd", Arrays.toString(discount));
Log.e("iii", Arrays.toString(image));
Log.e("tttttt", Arrays.toString(arrayListString.get(0)));
Log.e("ppppppp", Arrays.toString(arrayListInt.get(0)));
Log.e("mmmmmm", Arrays.toString(arrayListInt.get(1)));
Log.e("dddddd", Arrays.toString(arrayListInt.get(2)));
Log.e("iiiiii", Arrays.toString(arrayListString.get(1)));
}
}).run();
setAdapter();
}
@Override
public void failure(RetrofitError error) {
tv_title_header.setText(error.getMessage());
Log.e("error", error.getMessage());
}
});
}
答案 0 :(得分:0)
使用此代码
Log.e("tttttt", Arrays.toString(arrayListString.get(0)));
Log.e("ppppppp", Arrays.toString(arrayListInt.get(0)));
Log.e("mmmmmm", Arrays.toString(arrayListInt.get(1)));
Log.e("dddddd", Arrays.toString(arrayListInt.get(2)));
Log.e("iiiiii", Arrays.toString(arrayListString.get(1)));
您始终在查看添加到arrayLists的第一个值。我怀疑第二次,如果你愿意的话
Log.e("tttttt", Arrays.toString(arrayListString.get(2)));
您将获得属于第二次api通话的标题。因此,您必须在某处保留一个计数器,告诉您它是否是第1次,第2次,第3次......调用API。
但是,我建议您以不同的方式存储从API接收的数据。使用List
是好的,但你可以列出你喜欢的每个类,包括你自己的。因此,如果您创建这样的DataApi
类:
public class ApiData {
public String[] title, image;
int[] price, mrp, discount;
}
并用
替换当前的数组列表private List<ApiData> arrayListData = new ArrayList<ApiData>();
然后,您可以将数据存储在其中:
@Override
public void run() {
ApiData data = new ApiData();
data.title = Arrays.copyOf(product.getTitle(), product.getTitle().length);
...
arrayListData.add(data);
当您需要第一个api通话的标题时,您只需使用arrayListData.get(0).title
。
(对于奖励积分,encapsulate ApiData类中的公共字段。)