首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >j's异步ProgressBar冻结

j's异步ProgressBar冻结
EN

Stack Overflow用户
提问于 2012-12-24 23:44:15
回答 1查看 228关注 0票数 1

我正在使用j的异步库进行post/get (用我自己的库尝试,同样的事情发生了),我总是遇到一个问题。当我调用我的getNews()方法时,我的ProgressBar (不确定的)一直冻结。我在这里做错了什么?

代码语言:javascript
复制
protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();

public void getNews() throws JSONException
{

    VestiHTTPClient.get("json.php", null, new JsonHttpResponseHandler()
    {

        @Override
        public void onSuccess(JSONArray news)
        {
            try
            {
                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);                       
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
         public void onFinish() {
            Intent main = new Intent(SplashScreen.this, MainWindow.class);
            SplashScreen.this.startActivity(main);
            SplashScreen.this.finish();
         }
    });

}

一切都能正确触发(onSuccess、onFinish),但我的ProgressBar一直冻结(甚至尝试在单独的线程中运行它...)。

有人有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-25 20:37:47

终于让它工作了!我使用AndroidQuery (ListView示例)来加载和缓存图像(效果非常好)。

首先在活动A(我的SplashScreen活动)中创建静态变量

代码语言:javascript
复制
protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
protected static ArrayList<JSONObject> items = new ArrayList<JSONObject>();

然后使用AsyncTask获取所需的所有JSON信息,并填充ArrayLists和onPostExecute作为其他活动(在我的示例中为MainWindow)。

代码语言:javascript
复制
    @Override
    protected Integer doInBackground(String[]... params) {



        try
        {
            String address = "http://your.file.here";

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(address);

            try
            {

                HttpResponse response = client.execute(post);
                Log.w("RESPONSE", response.toString());

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;

                while ((sResponse = reader.readLine()) != null) {
                    result = result.append(sResponse);
                }

                reader.close();

                JSONArray news = new JSONArray(result.toString());
                int count = news.length();

                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    joNews.add(jo);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 1;
    }

     protected void onPostExecute(Integer result) {
         Intent main = new Intent(SplashScreen.this, MainWindow.class);
         SplashScreen.this.startActivity(main);
         SplashScreen.this.finish();
     }

最后,只需初始化活动B(我的MainWindow活动)中的变量,并使用AndroidQuery在ListView中加载它们!

代码语言:javascript
复制
private ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
private ArrayList<JSONObject> items = new ArrayList<JSONObject>();

    allNews = SplashScreen.allNews;
    items = SplashScreen.joNews;

    ArrayAdapter<JSONObject> aa = new ArrayAdapter<JSONObject>(this, R.layout.main_news_items, items){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

                if(convertView == null){
                        convertView = getLayoutInflater().inflate(R.layout.main_news_items, null);
                }

                JSONObject jo = getItem(position);

                AQuery aq = new AQuery(convertView);
                aq.id(R.id.name).text(jo.optString("TOPIC_TITLE", "No Title"));
                aq.id(R.id.title).text(jo.optString("CATEGORY", ""));

                String tb = jo.optString("IMAGE");
                if(tb.equals(""))
                    tb = "http://default.image.here";
                aq.id(R.id.profile_img).progress(R.id.progress).image(tb, true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);


                return convertView;

        }
    };

    ListView news = (ListView) findViewById(R.id.listView1);
    news.setAdapter(aa);
    news.setTextFilterEnabled(true);

希望这对某些人有帮助!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14023268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档