简体   繁体   English

片段中未执行异步任务

[英]Async task not executed in Fragment

I've been googling quite a fair bit to make this work and somehow it doens't for me. 我一直在搜索相当多的内容来完成这项工作,但对我而言它根本不起作用。 What I'm doing is running fragment and when i open it, i wish for it to be populated by data called from the mySQL database into the listview. 我正在做的是运行片段,当我打开它时,我希望它由从mySQL数据库调用到列表视图中的数据填充。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.narutohd.ServiceHandler.ServiceHandler;

import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class FindPeopleFragment extends ListFragment {

     private ProgressDialog pDialog;

        // URL to get contacts JSON
        private static String url = "REMOVED";

        // JSON Node names
        private static final String TAG_CONTACTS = "products";
        private static final String TAG_ID = "pid";
        private static final String TAG_NAME = "link";

        // contacts JSONArray
        JSONArray contacts = null;
        private List<HashMap<String, String>> contactList;
        private SimpleAdapter adapter;

    public FindPeopleFragment(){}

    public void onActivityCreated(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            contactList = new ArrayList<HashMap<String, String>>();
            adapter = new SimpleAdapter(getActivity(), contactList,
                R.layout.list_item, new String[] { TAG_ID, TAG_NAME },
                new int[] { R.id.pid, R.id.episode });

            ListView lv = getListView();
            lv.setAdapter(adapter);
            new GetContacts().execute();
            super.onActivityCreated(savedInstanceState);
        }
     private class GetContacts extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {


            }

            @Override
            protected Void doInBackground(Void... arg0) {
               //Code
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
               //Code
            }

        }

}

That is my code currently, but all it does on loading is nothing, no errors whatsoever. 那是我目前的代码,但是加载时所执行的一切都没有,也没有任何错误。 I believe it should be fine because it works well on an activity. 我认为应该没问题,因为它可以很好地完成某项活动。 I don't see any reason why it should be. 我认为没有任何理由。 An alternative i thought of would be to pass the data onPostExecute into a public variable then populating it using setListAdapter real time (onActivityCreated) as i understand that the reason why it might not work as of now is because the listview is already inflated once the oNActivity is launched. 我想到的另一种方法是将数据onPostExecute传递到一个公共变量,然后使用setListAdapter实时(onActivityCreated)填充它,因为我知道它到目前为止可能不起作用的原因是因为一旦oNActivity,列表视图已经膨胀启动。

Would appreciate some help on this! 希望对此有所帮助!

Sorry as I'm rather new to Java/Android, was wondering if I'm doing it wrong, or is another way to do it possibly running the Async Task from the mainActivity, THEN populating the listview on the fragment then? 抱歉,我刚接触Java / Android,想知道我做错了吗,还是另一种可能的方法是从mainActivity运行Async Task,然后在片段上填充listview?

I ran the code below to use adapter.notifyDataSetChanged but it didn't work. 我运行下面的代码以使用adapter.notifyDataSetChanged,但是没有用。 The GetContacts still doesn't get executed. GetContacts仍然不会执行。

I'm pretty sure the problem is at that GetContacts DoinBackground isn't executed. 我很确定问题出在未执行GetContacts DoinBackground。 Or it doesn't seem to be as I don't even see my URL showing up (Which should, neither do i see pDialog initiated at all). 或似乎不是,因为我什至没有看到我的URL出现(这应该,我也根本看不到pDialog已启动)。

I think,you should call "adapter.notifyDataSetChanged();" 我认为,您应该调用“ adapter.notifyDataSetChanged();”

This method can dynamically modify the ListView data. 此方法可以动态修改ListView数据。

private List<HashMap<String, String>> contactList;
private SimpleAdapter adapter;

public void onActivityCreated(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    contactList = new ArrayList<HashMap<String, String>>();
    adapter = new SimpleAdapter(getActivity(), contactList,
        R.layout.list_item, new String[] { TAG_ID, TAG_NAME },
        new int[] { R.id.pid, R.id.episode });

    ListView lv = getListView();
    lv.setAdapter(adapter);
    new GetContacts().execute();
    super.onActivityCreated(savedInstanceState);
}


public class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... arg0) {
                    //Your code...
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        adapter.notifyDataSetChanged();
    }
}

I don't know it that is all of the code but I guess that you should add a onCreateView. 我不知道这是全部代码,但是我想您应该添加一个onCreateView。 And use a layout with at listview 并在listview上使用布局

我自己解决了它,使用onResume更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM