Search code examples
androidweb-servicesandroid-asynctaskpreferenceactivity

preference activity from dynamic array


getting data from webservices but values not getting out of async class,

public class Settings extends PreferenceActivity{

ListPreference lst1;
public static String[] batcharr;
public static String[] streamarr;

public Settings(){
  //  TPGetListone getlist = new TPGetListone();
  //  getlist.execute();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.settings);
    lst1 = (ListPreference) findPreference("prefStream");
    TPGetListone getlist = new TPGetListone();
    getlist.execute();
    lst1.setEntries(batcharr);
    lst1.setEntryValues(batcharr);
}

public class TPGetListone extends AsyncTask<String, Void, Void> {
    String datax = "";
    JSONArray array;
    List<String> lstbatch;
    List<String> lststream;
    private String LogStr = "Panchratna";

    @Override
    protected Void doInBackground(String... params) {
        DataHelper gd = new DataHelper("http://xxxx.com/EventGetter","http://xxxx.com/EventGetter.asmx?wsdl","http://xxxx.com/EventGetter/GetCourseList","GetCourseList");
        datax =  gd.GetData("query","SELECT batch, stream FROM batch");
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Log.i(LogStr, "onPostExecuteCATALOG");
        try {
            array = new JSONArray(datax);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        lstbatch = new ArrayList<String>();
        lststream = new ArrayList<String>();
        for(int i = 0; i < array.length(); i++){
            try {
                lstbatch.add(array.getJSONObject(i).getString("batch"));
                lststream.add(array.getJSONObject(i).getString("stream"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
         batcharr = new String[lstbatch.size()];
         streamarr = new String[lststream.size()];
        for (int i=0;i<lststream.size();i++){
            lststream.toArray(batcharr);
            lstbatch.toArray(streamarr);
        }
    }

    @Override
    protected void onCancelled(Void aVoid) {
        super.onCancelled(aVoid);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


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

i know this is not a proper way, i m using webservices to fetch json values and convert it to list<> by debugging, values do come in List which is lstbatch and lststream, and array batcharr and streamarr , but when the task executes , in on create method , batcharr and streamarr is showing error,

same method worked last time on an Activity

Please help , Thank You


Solution

  • Move this code above the Async and it will be accessible

     List<String> lstbatch;
    List<String> lststream;
    
    
    public class TPGetListone extends AsyncTask<String, Void, Void> {
    String datax = "";
    JSONArray array;
    //--> Remove to Up    -->List<String> lstbatch;
    //--> Remove to Up    -->List<String> lststream;
    private String LogStr = "Panchratna";
    

    EDIT

     for (int i=0;i<lststream.size();i++){
            batcharr[i]=lststream.get(i);
            streamarr[i]=lstbatch.get(i);
        }