如何将List文本放在ListActivity中的一行上

时间:2011-08-15 19:27:14

标签: android

我正在使用此代码检索一组游戏标题平台和发布日期:

public class HtmlparserExampleActivity extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<String> gameList = new ArrayList<String>();

        Document doc = null;
        try {
            doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Get all td's that are a child of a row - each game has 4 of these
        Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
        // Iterator over those elements     
        ListIterator<Element> postIt = games.listIterator();          
        while (postIt.hasNext()) {     
            // Add the game text to the ArrayList     
            gameList.add(postIt.next().text());     
            Log.v(TAG, games.text());
        }         

        String[] items = new String[gameList.size()];
        gameList.toArray(items);
        this.setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items));
    }
}

它完全像这样。唯一的问题是,如果你去网页我正在检索项目。它在单独的列表行上显示游戏标题,发布日期和平台。

我如何将标题,平台发布日期和所有内容放在我检索的每个游戏中?

编辑 - ClassCast异常错误:

  

08-15 16:35:12.310:ERROR / AndroidRuntime(21589):致命异常:主要   08-15 16:35:12.310:ERROR / AndroidRuntime(21589):   java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams   无法转换为android.widget.AbsListView $ LayoutParams 08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.ListView.setupChild(ListView.java:1790)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.ListView.makeAndAddView(ListView.java:1759)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.ListView.fillDown(ListView.java:656)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.ListView.fillFromTop(ListView.java:716)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.ListView.layoutChildren(ListView.java:1609)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.widget.AbsListView.onLayout(AbsListView.java:1800)08-15   16:35:12.310:ERROR / AndroidRuntime(21589):at   android.view.View.layout(View.java:9581)

以下是我按照教程和你的答案构建的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayList<GameRelease> gameList = new ArrayList<GameRelease>();

    Document doc = null;
    try {
        doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Get all td's that are a child of a row - each game has 4 of these
    Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
    // Iterator over those elements     
    ListIterator<Element> postIt = games.listIterator();          
    while (postIt.hasNext()) {     
        // ...

        while (postIt.hasNext()) {     
            // Add the game text to the ArrayList     
            String name = postIt.next().text();
            String platform = postIt.next().text();
            String genre = postIt.next().text();
            String releaseDate = postIt.next().text();
            gameList.add(new GameRelease(name, platform, genre, releaseDate));
            Log.v(TAG, games.text());
        }

        this.setListAdapter(new GameReleaseAdapter(this, gameList));
    }         
}

private class GameReleaseAdapter extends ArrayAdapter<GameRelease> {

    private ArrayList<GameRelease> items;

    public GameReleaseAdapter(Context context,  ArrayList<GameRelease> items) {
        // TODO: make a layout for each item which you'd call (for example) itemLayout
        super(context, R.layout.item, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: return an item view styled however you want or as shown in the tutorial
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        GameRelease o = items.get(position);
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        tt.setText(o.getName());
        bt.setText(o.getReleaseDate());
        return bt;
    }
}
}

编辑 - 布局item.xml

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

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
            />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

它正在做的是将每个HTML <td>(表格单元格)作为单独的字符串而不是每个<tr>。你可能已经知道了。

你可能想要制作一个自定义适配器,它接受多个字符串(列表中的几个元素),并且有一个覆盖getView()以完全显示你想要的内容(排列好像在一个表格,或两行格式,较大的文字显示游戏名称,较小的文字说“平台:X类型:Y发布日期:Z”,两个例子。)

Here is a tutorial on making your own custom list adapter(这与您当前的ArrayAdapter<String>相反)。

让每个项目都有一个对象GameRelease(您可能想要的代替教程中的订单):

public class GameRelease {

    private String name;
    private String platform;
    private String genre;
    private String releaseDate;

    public String getName() {
        return name;
    }

    public String getPlatform() {
        return platform;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public String getGenre() {
        return genre;
    }

    public GameRelease(String name, String platform, String genre, String releaseDate) {
        this.name = name;
        this.platform = platform;
        this.genre = genre;
        this.releaseDate = releaseDate;
    }
}

然后,我会写一个看起来像这样的适配器,带上你的新GameRelease对象:

private class GameReleaseAdapter extends ArrayAdapter<GameRelease> {

    private ArrayList<GameRelease> items;

    public GameReleaseAdapter(Context context, ArrayList<Order> items) {
        // TODO: make a layout for each item which you'd call (for example) itemLayout
        super(context, R.layout.itemLayout, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: return an item view styled however you want or as shown in the tutorial
    }
}

现在,一旦你完成了所有设置,你可以代替你的while循环每个<td>只添加一个String元素,拿GameRelease并用多个<td>元素填充它(四个,在同一个命令网站给他们),例如:

ArrayList<GameRelease> gameList = new ArrayList<GameRelease>();

Document doc = null;
try {
    doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

// Get all td's that are a child of a row - each game has 4 of these
Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
// Iterator over those elements     
ListIterator<Element> postIt = games.listIterator();   

while (postIt.hasNext()) {     
    // Add the game text to the ArrayList     
    String name = postIt.next().text();
    String platform = postIt.next().text();
    String genre = postIt.next().text();
    String releaseDate = postIt.next().text();
    gameList.add(new GameRelease(name, platform, genre, releaseDate));
    Log.v(TAG, games.text());
}

this.setListAdapter(new GameReleaseAdapter(this, gameList));

请注意:由于ArrayAdapter<T>List<T>的构造函数(而不是T[]),这是ArrayList<T>的超类,您可以传入ArrayList<String> 1}}或ArrayList<GameRelease>而不是将其转换为数组。

答案 1 :(得分:0)

你可以迭代&lt; tr&gt; s,然后在循环中迭代&lt; td&gt;并沿途构建字符串,只有当你得到所有&lt; td&gt; s时才将其推出从排。