-1

I am using for loop to call asynctask this is how I call the asynctask in a Fragment:

private void LoadPartiList() {
for (int i = 0; i < participants.length; i ++){
    String getlistparti = cmd.getPartList();
    participants = getlistparti.split(",");
    partiparti = ((CommentandLikeActivity)getActivity()).getparticipantlist(participants[i]);
    Log.d("test","testlogcat " + partiparti);

}

here my asynctask which allocate in the activity which contain the fragment :

public ArrayList<User> getparticipantlist(final String participationID) {
    final ArrayList<User> list = new ArrayList<User>();
    final User user = new User();
    new AsyncTask<String, String, ArrayList<User>>() {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected ArrayList<User> doInBackground(String... params) {


            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters

                        Log.d("participantid", "17112015 " + participationID);
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("userid", participationID));


                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                "http://192.168.168.111:80/testing/participationlist.php", "GET", params);

                        // check your log for json response
                        Log.d("Single Product Details", json.toString());

                        // json success tag
                        success = json.getInt("success");
                        if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = json
                                    .getJSONArray("product"); // JSON Array

                            // get first product object from JSON Array
                            JSONObject product = productObj.getJSONObject(0);





                            user.setId(String.valueOf(product.getString(String.valueOf("vuid"))));
                            user.setUsername(product.getString("vusername"));
                            user.setProfileimage(product.getString("vprofileimage"));
                            list.add(user);

                            Log.d("","getwalalala " +product.getString(String.valueOf("vuid")) +" : "+ product.getString("vusername") + " : " +product.getString("vprofileimage") );


                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return list;
        }



        @Override
        protected void onPostExecute(ArrayList<User> list) {
            List<User> mUserlist = new ArrayList<User>();
            mAdapterr = new participantlistAdapter(getApplicationContext(), mUserlist);
            mAdapterr.Update();
            mAdapterr.add(user);

        }
    }.execute(null, participationID, null);

    return list;
}

here is what I Log when run the Asynctask

Logcat for Log.d("","getwalalalala")

but why it will just return [] or null? any wrong with my code? help please... I want to loop the userid to get username from phpserver according to the length in the participants.length and display in the listview, how should I do it? I get no answer after research online for few days

MellisaNg
  • 61
  • 7

1 Answers1

0

As you are trying to return data from AsyncTask which is not correct. AsyncTask doInBackground return data to your onPostExecute. So whatever you are trying to return can only be received in onPostExecute. but still you want to implement Asynctask then you achieve this by one of the following way:

  1. By defining AsyncTask as inner private class in your activity and directly using the data in onPostExecute to update your ui.

  2. Second method is to pass interface callback to your asynctask and with the help of that interface callback update your ui from AsyncTask to activity.

Anjali
  • 1
  • 1
  • 13
  • 20