【问题标题】:ListView with jsoup and 2 textviews in a row of the listviewListView 在列表视图的一行中带有 jsoup 和 2 个文本视图
【发布时间】:2014-02-10 13:04:24
【问题描述】:

想知道如何编写一个列表视图每行有 2 个文本视图的列表视图。

我的行将具有这样的 xml 布局:

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>

我目前的代码是:

public class PlayerActivity extends Activity {

public Elements name;
public Elements num;
public ArrayList<String> statName = new ArrayList<String>();
public ArrayList<String> statNum = new ArrayList<String>();
private ArrayAdapter<String> nameAdapter;
private ArrayAdapter<String> numAdapter;
private ListView lv;
String url = "http://www.futhead.com/14/players/141/andrea-pirlo/";

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

一开始我使用的是普通的 ArrayAdapter,直到我意识到它只支持一个 textview,所以我想知道如何实现一个可以支持两个 textview 的 Array 适配器。

【问题讨论】:

  • 你应该使用自定义适配器。

标签: android listview android-listview jsoup android-arrayadapter


【解决方案1】:

要实现这一点,您必须构建一个自定义适配器并扩展您的自定义行布局。使用ArrayAdapter 不起作用,因为

默认情况下,此类期望提供的资源 id 引用 单个文本视图。如果要使用更复杂的布局,请使用 也采用字段 id 的构造函数。该字段ID应该 在较大的布局资源中引用 TextView。

去这个how-to-make-a-custom-arrayadapter-for-listview,我会推荐你​​去谷歌搜索一些更好的替代方案来实现其他Adapters

【讨论】:

    【解决方案2】:

    您必须为您的 ListView 定义一个自定义适配器(它将扩展适配器类以实现“超级”实现)。您可以指定布局并根据您的要求分配该自定义布局的每个组件以显示数据。

    适配器扩展:http://developer.android.com/reference/android/widget/Adapter.html

    另外,参考这些:

    http://www.vogella.com/tutorials/AndroidListView/article.html
    http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html
    Displaying ArrayList items in ListView contains 2 textviews

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多