我有一個片段顯示來自 API 的資料,但我不想在每次呼叫片段時都呼叫 apis..所以我從不同的類呼叫 api 并呼叫片段中的方法來更新 UI(textViews)但是我獲取 textViews 的 null 例外,但那里的資料為 100%
如果我嘗試硬編碼 textView.setText("Hello") 它作業..
uj5u.com熱心網友回復:
Fragment 的任務之一是改變用戶界面!!!您必須在活動或片段中更改 ui
你必須使用interface. 您的服務器類:
public class server {
.
.
.
public void callServer(IServerResponse iResponse){
.
.
.
iResponse.onResponse(//your data such as List or string or json and etc);
}
public interface IServerResponse{
// use parameter type as you want like List or string ,etc..
void onResponse(String data);
}
}
您的片段或活動實作您的界面:
public class SomeFragment extends Fragment implements Server.IServerResponse{
....
@Override
public View onCreateView(...){
...
Server server=new Server();
server.callServer(this);
}
@Override
public void IServerResponse(String data){
textview.SetText(data);
}
}
uj5u.com熱心網友回復:
您應該在您的活動中創建一個 FrameLayout 并使用以下代碼:
FragmentA newFragment = new FragmentA ();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment).commit();
然后設定你的文字
String editValueName= editName.getText().toString();
String lastStatus = valueStatus.getText().toString();
newFragment .setText(editValueName, lastStatus);
uj5u.com熱心網友回復:
如果它適用于您想要的輸出,請嘗試快速修復
使 Textview 在類(您的活動)中保持靜態,textview其來源如下
public static TextView textview;
并在片段中
if(youractivity.textView!=null) {youractivity.textView.setText("your desire text");}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/318710.html
上一篇:C 中兩個排序陣列的并集
