Samah Samah - 7 months ago 63
Java Question

How to call fragment method from java class?

I try to call fragment method from java class:
I know there many of questions on stack overflow on this topic, but my case is different

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;
}
}





The result must be:
Display data on text view.

Answer

try this:

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);
        } 
    }
}

and then

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;
    }