0

I want to Update Text View ,spinner from AsycTask.Actually what i am doing

I Take the data from server and result return in json form then i parse JSON and populate the data in two Spinner and one textview.How can i update data when i am using Asyctask beacuse Textview Does not update..Here is my code sniped

TAG_CONFIG = "xyz"
TAG_CONFIG1 = "yyy"
JSONParser jParser = null;
JSONObject json = null;
jParser = new JSONParser();
json = jParser.getJSONFromUrl(url);

try {
    // Getting Array of Contacts
    contacts = json.getJSONArray(TAG_CONFIG);
    for (int i = 0; i < contacts.length(); i++) {
        JSONArray c = contacts.getJSONArray(i);
        CompanyName = new String[c.length()];
        for (int j = 0; j < c.length(); j++) {
            CompanyName[j] = c.getString(j);
        }


    }

similar for tab_confg1...

StarsSky
  • 6,721
  • 6
  • 38
  • 63
saqibabbasi
  • 423
  • 1
  • 5
  • 9

3 Answers3

2

doInBackground is not synced with the main UI thread... thus, you cannot directly update your UI from this method (you'd have to use a Handler or mContext.runOnUiThread() instead). You may, however, update your UI from onPostExecute, as this method is performed on the UI thread.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

Use runOnUiThread to access UI from a Non-UI Thread from doInBackground .

@Override
protected String doInBackground(Void... params) {
    Activity_Name.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // your code here to access UI elements

                }
            });
  return "string";
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

Try this...

1. Whenever we start an app in Android, we start on the Dedicated UI Thread.

2. Creation of any thread will drop one out of the UI Thread.

3. Keeping the UI work in UI thread and Non-UI work in Non-UI thread is advisable, but it went on to be the rule with the release of HoneyComb Android version.

4. Though Non-UI thread can be used to post the work from it to the UI thread using the handler, Android provides AsyncTask, which helps in Synchronization the work done on the Non-UI thread back to the UI thread

In AsyncTask<>

---> doInBackground() will handle the NON-UI WORK.

---> PostExectue() will help in post the NON-UI work back to the UI thread.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75