【问题标题】:how to fetch data from php and display in android listview in FRAGMENT?如何从 php 中获取数据并在 FRAGMENT 的 android listview 中显示?
【发布时间】:2015-05-04 18:09:16
【问题描述】:

onPostExecute() 中抛出错误:

构造函数 SimpleAdapter(Commodities, ArrayList>, int, String[], int[]) 是 未定义

问题出在我的onPostExecute()AsyncTask

@Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ListAdapter adapter = new SimpleAdapter(Commodities.this,
                    matchFixtureList, R.layout.list_item, new String[] {
                            TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
                            TAG_STOPLOSS, TAG_STATUS }, new int[] {
                            R.id.Script, R.id.BuySell, R.id.TradeSell,
                            R.id.Target, R.id.StopLoss, R.id.Status });
            setListAdapter(adapter);
        }

我该如何解决这个错误?这是整个类的代码:

public class Commodities extends ListFragment {

    private String URL_ITEMS = "http://ourvadodara.ddns.net/NeptuneFinsol/commodities_fetch.php";
    private static final String TAG_COMMODITIES = "commodities";
    private static final String TAG_DATE = "date";
    private static final String TAG_SCRIPT = "script";
    private static final String TAG_BUYSELL = "buysell";
    private static final String TAG_TRADESELL = "tradesell";
    private static final String TAG_TARGET = "target";
    private static final String TAG_STOPLOSS = "stoploss";
    private static final String TAG_STATUS = "status";


    JSONArray matchFixture = null;
    ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_bank__details,
                container, false);
        new GetFixture().execute();
        return rootView;
    }

    private class GetFixture extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg) {
            ServiceHandler serviceClient = new ServiceHandler();
            Log.d("url: ", "> " + URL_ITEMS);
            String json = serviceClient.makeServiceCall(URL_ITEMS,
                    ServiceHandler.GET);
            // print the json response in the log
            Log.d("Get match fixture response: ", "> " + json);
            if (json != null) {
                try {
                    Log.d("try", "in the try");
                    JSONObject jsonObj = new JSONObject(json);
                    Log.d("jsonObject", "new json Object");
                    // Getting JSON Array node
                    matchFixture = jsonObj.getJSONArray(TAG_COMMODITIES);
                    Log.d("json aray", "user point array");
                    int len = matchFixture.length();
                    Log.d("len", "get array length");
                    for (int i = 0; i < matchFixture.length(); i++) {
                        JSONObject c = matchFixture.getJSONObject(i);
                        String Script = c.getString(TAG_SCRIPT);
                        Log.d("Script", Script);
                        String BuySell = c.getString(TAG_BUYSELL);
                        Log.d("BuySell", BuySell);
                        String TradeSell = c.getString(TAG_TRADESELL);
                        Log.d("TradeSell", TradeSell);
                        String Target = c.getString(TAG_TARGET);
                        Log.d("Target", Target);
                        String StopLoss = c.getString(TAG_STOPLOSS);
                        Log.d("StopLoss", StopLoss);
                        String Status = c.getString(TAG_STATUS);
                        Log.d("Status", Status);

                        // hashmap for single match
                        HashMap<String, String> matchFixture = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        matchFixture.put(TAG_SCRIPT, Script);
                        matchFixture.put(TAG_BUYSELL, BuySell);
                        matchFixture.put(TAG_TRADESELL, TradeSell);
                        matchFixture.put(TAG_TARGET, Target);
                        matchFixture.put(TAG_STOPLOSS, StopLoss);
                        matchFixture.put(TAG_STATUS, Status);
                        matchFixtureList.add(matchFixture);
                    }
                } catch (JSONException e) {
                    Log.d("catch", "in the catch");
                    e.printStackTrace();
                }
            } else {
                Log.e("JSON Data", "Didn't receive any data from server!");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ListAdapter adapter = new SimpleAdapter(Commodities.this,
                    matchFixtureList, R.layout.list_item, new String[] {
                            TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
                            TAG_STOPLOSS, TAG_STATUS }, new int[] {
                            R.id.Script, R.id.BuySell, R.id.TradeSell,
                            R.id.Target, R.id.StopLoss, R.id.Status });
            setListAdapter(adapter);
        }
    }
}

【问题讨论】:

    标签: android json listview android-fragments android-fragmentactivity


    【解决方案1】:

    由于CommoditiesListFragment,因此您不能将this 用作Context

    尝试使用Commodities.this.getActivity() 而不是Commodities.this

            ListAdapter adapter = new SimpleAdapter(Commodities.this.getActivity(),
                    matchFixtureList, R.layout.list_item, new String[] {
                            TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
                            TAG_STOPLOSS, TAG_STATUS }, new int[] {
                            R.id.Script, R.id.BuySell, R.id.TradeSell,
                            R.id.Target, R.id.StopLoss, R.id.Status });
    

    您在此处需要的Context 是包含此ListFragmentActivity

    the documentation开始,构造函数的第一个参数是:

    context 与此 SimpleAdapter 关联的 View 的上下文 正在运行

    另外需要注意的是,当你调用setListAdapter()时,你需要使用Commodities.this

     //setListAdapter(adapter); //won't work
     Commodities.this.setListAdapter(adapter); //use this instead
    

    这是因为setListAdapter()在这里是ListFragment的方法,see documentation

    【讨论】:

    • 如果我想使用 extends FRAGMENT 那么我应该做些什么改变?
    • @Override protected void onPostExecute(Void result) { super.onPostExecute(result); ListAdapter 适配器 = new SimpleAdapter(Commodities.this.getActivity(), matchFixtureList, R.layout.list_item, new String[] { TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET, TAG_STOPLOSS, TAG_STATUS }, new int[] { R.id.Script , R.id.BuySell, R.id.TradeSell, R.id.Target, R.id.StopLoss, R.id.Status }); setListAdapter(adapter);} 如果我想使用片段而不是 ListFragment 那么呢?
    • 是的,当我使用 ListFragment 时它开始工作,但如果我使用 extends Fragment 则错误抛出 setListAdapter (对于 Commodities.GetFixture 类型的方法 setListAdapter(ListAdapter) 未定义)@DanielNugent
    • 你能告诉我怎么做吗?如果可能的话,你能告诉我代码吗?请发给我代码。我是android领域的新手
    • 我能得到你的电子邮件地址吗?请在 iphonic.pre@live.com 上回复我您的电子邮件地址,以便我可以向您发送我的文件。需要帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多