About Adding Tweets to ListActivity Using Twitter 4J

Asked 2 years ago, Updated 2 years ago, 61 views

About implementing UserStream using Twitter 4J

I was able to display the tweet I got from UserStream in the Log using the one you told me about in this question, but I don't know how to add the tweet to the list.
Create a Twitter client as before
I was doing it using the method shown in , but I am having trouble with this because the API has been cut off even though I got it from UserStream.How can I retrieve tweets and add them to the ListActivity list in real time?

android twitter twitter4j

2022-09-30 17:06

1 Answers

I made a sample to display the tweets I got from UserStream in ListView.

Add ListView to the Activity layout for which you want to view streams.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="com.xsota.twitter4juserstreamtest.MainActivity">
<ListView
    android: id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

Then use Handler in StatusListener to reflect updates to ListView.

listView=(ListView) findViewById (R.id.list);
  adapter = new ArrayAdapter <>(this, android.R.layout.simple_list_item_1);
  listView.setAdapter(adapter);

StatusListener listener = new StatusListener() {
      @ Override
      public void onStatus (final Status status) {
        newHandler(Looper.getMainLooper()) .post(newRunable(){
          @ Override
          public void run() {
            adapter.add("UserName:"+status.getUser().getName()+"Tweet:"+status.getText());
          }
        });
      }

The sample source code has been published, so please refer to it.

https://github.com/numa08/Twitter4jUserStreamTest


2022-09-30 17:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.