1

This is a follow up question from my question thread exiting error in android

I created an async task but the values do not show up in the list view....

public class History extends Activity implements OnItemClickListener
{
/** Called when the activity is first created. */
ListView list;

//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems;

//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
private String resDriver,resPassenger,ID;
private ProgressDialog dialog;
ArrayList<HashMap<String, Object>> listInfo = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> item;
JSONObject jDriver;
//JSONObject jPassenger;

// Make strings for logging
private final String TAG = this.getClass().getSimpleName();
private final String RESTORE = ", can restore state";
private final String state = "Home Screen taking care of all the tabs";
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Intent loginIntent = getIntent();
    ID = loginIntent.getStringExtra("ID");
    listItems = new ArrayList<String>();
    Log.i(TAG, "Started view active rides");
    setContentView(R.layout.searchresults);
    list = (ListView)findViewById(R.id.ListView01);
    list.setOnItemClickListener(this);
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems);

    list.setAdapter(adapter);
    getInfo();
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
// TODO Auto-generated method stub
    Toast.makeText(this, "u clicked " + listItems.get(position) ,Toast.LENGTH_LONG).show();
}

public void getInfo(){

    DownloadInfo task = new DownloadInfo();
    task.execute(new String[] { "http://www.vogella.de" });

}

private class DownloadInfo extends AsyncTask<String, Void , ArrayList<String>>{

    @Override
    protected ArrayList<String> doInBackground(String ... strings) {
        ArrayList<String> listItems1;
        jDriver = new JSONObject();
        try {
            jDriver.put("ID", ID);
            jDriver.put("task", "GET DATES");

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        listItems1 = new ArrayList<String>();
        Log.i(TAG,"Sending data for the driver rides");
        resDriver = HTTPPoster.sendJson(jDriver,"URL"); // Any Server URL

        JSONObject driver;
        try {
            driver = new JSONObject(resDriver);
            Log.i(TAG,"Recieved Driver details");
            if ( driver.getString("DATABASE ERROR").equals("False")){
                int length = Integer.parseInt( driver.getString("length"));
                Log.i(TAG,"length is " + length);
                for( int i =0 ; i< length ; i++){
                    String info = driver.getString(Integer.toString(i));
                    String[] array = info.split(",");
                    Log.i(TAG,"array is " + Arrays.toString(array));
                    Log.i(TAG,"Date "+ array.length);
                    Log.i(TAG,"DONE WITH THE LOOP");
                    //listInfo.add(item);
                    Log.i(TAG,"Date is"+array[0]);
                    listItems1.add(array[0]);                           
                }
            }
        } catch (JSONException e) {
        // TODO Auto-generated catch block
            listItems1.add("No driver rides created");
        }
        return listItems1;
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {

        listItems = result;
        adapter.notifyDataSetChanged();
    }
}
}

The problem is that the values in the adapter do not get modified...

2 Answers 2

2

You initialize your adapter with a empty listItems, after your fill your listItems in AsyncTask. onPostExecute(), your adapter is not get updated, try this:

protected void onPostExecute(ArrayList<String> result) {
  listItems = result;
  adapter.addAll(ListItems);
  adapter.notifyDataSetChanged();
}

Hope that help.

1

listItems = result; won't work, you need to use :

listItems.clear();
listItems.addAll(result);

If you create another list, your adapter won't know it, because it keeps a reference to the old list (which remains the same).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.