gpt4 book ai didi

java - 从另一个类获取 TextView

转载 作者:行者123 更新时间:2023-12-01 16:23:43 24 4
gpt4 key购买 nike

我想显示这个 TextView “txtCalculate”,它来自 CustomerMapActivity 类,该类显示在另一个布局的 Activity_map_customer 布局中,该布局是 BonDeCommande 类的 Activity_bon_de_commande。问题是我不知道该怎么做我是java编程新手谢谢

    public void readValues(){

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query lastQuery = ref.child("ride_info").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for(DataSnapshot ds : dataSnapshot.getChildren()){


double value0_float = ds.child("pickup").child("lat").getValue(Double.class);
double value1_float = ds.child("pickup").child("lng").getValue(Double.class);
double value2_float = ds.child("destination").child("lat").getValue(Double.class);
double value3_float = ds.child("destination").child("lng").getValue(Double.class);

String pickupLat = String.valueOf(value0_float);
String pickupLng = String.valueOf(value1_float);
String destiLat = String.valueOf(value2_float);
String destiLng = String.valueOf(value3_float);

String requestUrl=null;
try {
requestUrl = "https://maps.googleapis.com/maps/api/directions/json?"+
"mode=driving&"
+"transit_routing_preference=less_driving&"
+"origin="+pickupLat+","+pickupLng+"&"
+"destination="+destiLat+","+destiLng+"&"
+"key="+getResources().getString(R.string.google_maps_key);
Log.e("LINK",requestUrl);
mService.getPath(requestUrl).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
JSONArray routes = jsonObject.getJSONArray("routes");

JSONObject object = routes.getJSONObject(0);
JSONArray legs = object.getJSONArray("legs");

JSONObject legsObject = legs.getJSONObject(0);

//Get distance
JSONObject distance = legsObject.getJSONObject("distance");
String distance_text = distance.getString("text");
//use regex to extract double from string
//This regex will remove all text not digit
Double distance_value= Double.parseDouble(distance_text.replaceAll("[^0-9\\\\.]+",""));

//Get Time
JSONObject time = legsObject.getJSONObject("duration");
String time_text = time.getString("text");
Integer time_value = Integer.parseInt(time_text.replaceAll("\\D+",""));

String final_calculate = String.format("%.2f €",
TypeObject.getPrice(distance_value,time_value));

HERE -----> txtCalculate.setText(final_calculate);

} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(Call<String> call, Throwable t) {
mCurrentRide.cancelRide();
endRide();

}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
mCurrentRide.cancelRide();
endRide();
}
});
}

我的屏幕截图

enter image description here

最佳答案

您需要创建一个带有更新方法的接口(interface),在您的 Activity 中声明,然后将其作为参数传递给获取数据的处理程序对象。

不要忘记,如果您在不同的线程中获取数据,则需要始终在 UI 线程或主线程中更新 View 。

这里遵循一些示例代码:

您的 Activity 或 fragment

public class MainActivity extends AppCompatActivity
implements UpdateViewCallback { // implement the interface here

private TextView textView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

textView = findViewById(R.id.textView);

// Pass the interface in your Object that makes the async work
final AsyncWork asyncWork = new AsyncWork(this);

// Running
Thread thread = new Thread(asyncWork);
thread.start();
}

/**
* UpdateViewCallback
* @param result
*/
@Override
public void updateView(final String result) {
// Always update View on MainThread or an UI Thread, or else issues will start to happening
this.runOnUiThread(new Runnable() {
public void run() {
// Check if View is null since you're updating in a thread async
if (textView != null) {
textView.setText(result);
}
}
});
}

}

您的界面:

public interface UpdateViewCallback {
void updateView(String result);
}

处理异步工作的对象:

public class AsyncWork implements Runnable {

private UpdateViewCallback updateViewCallback;

public AsyncWork(UpdateViewCallback updateViewCallback) {
this.updateViewCallback = updateViewCallback;
}

/**
* Async Work here
*/
@Override
public void run() {
// Do some Work and after update using the interface you passed in the constructor
updateViewCallback.updateView("This is a test");
}
}

关于java - 从另一个类获取 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62203664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com