[英]how to call method of one fragment from another fragment class in android
[英]How to call fragment method from java class?
我嘗試從Java類調用片段方法:我知道關於該主題的堆棧溢出問題很多,但我的情況有所不同
ConnectionAsyncTask.java:
import android.app.Activity;
import android.os.AsyncTask;
import java.util.List;
public class ConnectionAsyncTask extends AsyncTask<String,String,String> {
Activity activity;
private EventListener listener;
public ConnectionAsyncTask(Activity activity) {
this.activity=activity;
}
@Override
protected String doInBackground(String... params) {
String content =HttpManager.getData(params[0]);
return content;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
List<Country> countries= CountryJasonParser.getObjectFromJason(s);
if (countries != null) {
String s1 = "";
s1=s1+"\n"+"" + countries.get(0).getDes() ;
//i want to call DisplyOnTextView(s1)
}
}
}
countryfragment.java:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class countryfragment extends android.support.v4.app.Fragment implements NavigationView.OnNavigationItemSelectedListener {
TextView desc;
public countryfragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ConnectionAsyncTask connectionAsyncTask = new ConnectionAsyncTask(getActivity());
//This is a server which contains data to load it connectionAsyncTask.execute("http://www.mocky.io/v2/570d3677270000f600dc29b6");
View view = inflater.inflate(R.layout.fragment_countryfragment,container,false);
desc = (TextView) view.findViewById(R.id.desc);
return view;
}
public void DisplyOnTextView(String s) {
desc.setText(s);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
}
結果必須是:在文本視圖上顯示數據。
您不能從AsyncTask
直接更新UI視圖。 出於您的目的,您可以有一個偵聽器,以便它在countryfragment.java
觸發函數回調,並且可以從回調函數中使用結果更新TextView
。
我猜您首先已經有了這樣的界面。
public interface EventListener {
public void responseReceiver(String result);
}
現在,在您的Fragment
實現EventListener
並Override
responseReceiver
方法。
public class countryfragment extends
android.support.v4.app.Fragment implements
NavigationView.OnNavigationItemSelectedListener
, EventListener {
// Other code goes here
// Override the callback function
@Override
public void responseReceiver(String result) {
DisplyOnTextView(result);
}
}
現在,當啟動AsyncTask
您需要像這樣傳遞正確的偵聽器:
ConnectionAsyncTask connectionAsyncTask = new ConnectionAsyncTask(getActivity());
connectionAsyncTask.listener = this;
connectionAsyncTask.execute();
現在像這樣更改您的onPostExecute
:
@Override
protected void onPostExecute(String s) {
List<Country> countries= CountryJasonParser.getObjectFromJason(s);
if (countries != null) {
String s1 = "";
s1=s1+"\n"+"" + countries.get(0).getDes() ;
listener.responseReceiver(s1);
}
}
嘗試這個:
public class ConnectionAsyncTask extends AsyncTask<String,String,String> {
Activity activity;
private EventListener listener;
private TextView mTextView
public ConnectionAsyncTask(Activity activity, TextView txt) {
this.activity=activity;
this.mTextView=txt;
}
@Override
protected String doInBackground(String... params) {
String content =HttpManager.getData(params[0]);
return content;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
List<Country> countries= CountryJasonParser.getObjectFromJason(s);
if (countries != null) {
String s1 = "";
s1=s1+"\n"+"" + countries.get(0).getDes() ;
mTextView.setText(s1);
}
}
}
接着
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_countryfragment,container,false);
desc = (TextView) view.findViewById(R.id.desc);
ConnectionAsyncTask connectionAsyncTask = new ConnectionAsyncTask(getActivity(), desc);
connectionAsyncTask.execute("http://www.mocky.io/v2/570d3677270000f600dc29b6");
return view;
}
您只需要將活動中的偵聽器發送到asyncTask即可從onPostExecute進行調用
碼:
public class Task extends AsyncTask<String,String,String> {
private TaskListener _listenr;
public Task(TaskListener listener) {
_listenr = listener;
}
@Override
protected String doInBackground(String... params) {
String res = "";
//some logic
return res;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
//some logic
if(_listenr!=null)
{
_listenr.onRetrieveData(s);
}
}
public interface TaskListener
{
public void onRetrieveData(String res);
}
}
Fragment:
public class countryfragment extends Activity implements TaskListener {
TextView desc;
public countryfragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ConnectionAsyncTask connectionAsyncTask = new ConnectionAsyncTask(this);
//This is a server which contains data to load it connectionAsyncTask.execute("http://www.mocky.io/v2/570d3677270000f600dc29b6");
View view = inflater.inflate(R.layout.fragment_countryfragment,container,false);
desc = (TextView) view.findViewById(R.id.desc);
return view;
}
public void DisplyOnTextView(String s) {
desc.setText(s);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
@Override
public void onRetrieveData(String res)
{
//some logic here with res
}
}
盡管不建議對HTTP調用使用AsyncTask,但您可能仍想在ConnectionAsyncTask中創建一個接口,例如
interface ConnectionAsyncTaskCallback{
void performAction(String s);
}
然后在您的片段中實現此接口
public class countryfragment extends android.support.v4.app.Fragment implements NavigationView.OnNavigationItemSelectedListener, ConnectionAsyncTask.ConnectionAsyncTaskCallback
您將必須實現performAction(String s)
方法,該方法將包含desc.setText(s);
最后,在ConnectionAsyncTask
添加成員ConnectionAsyncTaskCallback callback;
您將在構造函數中執行此操作,最后在onPostExecute
( callback.performAction(s1);
)上callback.performAction(s1);
。
TL; DR,這是修改后的代碼:
ConnectionAsyncTask:
public class ConnectionAsyncTask extends AsyncTask<String,String,String> {
Activity activity;
private EventListener listener;
private ConnectionAsyncTaskCallback callback;
public ConnectionAsyncTask(Activity activity, ConnectionAsyncTaskCallback callback) {
this.activity=activity;
this.callback = callback;
}
@Override
protected String doInBackground(String... params) {
String content =HttpManager.getData(params[0]);
return content;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
List<Country> countries= CountryJasonParser.getObjectFromJason(s);
if (countries != null) {
String s1 = "";
s1=s1+"\n"+"" + countries.get(0).getDes() ;
//i want to call DisplyOnTextView(s1)
callback.performAction(s1);
}
}
interface ConnectionAsyncTaskCallback {
void performAction(String s);
}
}
鄉村片段類別:
public class countryfragment extends android.support.v4.app.Fragment implements NavigationView.OnNavigationItemSelectedListener, ConnectionAsyncTask.ConnectionAsyncTaskCallback {
TextView desc;
public countryfragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ConnectionAsyncTask connectionAsyncTask = new ConnectionAsyncTask(getActivity());
//This is a server which contains data to load it connectionAsyncTask.execute("http://www.mocky.io/v2/570d3677270000f600dc29b6");
View view = inflater.inflate(R.layout.fragment_countryfragment,container,false);
desc = (TextView) view.findViewById(R.id.desc);
return view;
}
@Override
public void performAction(String s) {
desc.setText(s);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.