我创建了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();
}
任何帮助:)
答案 0 :(得分: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库