如何在AsyncTask中逐个展示arraylist?

时间:2014-05-18 08:18:59

标签: android arraylist android-asynctask

我正在使用arraylist存储来自游标选择的数据并返回主活动中的AsyncTask。为了逐个显示结果,我将for循环放在onPostExecute中。但屏幕上没有显示任何内容。当我在onPostExecute的第一次求助时把try和catch放在一起时,无法执行debug。那么AsyncTask代码出错并返回arraylist代码?

的AsyncTask:

protected class tryconnect extends AsyncTask<Void,Integer,Void>{
    @Override
    protected Void doInBackground(Void... arg0) {
        EditText city=(EditText)findViewById(R.id.city_address);
        String   mycity=city.getText().toString();
        EditText street=(EditText)findViewById(R.id.street_address);
        String   mystreet=street.getText().toString();
        EditText postcode=(EditText)findViewById(R.id.postcode_address);
        String   mypostcode=postcode.getText().toString();
        EditText number=(EditText)findViewById(R.id.number_address);
        String   mynumber=number.getText().toString();
        String   ReservedState="Free";
        List<String> result=dbHelper1.select1(ReservedState, mycity, mystreet, mypostcode);
        return null;
    }
    protected void onPostExecute(List<String> result) {
        // TODO Auto-generated method stub
        try{
            for(int i=0;i<result.size();i++){
                String b=result.get(i);
                // TODO Auto-generated method stub
                String baseURL="https://fontys.nedapparking.com/so.xml?Ln=adminfontys&Pw=f0nty589&Cm=1&Fm=A&PrkLt=1&Bay=";
                //c is the retrieving result from reservation table
                String a=b;
                StringBuilder URL=new StringBuilder(baseURL);
                URL.append(a);
                String fullURL=URL.toString();
                try{
                    URL website=new URL(fullURL);
                    //getting xmlreader to parse data
                    SAXParserFactory spf=SAXParserFactory.newInstance();
                    SAXParser sp=spf.newSAXParser();
                    XMLReader xr=sp.getXMLReader();

                    HandlingXMLStuff doingWork=new HandlingXMLStuff();
                    xr.setContentHandler(doingWork);
                    try{
                        xr.parse(new InputSource(website.openStream()));
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    information=doingWork.getInformation();
                }catch(Exception e){
                    e.printStackTrace();
                }
        }
            Message.message(FragmentHandler.this,information);
        }catch(Exception e){
            e.printStackTrace();
        }
        }
}

返回arraylist:

public List<String> select1 (String ReservedState, String mycity, String mystreet, String mypostcode){
    ReservedState="Free";
    SQLiteDatabase Database1=dbHelper1.getWritableDatabase();
    String[] columns={DbHelper1.KEY_ID1,DbHelper1.KEY_RESERVEDSTATE, DbHelper1.KEY_GEOGRAPHICALLOCATION,DbHelper1.KEY_CITY,DbHelper1.KEY_POSTCODE,DbHelper1.KEY_STREET};
    Cursor cursor=Database1.query(DbHelper1.TABLE_NAME_1,columns, DbHelper1.KEY_RESERVEDSTATE+"='"+ReservedState+"' AND "+DbHelper1.KEY_CITY+"='"+mycity+"' AND "+DbHelper1.KEY_POSTCODE+"='"+mypostcode+"' AND "+DbHelper1.KEY_STREET+"='"+mystreet+"'",null,null,null,null);
    StringBuffer buffer=new StringBuffer();
    List<String> al=new ArrayList<String>();
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        int index1=cursor.getColumnIndex(DbHelper1.KEY_ID1);
        String id=cursor.getString(index1);
        al.add(id);
        cursor.moveToNext();
    }
    return al;

1 个答案:

答案 0 :(得分:1)

首先,丢失try和catch子句,除非代码中的某些内容抛出异常

其次,在onPostExecute()的开头添加一项检查,以检查result是否为空

第三,当您应该返回要处理的列表时,您从doInBackground()返回null

更改此

List<String> result=dbHelper1.select1(ReservedState, mycity, mystreet, mypostcode);
return null;

这样的事情

return dbHelper1.select1(ReservedState, mycity, mystreet, mypostcode);