0

我创建了 ListView ,并且我能够静态填写列表(它工作得很好),但我无法动态填写,.. 在加载列表后使用提要推文之后什么都没有出现,这里是代码:

list = new ArrayList<String>();
        arrayAdapter =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
                listtwitt.setAdapter(arrayAdapter); 
                TwitAsyncTask twitAsync = new TwitAsyncTask();
                twitAsync.execute();
    }
    @SuppressLint("NewApi")
    private class TwitAsyncTask extends AsyncTask<Void, Void, Void>{    
        private ProgressDialog progressDialo;
        @Override
        protected void onPreExecute() {
             progressDialo = ProgressDialog.show(SocialNetwork.this, "", "En cours...");
            super.onPreExecute();}

        @Override
        protected Void doInBackground(Void... params) {
             final Query query = new Query();
                query.setQuery("%23morocco");
             QueryResult result;
            try {
                result = twitter.search(query);
                 for (twitter4j.Status status : result.getTweets()) {
                            String stat = "@" + status.getUser().getScreenName() + ":" + status.getText() +":"+status.getGeoLocation() +":"+status.getCreatedAt();
                            list.add("tweet");
                             Log.i(""," tweeeeeeet"+stat);
                             Log.i("","list doInBack"+list.isEmpty());

                     }
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                        return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialo.dismiss();
            arrayAdapter.clear();
            arrayAdapter.addAll(SocialNetwork.list);
            arrayAdapter.notifyDataSetChanged();
        }

任何帮助:)

4

1 回答 1

0

刚刚在 StackOverflow 上编码:

new TwitAsyncTask(this, tweetListener).execute();

 private OnTweetsLoadedListener tweetListener = new OnTweetsLoadedListener {
     @Override
     public void onTweetsLoaded(List<String> tweets) {
             arrayAdapter =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tweets);
             listview.setAdapter(arrayAdapter);      
     }
 }     

private static class TwitAsyncTask extends AsyncTask<Void, Void, List<String>>{    

    private final Context context;
    private final OnTweetsLoadedListener listener;
    private ProgressDialog progressDialo;

    public interface OnTweetsLoadedListener() {
         void onTweetsLoaded(List<String> tweets);
    }

    public TwitAsyncTask(Context context, OnTweetsLoadedListener listener) {
        this.context = context;
        this.listener = listener;
    } 

    @Override
    protected void onPreExecute() {
         super.onPreExecute();
         progressDialo = ProgressDialog.show(context, "", "En cours...");
    }

    @Override
    protected Void doInBackground(Void... params) {
         List<String> tweets = new ArrayList<String>();
        try {
            Query query = new Query();
            query.setQuery("%23morocco");
            QueryResult result = twitter.search(query);
            for (twitter4j.Status status : result.getTweets()) {
                 String stat = "@" + status.getUser().getScreenName() + ":" + status.getText() +":"+status.getGeoLocation() +":"+status.getCreatedAt();
                 tweets.add(stat);
                  Log.i("TAG"," tweeeeeeet"+stat);
            }
        } catch (TwitterException e) {
            Log.e("TAG", "ASyncFail", e);
        }
        Log.i("TAG","done");
        return tweets;
    }

    @Override
    protected void onPostExecute(List<String> tweets) {
        super.onPostExecute(result);
        progressDialo.dismiss();
        if(listener != null) {
             listener.onTweetsLoaded(tweets);
        }
    }

如果您对此代码有疑问,则意味着您错误地使用了 Twitter4J 库

于 2013-08-03T00:00:31.940 回答