0

onPostExecute()错误投掷:如何从PHP中获取数据并在android listview中显示在FRAGMENT中?

构造SimpleAdapter(商品, 的ArrayList>,整型,字符串[],INT [])是 未定义

的问题是在我的AsyncTaskonPostExecute()

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

回答

1

由于CommoditiesListFragment,您不能使用this作为Context

尝试使用Commodities.thisCommodities.this.getActivity()代替

 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,构造函数的第一个参数是:

方面在那里与此SimpleAdapter 相关的查看正在运行

另一个要注意的方面是,你会当您致电setListAdapter()时需要使用Commodities.this

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

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

+0

如果我想使用扩展FRAGMENT,那么我应该做什么改变? –

+0

@Override \t \t保护无效onPostExecute(无效结果){ \t \t \t super.onPostExecute(结果); \t \t \t ListAdapter适配器=新SimpleAdapter(Commodities.this.getActivity(), \t \t \t \t \t matchFixtureList,R.layout.list_item,新的String [] { \t \t \t \t \t \t \t TAG_SCRIPT,TAG_BUYSELL,TAG_TRADESELL ,TAG_TARGET, \t \t \t \t \t \t \t TAG_STOPLOSS,TAG_STATUS},新INT [] { \t \t \t \t \t \t \t R.id.Script,R.id.BuySell,R.id.TradeSell, \t \t \t \t \t \t \t R.id.Target,R.id.StopLoss,R.id.Status}); \t \t \t setListAdapter(适配器);}如果我想使用的片段,而不是ListFragment呢? –

+0

是它得到了如果我使用扩展片段,然后错误setListAdapter抛出的工作当我使用ListFragment但(该方法setListAdapter(ListAdapter)是未定义的类型Commodities.GetFixture)@DanielNugent –

相关问题