Java 如何在Android中显示两列列表视图?

Java 如何在Android中显示两列列表视图?,java,android,android-widget,Java,Android,Android Widget,我有一个android应用程序,它显示一个网格视图,显示: 一, 二, 三, 四, GridView GridView=(GridView)findviewbyd(R.id.GridView\u测试); DataBaseHelper dbhelper=新的DataBaseHelper(此); ArrayList测试=新的ArrayList(5); backlinksadapter.添加(“1”); backlinksadapter.add(“2”); backlinksadapter.add(“

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

一,

二,

三,

四,

GridView GridView=(GridView)findviewbyd(R.id.GridView\u测试);
DataBaseHelper dbhelper=新的DataBaseHelper(此);
ArrayList测试=新的ArrayList(5);
backlinksadapter.添加(“1”);
backlinksadapter.add(“2”);
backlinksadapter.add(“3”);
backlinksadapter.添加(“4”);
ArrayAdapter mAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,测试);
setAdapter(mAdapter);
目前正在运行,但我想在网格的每一行显示两列,其值为二维数组(类似于ASP.Net中的GridView-作为数据源-)

我想展示:

1人1人

2人2人

3人3人

4 |人4


有什么想法吗?

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


本教程介绍了如何做到这一点。

JRL是正确的,您应该编写自己的自定义适配器,它将允许您控制每行中显示的内容。你可能会觉得有用。可以找到更高级的示例

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


最好的成功。

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

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

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

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

我的布局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>
MatrixCursor构造函数的参数是一个字符串数组,用于定义游标中列的顺序和名称。重要提示:请确保添加“\u id”列,否则MatrixColumn将引发异常并无法工作

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


SimpleCursorAdapter ctor参数是要用于每个列表行的视图ID、包含数据的游标、字符串数组(其中每个元素都是来自游标的列名)(按获取它们的顺序)以及视图用于列表行中每个项目的视图ID的对应数组。

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

您只需将其添加到XML中即可
android:numColumns=“2”

如果你能发布一张展示你想做什么的图片,那就太好了……有人能解释一下Mautor,mCopyright的成员吗?它们可以是数组吗?正如上面写的“第1行\nline2\nline3行”???这些不是光标中的行-它们是列表列,对吗?我可以在这些列上使用标题吗?+1对于“_id”列例外部分,它真的让我头疼,直到我读到这个注释:)仅供参考链接是dead@Sealer_05现在它重定向到通用的
RecyclerVIew
教程。也许比让死者安息更让人困惑。
 <?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>
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()