ListView适配器问题

时间:2014-02-09 22:14:48

标签: android listview jsoup

当我注释掉“nameAdapter = ...”或“numAdapter = ...”的代码并保留另一个未注释的代码时,它会在listview中显示数据。因此,它可以单独显示两者,但是,当我将两者都取消注释时,它将仅显示numAdapter所涉及的数据。可能是一个简单的错误,但我无法弄清楚我哪里出错了。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);

    lv = (ListView) findViewById(R.id.listView);

    new Logo().execute();
    new statistics().execute();
    nameAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_name,statName);
    numAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_number,statNum);

}

private class statistics extends AsyncTask<String, Void, String> {
    //final ListView listview = (ListView) findViewById(R.id.listView);
    /*String[] values = new String[]{ "Ball Control", "Crossing", "Curve",
            "Dribbling", "Finishing", "Free Kick Accuracy", "Heading Accuracy", "Long Passing",
            "Long Shots", "Marking", "Penalties", "Short Passing", "Shot Power", "Sliding Tackle",
            "Standing Tackle", "Volleys"};*/
    //String[] stats = new String[16];
    @Override
    protected String doInBackground(String... arg) {
        Document document;
        try {
            document = Jsoup.connect(url).get();
            num = document.select("div#stats-base p");
            statNum.clear();
            for (Element element : num) {
                statNum.add(element.ownText());
            }
            name = document.select("div#stats-base span");
            statName.clear();
            for (Element element : name) {
                statName.add(element.ownText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    @Override
    protected void onPostExecute(String result) {

        lv.setAdapter(nameAdapter);
        lv.setAdapter(numAdapter);
    }
}

player.xml:

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/playerPic"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_below="@+id/playerPic"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="104dp"/>
</RelativeLayout>

simple_list_item_1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/stat_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/stat_number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:layout_below="@id/stat_name"/>


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为这种行为并不神秘。您设置为lv的最后一个适配器是幸存的。

如果你要找的是同时添加statNamestatNum,我会设置其中一个,然后在另一个上使用.addAll(...)。例如:

lv = lv.setAdapter(nameAdapter);
lv.addAll(statNum);
nameAdapter.notifyDataSetChanged();

不要忘记让ListView呈现新数据的最后一行。