android中的异步任务无法正常工作

时间:2011-11-18 20:03:14

标签: android android-asynctask

这是我的问题thread exiting error in android

中的后续问题

我创建了一个异步任务,但这些值没有显示在列表视图中....

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();
    }
}
}

问题是适配器中的值不会被修改......

2 个答案:

答案 0 :(得分:2)

在AsyncTask中填充listItem之后,使用空listItems初始化适配器。 onPostExecute(),你的适配器没有得到更新,试试这个:

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

希望有所帮助。

答案 1 :(得分:1)

listItems = result;不起作用,您需要使用:

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

如果您创建另一个列表,您的适配器将不会知道它,因为它保留对旧列表的引用(保持不变)。