英文:
Filtering in Android - issue that the code works but it doesn't return to the screen as expected
问题
我已经在Android中创建了一个应用程序,用于检索本地地区的餐馆并以列表视图返回它们。我为每个问题中的细节(名称/地址/邮政编码和卫生评分)创建了单独的数组。一些场所是豁免的,数组将以-1的形式返回信息,因此我想使用过滤器进行修改,我认为我已经做到了。
这是我的异步任务(Async Task),它返回JSON数组。
// 让事情在后台运行,JSON数组来检索列表
class RetrieveNearestRestaurantsTask extends AsyncTask<Void, Void, JSONArray> {
private Exception exception;
// 在后台执行搜索/填充数组列表
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected JSONArray doInBackground(Void... voids) {
// 如果searchUrl转换为空
if (!searchUrl.toString().isEmpty()) {
// 进行以下操作
try {
URL url = new URL(searchUrl.toString());
URLConnection tc = url.openConnection();
InputStreamReader isr = new InputStreamReader(tc.getInputStream());
BufferedReader in = new BufferedReader(isr);
String line; // 变量
// 当line(读入的行)不等于null时
// 创建一个新对象,并将其输出为JSON中的行
while ((line = in.readLine()) != null) {
ja = new JSONArray(line);
// 遍历数组的长度
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// 输出以满足基本功能要求
businessNames.add(JO.getString("BusinessName"));
postCodes.add(JO.getString("PostCode"));
addressList1.add(JO.getString("AddressLine1"));
addressList2.add(JO.getString("AddressLine2"));
addressList3.add(JO.getString("AddressLine3"));
// 如果餐馆的评分为-1,则显示豁免
// 应该显示
ratingValue.add(JO.getString("RatingValue"));
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt"));
calcDistance.add(JO.getString("DistanceKM"));
// 将所有内容一起输出
ConcatenateSearchResults();
}
}
isr.close();
in.close();
//return ja;
} catch (Exception e) {
this.exception = e;
return ja;
} finally {
//is.close();
}
}
return ja;
}
// 加载搜索结果
protected void onPostExecute(JSONArray jsonArray) {
LoadSearchResults();
}
}
连接搜索结果:
private ArrayList<String> ConcatenateSearchResults() {
int length = businessNames.size();
ArrayList<String> concatenatedResults = new ArrayList<>();
if (!businessNames.isEmpty() && !calcDistance.isEmpty() && !ratingValue.isEmpty()
&& !addressList1.isEmpty() && !addressList2.isEmpty() && !addressList3.isEmpty()
&& !postCodes.isEmpty()) {
for (int i = 0; i < length; i++) {
concatenatedResults.add("\n" + businessNames.get(i) +
"\nDistance (in Km) :" + calcDistance.get(i) +
"\n\nRating: " + ratingValue.get(i) +
"\nAddress Line 1: " + addressList1.get(i) +
"\nAddress Line 2: " + addressList2.get(i) +
"\nAddress Line 3: " + addressList3.get(i) +
"\nPostcode: " + postCodes.get(i));
}
}
return concatenatedResults;
}
}
但是,我认为某些地方我没有启用正确的ratingValue变量(?)来返回正确的信息(适用时显示豁免而不是-1),但我没有找出可能出错的地方。感谢任何帮助,我正在慢慢变得更擅长解释我不知道的东西。
英文:
I've created an app in Android to retrieve restaurants in the local area and return them in list view. I've created individual arrays for each of the details in question (name / address / postcode & hygiene rating). Some of the venues are exempt and the array will return the information as -1, so I want to alter this using a filter, which I think I have done.
This is my Async Task which returns the JSON array.
// lets things run in the background, JSON Array to retrieve the list
class RetrieveNearestRestaurantsTask extends AsyncTask<Void, Void, JSONArray> {
private Exception exception;
// performs the search in the background / populates the arraylist
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected JSONArray doInBackground(Void... voids) {
// if the searchUrl conversion is empty
if (!searchUrl.toString().isEmpty()) {
// do this instead
try {
URL url = new URL(searchUrl.toString());
URLConnection tc = url.openConnection();
InputStreamReader isr = new InputStreamReader(tc.getInputStream());
BufferedReader in = new BufferedReader(isr);
String line; // variable
// while line in (read in) is not equal to null
// create a new object and output as line in JSON
while ((line = in.readLine()) != null) {
ja = new JSONArray(line);
// run through the length of the array
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// output to meet Basic Functionality requirement
businessNames.add(JO.getString("BusinessName"));
postCodes.add(JO.getString("PostCode"));
addressList1.add(JO.getString("AddressLine1"));
addressList2.add(JO.getString("AddressLine2"));
addressList3.add(JO.getString("AddressLine3"));
// if the rating of the restaurant is -1, exempt
// should be displayed
ratingValue.add(JO.getString("RatingValue"));
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt"));
calcDistance.add(JO.getString("DistanceKM"));
// output everything together
ConcatenateSearchResults();
}
}
isr.close();
in.close();
//return ja;
} catch (Exception e) {
this.exception = e;
return ja;
} finally {
//is.close();
}
}
return ja;
}
Loads the search results.
protected void onPostExecute(JSONArray jsonArray) {
LoadSearchResults();
}
}
Concatenates the search results
private ArrayList<String> ConcatenateSearchResults()
{
int length = businessNames.size();
ArrayList<String> concatenatedResults = new ArrayList<>();
if(!businessNames.isEmpty() && !calcDistance.isEmpty() && !ratingValue.isEmpty()
&& !addressList1.isEmpty() && !addressList2.isEmpty() && !addressList3.isEmpty()
&& !postCodes.isEmpty())
{
for(int i=0; i < length; i++)
{
concatenatedResults.add("\n"+businessNames.get(i) +
"\nDistance (in Km) :"+ calcDistance.get(i) +
"\n\nRating: "+ ratingValue.get(i) +
"\nAddress Line 1: "+ addressList1.get(i) +
"\nAddress Line 2: "+ addressList2.get(i)+
"\nAddress Line 3: "+ addressList3.get(i) +
"\nPostcode: "+ postCodes.get(i));
}
}
return concatenatedResults;
}
}
However, I think somewhere I haven't enabled the proper ratingValue variable (?) to return the correct information (exempt instead of -1, where applicable, but I haven't worked out what I might have done wrong. Thank you for any help, I am slooowly getting better at explaining what I don't know.
专注分享java语言的经验与见解,让所有开发者获益!
评论