将每个循环中的Reformation2调用中的服务器响应保存到文件、数据库或新的List/ArrayList



我正在开发一个Android应用程序,并收到一个服务器响应,我想将其存储在数据库中或存储在ArrayList或List中,以便迭代这些值并将这些值与扫描的字符串进行比较;记录的输出是我需要和想要保存的数据;不幸的是,我的Java技能不太好,所以我真的不知道如何将这些数据保存在另一个List或ArrayList中。我需要的数据就在那里,所以我真的不知道如何存储…

这是API-Call:

public static void writeItemsToDatabase(Context mContext, String basic) {
//creating the itemApi interface
ItemApi itemApi = retrofit.create(ItemApi.class);
//making the call object
Call<List<Item>> call = itemApi.checkItems(basic);
call.enqueue(new Callback<List<Item>>() {
@Override
public void onResponse(@NonNull Call<List<Item>> call,
@NonNull Response<List<Item>> response) {
if (response.isSuccess()) {
List<Item> itemList;
itemList =  response.body();
int dataSize = response.body().size();
Log.d(TAGGG, String.valueOf(dataSize));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));
class DownloadTask extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
// Do something that takes a long time, for example:
try (DatabaseHandler erpDevelopment = new DatabaseHandler((XXXApp)
mContext.getApplicationContext())) {
itemList.stream().limit(4600).forEach(item -> {
erpDevelopment.addItem(item);
erpDevelopment.close();
});
}
// Call this to update your progress
return "this string is passed to onPostExecute";
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
}
}
new DownloadTask().execute();
}
}
@Override
public void onFailure(Call<List<Item>> call, Throwable t) {}
});
return;
}

这是项目类别:

package com.example.xxx;

import com.google.gson.annotations.SerializedName;
public class Item {
@SerializedName("no")
private String no;
@SerializedName("ean")
private String ean;
@SerializedName("name")
private String name;
@SerializedName("itemgroupname")
private String itemgroupname;
@SerializedName("type")
private String type;
@SerializedName("destruction")
private Boolean destruction;
@SerializedName("archived")
private Boolean archived;
public Item(String no, String ean, String name, String type, String itemgroupname, Boolean destruction,
Boolean archived) {
this.no = no;
this.name = name;
this.type = type;
this.ean = ean;
this.itemgroupname = itemgroupname;
this.destruction = destruction;
this.archived = archived;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getEan() {
return ean;
}
public void setEan(String ean) {
this.ean = ean;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getItemgroupname() {
return itemgroupname;
}
public void setItemgroupname (String Itemgroupname) {
this.itemgroupname = itemgroupname;
}
public boolean getDestruction() {
return destruction;
}
public void setDestruction (Boolean Destruction ) {
this.destruction = destruction;
}
public boolean getArchived() {
return true;
}
public void setArchived (Boolean Archived ) {
this.archived = archived;
}
}

我想稍后存储在数据库中,但首先存储在文件或ArrayList或List中的输出如下:

itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));

这正是我需要的数据,因此我真的不知道如何";放入";单独的数据;我有一个数据库,但首先我想把它存储在一个文件或ArrayList/List中,这取决于什么更有意义。

我该怎么做?ForEach循环如何分别保存List.getEan((和List.getNo((中的所有数据?如有任何提示或帮助,我们将不胜感激,提前表示感谢。

好的。。。首先声明两个字符串列表。一个用于Ean,另一个用于No.

List<String> EanList = new ArrayList<>();
List<String> NoList = new ArrayList<>();

像这样添加它们:

public static void writeItemsToDatabase(Context mContext, String basic) 
{
List<String> EanList = new ArrayList<>();
List<String> NoList = new ArrayList<>();
................
}

然后在响应成功时执行以下操作:

if(response.isSuccess())
{
int dataSize = itemList.size();
for(int i=0; i<dataSize; i++)
{
EanList.add(itemList.get(i).getEan());
NoList.add(itemList.get(i).getNo());
}
}

在这里,您基本上只需要将值复制到另外两个字符串列表中。在运行for循环之后,EanList和NoList将包含各自的值。

最新更新