![](/img/trans.png)
[英]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.