Я использую recyclerView в своем приложении, в котором я вычисляю расстояние между городами в потоке, отличном от потока пользовательского интерфейса. Теперь я хочу обновлять поле расстояния recyclerView только всякий раз, когда расстояние вычисляется.
ТИА
public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.ListItemViewHolder> {
private List<DashBoardData> dashBoardData;
private Context context;
public DashboardAdapter(List<DashBoardData> dashBoardData, Context context) {
if (dashBoardData == null) {
throw new IllegalArgumentException("Data must not be null");
}
this.dashBoardData = dashBoardData;
this.context = context;
}
@NonNull
@Override
public ListItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, viewGroup, false);
return new ListItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ListItemViewHolder holder, int position) {
holder.tvSource.setText(dashBoardData.get(position).getSource());
holder.tvDestination.setText(dashBoardData.get(position).getDestination());
getDistance(context,
dashBoardData.get(position).getStartLocation(),
dashBoardData.get(position).getEndLocation());
holder.tvDistance.setText("");
}
@Override
public int getItemCount() {
return dashBoardData.size();
}
public final static class ListItemViewHolder extends RecyclerView.ViewHolder {
TextView tvSource, tvDestination, tvDistance;
public ListItemViewHolder(View itemView) {
super(itemView);
tvSource = (TextView) itemView.findViewById(R.id.tv_from_source);
tvDestination = (TextView) itemView.findViewById(R.id.tv_to_destination);
tvDistance = itemView.findViewById(R.id.tv_distance);
}
}
public static void getDistance(Context context, String srcLocation, String destLocation) {
String url = getDirectionsUrl(context, srcLocation, destLocation);
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(url);
}
public static class DownloadTask extends AsyncTask<String, Void, String> {
// Downloading data in non-ui thread
@Override
protected String doInBackground(String... url) {
String data = "";
try {
// Fetching the data from web service
String urlEncoded = (url[0]).replaceAll(" ", "");
data = downloadUrl(urlEncoded);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
private static String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception downloading url", e.toString());
} finally {
if (iStream != null) {
iStream.close();
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return data;
}
private static class ParserTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... jsonData) {
JSONObject jObject;
String distance = "0 Mt";
try {
jObject = new JSONObject(jsonData[0]);
JSONArray jRoutes = null;
JSONArray jLegs = null;
jRoutes = jObject.getJSONArray("routes");
for (int i = 0; i < jRoutes.length(); i++) {
jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
for (int j = 0; j < jLegs.length(); j++) {
distance = (String) ((JSONObject)((JSONObject) jLegs.get(j)).get("distance")).get("text");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return distance;
}
@Override
protected void onPostExecute(String result) {
//update ui
}
}
}
это мой адаптер, и я передаю ему список приборной панели. После анализа данных json на предмет расстояния я хочу уведомить поле расстояния в методе onPostExecute ().