Issue
I need help on how to call a method that is in a fragment from a class AsyncTask
. I've been looking to similar questions, I've tried some things and it has not worked for me.
The problem:
The AsyncTask
class downloads an XML and saves some tags that interest me. Next in the method onPostExecute
I have to call another method that is in the Fragment
class to fill in the textviews with the downloaded values of the XML.
Code:
public class XMLParse extends AsyncTask<String, Void, Void> {
public static String v_temperatura = "", v_humitat = "", v_pressio = "", v_pluja = "", v_hora = "", v_vent = "";
protected void onPostExecute(Void args) {
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
v_temperatura = getNode("temp", eElement);
v_humitat =getNode("hum", eElement);
v_pressio = getNode("baro", eElement);
v_vent = getNode("windinmph", eElement);
v_pluja = getNode("todayraininmm", eElement);
v_hora = getNode("time", eElement);
if (onAnar.equals("widget")) {
WidgetActivity widgetActivity = new WidgetActivity();
widgetActivity.omplirTextViews(MyApplication.getAppContext());
} else if (onAnar.equals("frgDades")) {
/*
* Here I need to call the fragment method to
* fill the values in the textviews
*
* Example:
* DadesActualsFragment dadesActualsFragment = new DadesActualsFragment();
* dadesActualsFragment.omplirTextViews();
*/
}
}
}
}
}
Edit:
This is the code that execute the AsyncTask:
@Override
public void onRefresh() {
Toast.makeText(MyApplication.getAppContext(), "test", Toast.LENGTH_SHORT).show();
try {
XMLParse xmlParse = new XMLParse();
xmlParse.execute("frgDades");
} catch (Exception e) {
e.printStackTrace();
}
mSwipeRefreshLayout.setRefreshing(false);
}
Fragment TextViews code: The omplirTextViews() method take the value of the variables that are defined to the XMLParse class.
public class DadesActualsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshLayout;
TextView tv_app_temperatura_val, tv_app_humitat_val, tv_app_pressio_val, tv_app_vent_val, tv_app_pluja_val, tv_app_hora_val;
public DadesActualsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.fragment_dades_actuals, container, false);
tv_app_temperatura_val = myInflatedView.findViewById(R.id.tv_app_temperatura_val);
tv_app_humitat_val = myInflatedView.findViewById(R.id.tv_app_humitat_val);
tv_app_pressio_val = myInflatedView.findViewById(R.id.tv_app_pressio_val);
tv_app_vent_val = myInflatedView.findViewById(R.id.tv_app_vent_val);
// Inflate the layout for this fragment
return myInflatedView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// Implementació del Swipe Refresh
mSwipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.fragment_dadesActuals_swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
Toast.makeText(MyApplication.getAppContext(), "test", Toast.LENGTH_SHORT).show();
// Executar l'XML parse cada cop que es refresca el widget
try {
//XMLParse xmlParse = new XMLParse(dadesActualsFragment);
//xmlParse.execute("frgDades");
DadesActualsFragment dadesActualsFragment = new DadesActualsFragment();
new XMLParse(dadesActualsFragment).execute("frgDades");
} catch (Exception e) {
e.printStackTrace();
}
mSwipeRefreshLayout.setRefreshing(false);
}
public void omplirTextViews() {
System.out.println("------------------");
tv_app_temperatura_val.setText(XMLParse.v_temperatura);
tv_app_humitat_val.setText(XMLParse.v_humitat);
tv_app_pressio_val.setText(XMLParse.v_pressio);
tv_app_vent_val.setText(XMLParse.v_vent);
}
}
How can I do it? Regards.
Solution
You could do something like this, you can pass the fragment instance while calling the Asynctask constructor. Please see the code below.
class WorkerTask extends AsyncTask<Void, Void, Void> {
private DadesActualsFragment dadesActualsFragment;
WorkerTask(DadesActualsFragment dadesActualsFragment) {
this.dadesActualsFragment = dadesActualsFragment;
}
@Override
protected Void doInBackground(Void... params)
{
...
}
@Override
protected void onPostExecute(Void res)
{
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
v_temperatura = getNode("temp", eElement);
v_humitat =getNode("hum", eElement);
v_pressio = getNode("baro", eElement);
v_vent = getNode("windinmph", eElement);
v_pluja = getNode("todayraininmm", eElement);
v_hora = getNode("time", eElement);
if (onAnar.equals("widget")) {
WidgetActivity widgetActivity = new WidgetActivity();
widgetActivity.omplirTextViews(MyApplication.getAppContext());
} else if (onAnar.equals("frgDades")) {
dadesActualsFragment.omplirTextViews();
}
}
}
}
}
And you have to initialize and trigger AsyncTak like this
new WorkerTask(dadesActualsFragment).execute();
Answered By - lib4backer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.