在twitter4j的listview中显示推文

时间:2013-08-20 22:58:08

标签: android twitter twitter4j

我一直在玩twitter4j和Android,到目前为止一直很好。但是我在查明如何在列表View上正确显示推文时遇到了问题。到目前为止,这是我的代码,基于code example given on the Twitter4j website

public class MainActivity extends Activity {
//ListView with the tweets
private ListView timelineListView; 

// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

//Adapter
ArrayAdapter<twitter4j.Status> tweetAdapter ;  

//List
List<Status> rawStatuses;

//Other stuff
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);
//Login methods, checking that there is an Internet connection, etc... When a button is
//pressed, an Asynctask is called and it retrieves the tweets:


class updateTimeline extends AsyncTask <Void, Void, Void>{

    protected void onPreExecute(Void thing){

    }

    protected Void doInBackground(Void... arg0) {           
        try {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

            // Access Token
            String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
            // Access Token Secret
            String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");

            AccessToken accessToken = new AccessToken(access_token, access_token_secret);
            Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
            User user = twitter.verifyCredentials();

            rawStatuses = twitter.getHomeTimeline();


            System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
            for (twitter4j.Status status : rawStatuses) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); 
                }

        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            //System.exit(-1);
        }
        return null;
    }




    protected void onPostExecute(Void result) {
        // dismiss the dialog after getting all products
        //pDialog.dismiss();

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
            Toast.makeText(getApplicationContext(),
                        "Timeline updated", Toast.LENGTH_SHORT)
                        .show();    
            tweetAdapter = new ArrayAdapter<twitter4j.Status>(MainActivity.this, android.R.layout.simple_list_item_1, rawStatuses);
            timelineListView.setAdapter(tweetAdapter);
            }

        });
    }

它检索最后20条推文或者很好。但我的问题来自最后一部分,我将列表rawStatuses绑定到适配器和listView,因为它基本上在屏幕上打印所有信息:

tweets

好的,并非所有这些信息都有用或者不再需要,但它确实存在。而且我不知道如何在listView中显示例如Twitter句柄和推文(例如,正确地从列表rawStatuses中提取该信息)。我想过拥有一个包含最有用信息的List(类似于: List <User, Tweet, ID, Time>),但在我看来这很麻烦,也是一个非常糟糕的解决方案。

我的问题是:如何或应该管理推文包含的所有信息(许多推文),以便我可以显示我想要的内容,还有其余的内容?我找到的最接近的答案是this answer,但给出的链接不再有效。

我希望我已经解释过了自己。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,你实际上是在正确的道路上。我解决这个问题的方法是创建一个字符串列表并将其存储到ArrayList中,然后将该列表放入和ArrayAdapter而不是原始状态:

ArrayAdapter<String> stringTweetAdapter;
List<twitter4j.Status> statuses;
List<String> stringStatuses = new ArrayList<String>();

.....


User user = twitter.verifyCredentials();
statuses = twitter.getHomeTimeline();
System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
for (twitter4j.Status status : statuses) {
 Log.d("Twitter","@" + status.getUser().getScreenName() + " - " + status.getText());
 String myTweets = ("@" + status.getUser().getScreenName() + " - " + status.getText());
                stringStatuses.add(myTweets);
 }

......

protected void onPostExecute(String file_url) {
 // dismiss the dialog after getting all products
 progressDialog.dismiss();
 // updating UI from Background Thread
 runOnUiThread(new Runnable() {
  @Override
  public void run() {
    Toast.makeText(getApplicationContext(),"Retrieving TimeLine Done..", Toast.LENGTH_SHORT).show();
    stringTweetAdapter = new ArrayAdapter<String>(ShowTimeline.this, R.layout.timeline_list_item, R.id.textTimelineItem, stringStatuses);
    twitterFeed.setAdapter(stringTweetAdapter);
    }
   });
  }