0

i have this code (i used some codes from this topic implement AsyncTask in Fragment android) that loads twitter feed (twitter4j) inside a fragment but the list is empty only a blanc fragment(* the data are loaded LOG:Status Count﹕ 20 Feeds*),

my code:

public class TwitterFragment extends Fragment {

ListView i;
List<Status> statusess;
ConfigurationBuilder cb;
twitter4j.Status status3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_twitter, container, false);

        i = (ListView) rootView.findViewById(R.id.listviewtwitter);

        //new LongOperation().execute("");
        bindListview();
        return rootView;

    }

public void bindListview() {

    new LongOperation(getActivity(),i).execute("");
}
  class LongOperation extends AsyncTask<String, Void, String> {
    ArrayList<String> statusListTextOnly;
      public  LongOperation(Activity context,ListView lview) {

      }

      @Override
    protected String doInBackground(String... params) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("xxx")
                .setOAuthConsumerSecret("xxxxx")
                .setOAuthAccessToken("xxxxx")
                .setOAuthAccessTokenSecret("xxxxx");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        try {
            String user;
            user = "Almounir";
            statusess = twitter.getUserTimeline(user);
            Log.i("Status Count", statusess.size() + " Feeds");

        } catch (TwitterException te) {
            te.printStackTrace();
        }
        statusListTextOnly = new ArrayList<String>();

        for (twitter4j.Status status3 : statusess) {
            statusListTextOnly.add(status3.getText());
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        i.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, statusListTextOnly));
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}
}

My Log

12-23 12:53:53.042    2421-2421/com.example.nbalarmtab I/art﹕ Not late-enabling -    Xcheck:jni (already on)
12-23 12:53:53.106    2421-2428/com.example.nbalarmtab E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe
12-23 12:53:53.106    2421-2428/com.example.nbalarmtab I/art﹕ Debugger is no longer active
12-23 12:53:53.640    2421-2436/com.example.nbalarmtab D/OpenGLRenderer﹕ Render dirty regions requested: true
12-23 12:53:53.652    2421-2421/com.example.nbalarmtab D/﹕ HostConnection::get() New Host Connection established 0xae1f3e00, tid 2421
12-23 12:53:53.663    2421-2421/com.example.nbalarmtab D/Atlas﹕ Validating map...
12-23 12:53:53.729    2421-2433/com.example.nbalarmtab I/art﹕ Background sticky concurrent mark sweep GC freed 1991(140KB) AllocSpace objects, 1(81KB) LOS objects, 29% free, 798KB/1135KB, paused 6.505ms total 18.704ms
12-23 12:53:54.013    2421-2436/com.example.nbalarmtab D/﹕ HostConnection::get() New Host Connection established 0xae1e8390, tid 2436
12-23 12:53:54.028    2421-2436/com.example.nbalarmtab I/OpenGLRenderer﹕ Initialized EGL, version 1.4
12-23 12:53:54.040    2421-2436/com.example.nbalarmtab D/OpenGLRenderer﹕ Enabling debug mode 0
12-23 12:53:54.057    2421-2436/com.example.nbalarmtab W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-23 12:53:54.057    2421-2436/com.example.nbalarmtab W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa69184e0, error=EGL_SUCCESS
12-23 12:53:55.528    2421-2437/com.example.nbalarmtab I/Status Count﹕ 20 Feeds

a picture of my situation:

enter image description here

this is the original code in Activity (work 100%):

public class MainActivity extends Activity {

ListView i;
List<Status> statusess;
ConfigurationBuilder cb;
twitter4j.Status status3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    i = (ListView) findViewById(R.id.listview1);
    new LongOperation().execute("");

}

private class LongOperation extends AsyncTask<String, Void, String> {
    ArrayList<String> statusListTextOnly;
    @Override
    protected String doInBackground(String... params) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("nnnnnn")
                .setOAuthConsumerSecret("bbbbbb")
                .setOAuthAccessToken("hhhhhhh")
                .setOAuthAccessTokenSecret("hhhhhh");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        try {
             String user;
            user = "xxxxx";
            statusess = twitter.getUserTimeline(user);
            Log.i("Status Count", statusess.size() + " Feeds");

        } catch (TwitterException te) {
            te.printStackTrace();
        }
        statusListTextOnly = new ArrayList<String>();

        for (twitter4j.Status status3 : statusess) {
            statusListTextOnly.add(status3.getText());
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        i.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, statusListTextOnly));
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}
}

i cant understand why the code isn't working.. please help im stuck for 3 days with this.

Edit: My fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/listviewtwitter"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"/>
 </RelativeLayout>
5
  • try calling the bindListview() from fragment's onResume() Commented Dec 24, 2014 at 22:57
  • @injecteer still same result what i did: ` public void onResume() { bindListview(); super.onResume(); } private void bindListview() { new LongOperation(getActivity(),i).execute(""); }` Commented Dec 25, 2014 at 8:07
  • Just out of curiosity, what does your fragment_twitter.xml look like? Commented Dec 29, 2014 at 16:23
  • @mjp66 ihave edited my question with the xml Commented Dec 29, 2014 at 16:41
  • The problem might be in your listview height and weight attributes. Could you try removing layout_weight (it's invalid within a relativelayout) and setting layout_height to "wrap_content"? Commented Dec 29, 2014 at 16:48

1 Answer 1

1

RelativeLayout is not the same as LinearLayout. RelativeLayout's children cannot use weight attribute thus it's height (currently 0dp) is not automatically expanded.

Use this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/listviewtwitter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false"/>
</RelativeLayout>

Or change RelativeLayout, to LinearLayout.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you man, this is it.. i didn't even look to the xml in a WEEK
Also, if you're sticking to RelativeLayout, remove android:orientation. That only applies to LinearLayout ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.