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

时间:2022-10-25 20:02:23

Error throwing in onPostExecute():

抛入onPostExecute()时出错:

The constructor SimpleAdapter(Commodities, ArrayList>, int, String[], int[]) is undefined

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

The problem is in onPostExecute() of my AsyncTask:

问题出在我的AsyncTask的onPostExecute()中:

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

How can I solve this error? Here is the whole class code:

我该如何解决这个错误?这是全班代码:

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 个解决方案

#1


Since Commodities is a ListFragment, you can't use this as a Context.

由于Commodities是ListFragment,因此您不能将其用作Context。

Try using Commodities.this.getActivity() instead of Commodities.this

尝试使用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 });

The Context that you need here is the Activity that contains this ListFragment.

这里需要的Context是包含此ListFragment的Activity。

From the documentation, the first parameter of the constructor is:

从文档中,构造函数的第一个参数是:

context The context where the View associated with this SimpleAdapter is running

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

Another thing to note is that you will need to use Commodities.this when you call setListAdapter():

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

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

This is because setListAdapter() is a method of ListFragment here, see documentation.

这是因为setListAdapter()是ListFragment的一个方法,请参阅文档。

#1


Since Commodities is a ListFragment, you can't use this as a Context.

由于Commodities是ListFragment,因此您不能将其用作Context。

Try using Commodities.this.getActivity() instead of Commodities.this

尝试使用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 });

The Context that you need here is the Activity that contains this ListFragment.

这里需要的Context是包含此ListFragment的Activity。

From the documentation, the first parameter of the constructor is:

从文档中,构造函数的第一个参数是:

context The context where the View associated with this SimpleAdapter is running

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

Another thing to note is that you will need to use Commodities.this when you call setListAdapter():

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

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

This is because setListAdapter() is a method of ListFragment here, see documentation.

这是因为setListAdapter()是ListFragment的一个方法,请参阅文档。