Skip to main content
deleted 10 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Handling AsyncTask, is there Filling a better way?spinner from JSON web data

The code below is used to download a JSON data from the web and use the data to fill a spinner. I would like to know if there is better way. Is it wise to move everything from onGetPriceType to the AsyncTask?

Interface

Interface

AsyncTask

AsyncTask

Activity

Activity

Handling AsyncTask, is there a better way?

The code below is used to download a JSON data from web and use the data to fill a spinner. I would like to know if there is better way. Is it wise to move everything from onGetPriceType to the AsyncTask?

Interface

AsyncTask

Activity

Filling a spinner from JSON web data

The code below is used to download a JSON data from the web and use the data to fill a spinner. I would like to know if there is better way. Is it wise to move everything from onGetPriceType to the AsyncTask?

Interface

AsyncTask

Activity

added 29 characters in body; edited title
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309

Handling AsyncTask, is there a better way to handle asynctask?

CodeThe code below is used to download jsonfilea JSON data from web and use itsthe data to fill a spinner. I wishwould like to know if there is better way. Is it wise to move everything from onGetPriceTypeonGetPriceType to AsyncTaskthe AsyncTask?

public interface GetBuildTypeInterface {
    public void onGetBuildType(HashMap<String,String> result); 
}

is there a better way to handle asynctask?

Code below is used to download jsonfile from web and use its data to fill spinner. I wish to know if there is better way. Is it wise to move everything from onGetPriceType to AsyncTask?

public interface GetBuildTypeInterface {
    public void onGetBuildType(HashMap<String,String> result);}

Handling AsyncTask, is there a better way?

The code below is used to download a JSON data from web and use the data to fill a spinner. I would like to know if there is better way. Is it wise to move everything from onGetPriceType to the AsyncTask?

public interface GetBuildTypeInterface {
    public void onGetBuildType(HashMap<String,String> result); 
}
formatting and added a tag
Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

Interface

Interface

AsyncTask

AsyncTask

public class GetBuildType extends AsyncTask<Void, Void, JSONArray> {
    private GetBuildTypeInterface callback;

    public GetBuildType(GetBuildTypeInterface callback) {
        this.callback = callback;
    }

    @Override
    protected JSONArray doInBackground(Void... params) {
        UserFunctions u = new UserFunctions();
        return u.getBt();
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);
        HashMap<String, String> map = new HashMap<String, String>();

        if (result != null) {
            map = result.parse();
            callback.onGetBuildType(map);

        } else {
            callback.onGetBuildType(null);
        }
    }
 
}

Activity

Activity

public class AddFillActivityApp extends ErrorActivity implements
        GetBuildTypeInterface

{
    ArrayAdapter<String> adapterBuild;
    Spinner spinBuildType;

@Override
    public void onGetPriceType(final HashMap<String, String> result) {
        if (result != null) {
            List<String> fields = new ArrayList<String>();
            fields.addAll(result.keySet());
            adapterPrice = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, fields);
            adapterPrice
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinPriceType.setAdapter(adapterPrice);

            spinPriceType
                    .setOnItemSelectedListener(new OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {

                            if (position > -1) {
                                String selection = spinPriceType
                                        .getSelectedItem().toString();
                                a.setNot_priceFor(result.get(selection));
                            }
    
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
     
                        }
                    });
        }else error();
    }
}

Interface

AsyncTask

public class GetBuildType extends AsyncTask<Void, Void, JSONArray> {
    private GetBuildTypeInterface callback;

    public GetBuildType(GetBuildTypeInterface callback) {
        this.callback = callback;
    }

    @Override
    protected JSONArray doInBackground(Void... params) {
        UserFunctions u = new UserFunctions();
        return u.getBt();
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);
        HashMap<String, String> map = new HashMap<String, String>();

        if (result != null) {
            map = result.parse();
            callback.onGetBuildType(map);

        } else {
            callback.onGetBuildType(null);
        }
    }
 
}

Activity

public class AddFillActivityApp extends ErrorActivity implements
        GetBuildTypeInterface

{
    ArrayAdapter<String> adapterBuild;
    Spinner spinBuildType;

@Override
    public void onGetPriceType(final HashMap<String, String> result) {
        if (result != null) {
            List<String> fields = new ArrayList<String>();
            fields.addAll(result.keySet());
            adapterPrice = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, fields);
            adapterPrice
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinPriceType.setAdapter(adapterPrice);

            spinPriceType
                    .setOnItemSelectedListener(new OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {

                            if (position > -1) {
                                String selection = spinPriceType
                                        .getSelectedItem().toString();
                                a.setNot_priceFor(result.get(selection));
                            }
 
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
 
                        }
                    });
        }else error();
    }
}

Interface

AsyncTask

public class GetBuildType extends AsyncTask<Void, Void, JSONArray> {
    private GetBuildTypeInterface callback;

    public GetBuildType(GetBuildTypeInterface callback) {
        this.callback = callback;
    }

    @Override
    protected JSONArray doInBackground(Void... params) {
        UserFunctions u = new UserFunctions();
        return u.getBt();
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);
        HashMap<String, String> map = new HashMap<String, String>();

        if (result != null) {
            map = result.parse();
            callback.onGetBuildType(map);

        } else {
            callback.onGetBuildType(null);
        }
    }
}

Activity

public class AddFillActivityApp extends ErrorActivity implements
        GetBuildTypeInterface

{
    ArrayAdapter<String> adapterBuild;
    Spinner spinBuildType;

@Override
    public void onGetPriceType(final HashMap<String, String> result) {
        if (result != null) {
            List<String> fields = new ArrayList<String>();
            fields.addAll(result.keySet());
            adapterPrice = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, fields);
            adapterPrice
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinPriceType.setAdapter(adapterPrice);

            spinPriceType
                    .setOnItemSelectedListener(new OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {

                            if (position > -1) {
                                String selection = spinPriceType
                                        .getSelectedItem().toString();
                                a.setNot_priceFor(result.get(selection));
                            }   
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {    
                        }
                    });
        }else error();
    }
}
Source Link
Yarh
  • 255
  • 1
  • 3
  • 8
Loading