0

我一直在玩 twitter4j 和 Android,到目前为止一切都很好。但是,我在弄清楚如何在列表视图上正确显示推文时遇到了问题。到目前为止,这是我的代码,基于Twitter4j 网站上给出的代码示例

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 <User, Tweet, ID, Time>,但在我看来这很麻烦而且解决方案很差。

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

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

4

1 回答 1

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);
    }
   });
  }
于 2013-09-20T17:07:34.110 回答