如何从PHP获取数据并在FRAGMENT的android listview中显示?

时间:2015-05-04 18:09:17

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

投掷onPostExecute()时出错:

  

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

问题出在onPostExecute()的{​​{1}}:

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

1 个答案:

答案 0 :(得分: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是包含此Activity的{​​{1}}。

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

  

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

需要注意的另一点是,当您致电ListFragment时,您需要使用Commodities.this

setListAdapter()

这是因为 //setListAdapter(adapter); //won't work Commodities.this.setListAdapter(adapter); //use this instead setListAdapter()的方法,see documentation