Break AsyncTaskResult <arraylist <hashmap <string,string =“”>&gt;&gt;到另一个列表</arraylist <hashmap <string,>

时间:2014-01-03 08:56:15

标签: android

有没有办法打破AsyncTaskResult&gt;&gt; 进入列表? 或者得到30,40,......它的价值只有?

我有一个Async类,它将webservice fetch的结果返回给适配器。 现在因为巨大的数据我希望将结果分成单独的段并传递 异步类的结果数。

这是myasync类:

protected AsyncTaskResult<ArrayList<HashMap<String, String>>> doInBackground(
        String... arg0) {

        String xml = "Get String from my web service class"
        myXmlParsingClass myparser = new myXmlParsingClass (xml, "getitem");
        myparser .runParser();

        AsyncTaskResult<ArrayList<HashMap<String, String>>> myresult = new AsyncTaskResult<ArrayList<HashMap<String, String>>>(myXmlParsingClass .getParsedData());

        return myresult ;

    }

和我的asynctaskresult:

public class AsyncTaskResult<T> {
    private T result = null;
    private Exception error = null;

    public T getResult() {
        return result;
    }

    public Exception getError() {
        return error;
    }

    public AsyncTaskResult(T result) {
        super();
        this.result = result;
    }

    public AsyncTaskResult() {
        super();
    }

    public AsyncTaskResult(Exception error) {
        super();
        this.error = error;
    }

这是我到目前为止所做的: 在我的适配器里面的getview方法中,我做了第二个方法将一个数字传递给异步类:

MyAsyncClass ma = new MyAsyncClass(myview,userid,5);
MyAsyncClass中的

public MyAsyncClass ( View context,String _id) {
    this.targetCtx = context ;
    id = _id;
}

public MyAsyncClass ( View context,String _id,int _ID_To_Show) {
    this.targetCtx = context ;
    id = _id;
    ID_To_Show= _ID_To_Show;
}

和PostExecute方法:

if(ID_To_Show >0 ){
     MySecondAdapter Madapter = new MySecondAdapter(targetCtx,        myresult.getResult(),ID_To_Show);
     mylist.setAdapter(Madapter);
   }else{
   MySecondAdapter Madapter = new MySecondAdapter(targetCtx, myresult.getResult());
   mylist.setAdapter(Madapter);

最后在MySecondAdapter中添加了这个:

public MySecondAdapter(View a, ArrayList<HashMap<String, String>> id,int _ID_To_Show) {
    idfromhayoolafetch = id;
    myinflater = (LayoutInflater) AppContext.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ID_To_Show= _ID_To_Show;
}
public MySecondAdapter(View a, ArrayList<HashMap<String, String>> id) {
    idfromhayoolafetch = id;
    myinflater = (LayoutInflater) AppContext.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
    if(ID_To_Show>0){
        return ID_To_Show;  
    }else{
    return idfromhayoolafetch.size();
    }
    }

此approch显示我想在每次通话中显示的项目数量,我可以将值传递给它 但其他方式我可以做到这一点? 问题是我不能要求hayoola限制回应的价值。

1 个答案:

答案 0 :(得分:0)

这样的请求应由服务器自己处理。 因为服务器应该接受要返回的项目数,并且只发送那么多项。

但是如果你想要异步任务来处理它,你仍然可以执行以下操作:

protected AsyncTaskResult<ArrayList<HashMap<String, String>>> doInBackground(
        String... arg0) {

        String xml = "Get String from my web service class"
        myXmlParsingClass myparser = new myXmlParsingClass (xml, "getitem");
        myparser .runParser();

        AsyncTaskResult<ArrayList<HashMap<String, String>>> myresult = new AsyncTaskResult<ArrayList<HashMap<String, String>>>(myXmlParsingClass .getParsedData());
        ArrayList<HashMap<String, String>> result=myresult.getResult();
        myresult.setResult(result.subList(0,maxItems));//maxItems=30 set it while creating async task object
        return myresult ;

    }