【问题标题】:show tweets in a listview in twitter4j在 twitter4j 的列表视图中显示推文
【发布时间】:2013-08-20 22:58:08
【问题描述】:

我一直在玩 twitter4j 和 Android,到目前为止一切都很好。但是,我在弄清楚如何在列表视图上正确显示推文时遇到了问题。到目前为止,这是我的代码,基于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,因为它基本上会在屏幕上打印所有信息:

好吧,并不是所有的信息都是有用的或永远需要的,但它就在那里。而且我不知道如何在 listView 中仅显示 Twitter 句柄和推文,例如(即正确地从列表 rawStatuses 中提取该信息)。我考虑过拥有一个包含最有用信息的列表(例如: List &lt;User, Tweet, ID, Time&gt; ),但在我看来这很麻烦而且是一个非常糟糕的解决方案。

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

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

【问题讨论】:

    标签: android twitter twitter4j


    【解决方案1】:

    我遇到了同样的问题,而您实际上是在正确的道路上。我为解决这个问题所做的也是创建一个字符串列表并将其存储到一个 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);
        }
       });
      }
    

    【讨论】:

    • 我还应该指出,我使用自己的布局而不是默认的布局来映射我的 ListView,而不是使用默认的 layout_list_item_1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多