Java 如何在Android中显示两列ListView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2432951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:35:25  来源:igfitidea点击:

How to display a two column ListView in Android?

javaandroidandroid-widget

提问by Sosi

I have an android application that shows a grid view that shows:

我有一个显示网格视图的 android 应用程序,其中显示:

1

1

2

2

3

3

4

4

GridView gridview=(GridView)findViewById(R.id.GridView_test);
DataBaseHelper dbhelper=new DataBaseHelper(this);
ArrayList<String> test=new ArrayList<String>(5);
backlinksadapter.add("1");
backlinksadapter.add("2");
backlinksadapter.add("3");
backlinksadapter.add("4");
ArrayAdapter mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test);
gridview.setAdapter(mAdapter);

By the moment is working, but i would like to show foreach line of the grid, 2 columns with the values of a 2 dimensional array (something like the GridView in ASP.Net - as datasource -).

目前正在工作,但我想显示网格的每行,2 列,其中包含二维数组的值(类似于 ASP.Net 中的 GridView - 作为数据源 -)。

I would like to show:

我想展示:

1 | Person 1

1 | 人 1

2 | Person 2

2 | 人 2

3 | Person 3

3 | 人 3

4 | Person 4

4 | 人 4

Any idea?

任何的想法?

采纳答案by Steven F. Le Brun

Does it have to be a grid view? Will a ListView work?

它必须是网格视图吗?ListView 会工作吗?

I wrote a ListView and ListActivity that displays two items in each row. I started with the SDK supplied simple_list_item_2.xml layout that lists two items per row but places one on top of the other (two lines) with the second line using a smaller font. What I wanted was both items on the same line, one to the right and one to the left.

我写了一个 ListView 和 ListActivity ,每行显示两个项目。我从 SDK 提供的 simple_list_item_2.xml 布局开始,该布局每行列出两个项目,但将一个项目放在另一个(两行)之上,第二行使用较小的字体。我想要的是同一行的两个项目,一个在右边,一个在左边。

First I copied the simple_list_item_2.xml into my project's res/layout directory under a new name and changed the property android:mode="twoLine" to "oneLine" while still keeping the view element name as "TwoLineListItem". I then replaced the two inner elements with ones that did what I wanted.

首先,我以新名称将 simple_list_item_2.xml 复制到我项目的 res/layout 目录中,并将属性 android:mode="twoLine" 更改为 "oneLine",同时仍将视图元素名称保留为 "TwoLineListItem"。然后,我用满足我要求的元素替换了两个内部元素。

In the code to initialize the list, I created a MatrixCursor and filled it with the desired data. To support two items, each row in the MatrixCursor requires three columns, one being a primary key, "_id", and the other two columns being the items that I wanted to be displayed. I was then able to use a SimpleCursorAdapter to fill in and control the ListView.

在初始化列表的代码中,我创建了一个 MatrixCursor 并用所需的数据填充它。为了支持两个项目,MatrixCursor 中的每一行都需要三列,一列是主键“_id”,另外两列是我想要显示的项目。然后我就可以使用 SimpleCursorAdapter 来填充和控制 ListView。

My Layout XML File:

我的布局 XML 文件:

 <?xml version="1.0" encoding="utf-8"?>
  <TwoLineListItem
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="2dip"
     android:paddingBottom="2dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:minHeight="?android:attr/listPreferredItemHeight"
     android:mode="oneLine"
  >
<TextView
    android:id="@android:id/text1"
    android:gravity="left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dip"
    android:layout_marginTop="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
    android:id="@android:id/text2"
    android:gravity="right"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dip"
    android:layout_marginTop="6dip"
    android:layout_marginRight="6dip"
    android:layout_toRightOf="@android:id/text1"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/MainFontColor"
/>
</TwoLineListItem>

Note that I used "left" and "right" android:gravity values to make the left side item left justified and the right side item right justified. You will need different gravity values for your layout plus you will need properties to control the size of the left side item that I did not need.

请注意,我使用“左”和“右” android:gravity 值使左侧项目左对齐,右侧项目右对齐。您的布局需要不同的重力值,此外您还需要一些属性来控制我不需要的左侧项目的大小。

The method in my ListActivity class that initialized the ListView:

我的 ListActivity 类中初始化 ListView 的方法:

private void initListView()
{
    final String   AuthorName    = "Author: ";
    final String   CopyrightName = "CopyRight: ";
    final String   PriceName     = "Price: ";

    final String[] matrix  = { "_id", "name", "value" };
    final String[] columns = { "name", "value" };
    final int[]    layouts = { android.R.id.text1, android.R.id.text2 };

    MatrixCursor  cursor = new MatrixCursor(matrix);

    DecimalFormat formatter = new DecimalFormat("##,##0.00");

    cursor.addRow(new Object[] { key++, AuthorName, mAuthor });
    cursor.addRow(new Object[] { key++, CopyrightName, mCopyright });
    cursor.addRow(new Object[] { key++, PriceName,
            "$" + formatter.format(mPrice) });

    SimpleCursorAdapter data =
        new SimpleCursorAdapter(this,
                R.layout.viewlist_two_items,
                cursor,
                columns,
                layouts);

    setListAdapter( data );

}   // end of initListView()

The parameter to the MatrixCursor constructor is an array of strings that define the order and the names of the columns in the cursor. Important: Make sure to add an "_id" column, without it the MatrixColumn will throw an exception and fail to work!

MatrixCursor 构造函数的参数是一个字符串数组,用于定义游标中列的顺序和名称。重要提示:确保添加一个“_id”列,如果没有它,MatrixColumn 将抛出异常并无法工作!

The three variables, mAuthor, mCopyright and mPrice, are three data members in my ListAdaptor class and they are initialized elsewhere. In my actual code, mAuthor is actually built in this method from a list of author names. The author names are concatenated into a single string using "\n" as separators between the names. This causes the multiple author names to appear on different lines in the same TextView.

三个变量 mAuthor、mCopyright 和 mPrice 是我的 ListAdaptor 类中的三个数据成员,它们在其他地方被初始化。在我的实际代码中,mAuthor 实际上是根据作者姓名列表在此方法中构建的。作者姓名使用“\n”作为姓名之间的分隔符连接成一个字符串。这会导致多个作者姓名出现在同一个 TextView 的不同行上。

The SimpleCursorAdapter ctor parameters are the ID of the View to use for each List row, the cursor containing the data, an array of strings where each element is the name of the column from the cursor (in the order to get them) and a corresponding array of View IDs for the View to use for each item in the List row.

SimpleCursorAdapter ctor 参数是用于每个 List 行的视图 ID、包含数据的游标、一个字符串数组,其中每个元素都是游标中列的名称(按获取它们的顺序)和相应的用于列表行中每个项目的视图的视图 ID 数组。

回答by JRL

If you can't find an existing adapter that has those properties, you can create your own custom adapter and have it map to your view.

如果找不到具有这些属性的现有适配器,则可以创建自己的自定义适配器并将其映射到您的视图。

The Hello, GridViewtutorial shows how to do this.

你好,GridView控件教程演示如何做到这一点。

回答by stfn

JRL is right, you should write your own custom adapter which will let you control what will be displayed in each row. You might find thishelpful. A more advanced example can be found here http://developerlife.com/tutorials/?p=327.

JRL 是对的,您应该编写自己的自定义适配器,让您控制每行中显示的内容。您可能会发现很有帮助。一个更高级的例子可以在这里找到http://developerlife.com/tutorials/?p=327

If you don't want/have to use a list a TableLayout could display your data as well. But that would be the manual approach since you'd have to fill each cell.

如果您不想/必须使用列表,则 TableLayout 也可以显示您的数据。但这将是手动方法,因为您必须填充每个单元格。

Best of success.

最好的成功。

回答by Parallel Universe

Why not create a class, whose toString()returns str(id) + " | " + personName, and use it as a template class instead of the Stringobject?

为什么不创建一个toString()返回的类,并将str(id) + " | " + personName其用作模板类而不是String对象呢?

回答by Shoeb Ahmed Siddique

You can simply add this in your XML android:numColumns="2"

您可以简单地将其添加到您的 XML 中 android:numColumns="2"