填充一个ListView W / AsyncTask的 [英] Populating a ListView w/ AsyncTask

查看:106
本文介绍了填充一个ListView W / AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是很优雅,但我想要做的是连接到一个Web服务,获取JSON,解析它,创建一个对象了吧,说对象添加到一个ArrayList,然后使用ArrayList的填充我的ListView。

This is probably not very elegant, but what I'm trying to do is connect to a web service, fetch the JSON, parse it, create an object out of it, add that object to an ArrayList and then use that ArrayList to populate my ListView.

我试图做这一切与AsyncTask的。

I'm trying to do all of this with AsyncTask.

摘要:doInBackgroud需要的URL字符串,用它来连接到Web服务。我得到的JSON数据作为一个字符串,分析它,构造一个新的对象出来的数据,并把它添加到ArrayList中。然后,在 onPostExecute 我试图用设置listadapter的 ArrayAdapter ,利用我的ArrayList

SUMMARY: doInBackgroud takes a String of a url, uses it to connect to a web service. I get the JSON data as a string, parse it, construct a new object out of the data, and add it to ArrayList. Then in onPostExecute I'm trying to set the listadapter using an ArrayAdapter that utilizes my ArrayList.

下面是我有:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONObject;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AllOffersListActivity extends ListActivity {

    private static final String CONSUMER_KEY = "bla";
    private static final String CONSUMER_SECRET = "bla";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        new CreateArrayListTask().execute("http://example.com/sample.json");




    }

    private class CreateArrayListTask extends AsyncTask<String, Void, ArrayList<Offer>> {
        private final ProgressDialog dialog = new ProgressDialog(AllOffersListActivity.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Fetching offers...");
            this.dialog.show();

        }
        @Override
        protected ArrayList<Offer> doInBackGround(String...urls) {
            ArrayList<Offer> offerList = new ArrayList<Offer>();

            for(String url: urls) {
                OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                consumer.setTokenWithSecret("", "");

                try {

                    URL url1 = new URL(url);
                    HttpURLConnection request = (HttpURLConnection) url1.openConnection();

                    // sign the request
                    consumer.sign(request);

                    // send the request
                    request.connect();


                    String JSONString = convertStreamToString(request.getInputStream());

                    JSONObject jObject = new JSONObject(JSONString);

                    JSONObject offerObject = jObject.getJSONObject("offer");

                    String titleValue = offerObject.getString("title");
                    //System.out.println(titleValue);

                    String descriptionValue = offerObject.getString("description");
                    //System.out.println(attributeValue);
                    JSONObject businessObject = offerObject.getJSONObject("business");
                    String nameValue = businessObject.getString("name");

                    Offer myOffer = new Offer(titleValue, descriptionValue, nameValue);

                    offerList.add(myOffer);

                } catch (Exception e) {
                    e.printStackTrace();

                } 
            }
            return offerList; 

        }

        @Override
        protected void onPostExecute(ArrayList<Offer> offerList) {
            if(this.dialog.isShowing())
                this.dialog.dismiss();
            setListAdapter(new ArrayAdapter<Offer>(AllOffersListActivity.this, android.R.layout.simple_list_item_1, offerList));        
        }
    }

    private String convertStreamToString(InputStream inputStream) throws IOException {
        if(inputStream != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];

            try {

                Reader reader = new BufferedReader( new InputStreamReader(inputStream, "UTF-8"));

                int n;
                while((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);

                }
            } finally {
                inputStream.close();
            }
            return writer.toString();
        } else {
            return "";
        }

    }

}

我看到两个错误。一个是我的私人异步类:AllOffersListActivity.CreateArrayListTask必须实现继承的抽象方法的AsyncTask&LT类型;字符串,无效的ArrayList&LT;优惠&GT;&GT; .doInBackground(字符串...)

其次,在我的 doInBackGround 覆盖,我收到:类型AllOffersListActivity.CreateArrayListTask的方法doInBackGround(字符串)必须覆盖或实现超类型的方法

Secondly, on my doInBackGround Override, I'm getting: The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must override or implement a supertype method

我是什么在这里失踪?

推荐答案

这只是一个小错字;应 doInBackground 而不是 doInBackGround

It's just a small typo; should be doInBackground instead of doInBackGround.

这篇关于填充一个ListView W / AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆