清单:Fragment中的ArrayAdapter

时间:2014-12-23 13:45:34

标签: java android android-fragments adapter android-arrayadapter

我有这个代码(我使用了这个主题implement AsyncTask in Fragment android中的一些代码),它在片段中加载了twitter feed(twitter4j),但是列表是空的只有一个blanc片段(*数据已加载LOG:状态计数:20个Feed *),

我的代码:

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) {
    }
}
}

我的日志

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

我的情况图片:

enter image description here

这是Activity中的原始代码(工作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) {
    }
}
}

我无法理解为什么代码不起作用..请帮助我坚持3天。

编辑:我的片段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>

1 个答案:

答案 0 :(得分:1)

RelativeLayout与LinearLayout不同。 RelativeLayout的孩子不能使用weight属性,因此它的高度(当前为0dp)不会自动展开。

使用此:

<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>

或者将RelativeLayout更改为LinearLayout。