简体   繁体   English

如何在Android中显示两列ListView?

[英]How to display a two column ListView in Android?

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 -). 目前正在工作,但我想显示网格的foreach行,2列具有2维数组的值(类似于ASP.Net中的GridView - 作为数据源 - )。

I would like to show: 我想表明:

1 | 1 | Person 1 人1

2 | 2 | Person 2 人2

3 | 3 | Person 3 人3

4 | 4 | Person 4 人4

Any idea? 任何的想法?

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. 我写了一个ListView和ListActivity,每行显示两个项目。 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. 我开始使用SDK提供的simple_list_item_2.xml布局,该布局每行列出两个项目,但是将一个放在另一个(两行)之上,第二行使用较小的字体。 What I wanted was both items on the same line, one to the right and one to the left. 我想要的是同一条线上的两个项目,一个在右边,一个在左边。

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". 首先,我使用新名称将simple_list_item_2.xml复制到项目的res / layout目录中,并将属性android:mode =“twoLine”更改为“oneLine”,同时仍将视图元素名称保留为“TwoLineListItem”。 I then replaced the two inner elements with ones that did what I wanted. 然后我用那些做了我想要的东西替换了两个内部元素。

In the code to initialize the list, I created a MatrixCursor and filled it with the desired data. 在初始化列表的代码中,我创建了一个MatrixCursor并用所需的数据填充它。 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. 为了支持两个项目,MatrixCursor中的每一行都需要三列,一列是主键“_id”,另外两列是我想要显示的项目。 I was then able to use a SimpleCursorAdapter to fill in and control the ListView. 然后,我可以使用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. 请注意,我使用“left”和“right”android:gravity值使左侧项左对齐,右侧项右对齐。 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. 您将需要不同的重力值用于布局,您将需要属性来控制我不需要的左侧项目的大小。

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. MatrixCursor构造函数的参数是一个字符串数组,用于定义游标中列的顺序和名称。 Important: Make sure to add an "_id" column, without it the MatrixColumn will throw an exception and fail to work! 重要提示:确保添加“_id”列,如果没有它,MatrixColumn将抛出异常而无法正常工作!

The three variables, mAuthor, mCopyright and mPrice, are three data members in my ListAdaptor class and they are initialized elsewhere. 三个变量,mAuthor,mCopyright和mPrice,是我的ListAdaptor类中的三个数据成员,它们在其他地方初始化。 In my actual code, mAuthor is actually built in this method from a list of author names. 在我的实际代码中,mAuthor实际上是从作者名称列表中构建的。 The author names are concatenated into a single string using "\\n" as separators between the names. 作者名称连接成一个字符串,使用“\\ n”作为名称之间的分隔符。 This causes the multiple author names to appear on different lines in the same TextView. 这会导致多个作者姓名出现在同一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行的View的ID,包含数据的游标,一个字符串数组,其中每个元素是游标中列的名称(按获取顺序排列)和相应的视图的视图ID数组,用于“列表”行中的每个项目。

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, GridView tutorial shows how to do this. Hello,GridView教程展示了如何执行此操作。

JRL is right, you should write your own custom adapter which will let you control what will be displayed in each row. JRL是对的,您应该编写自己的自定义适配器,它可以让您控制每行显示的内容。 You might find this helpful. 您可能会觉得很有帮助。 A more advanced example can be found here http://developerlife.com/tutorials/?p=327 . 可以在http://developerlife.com/tutorials/?p=327找到更高级的示例。

If you don't want/have to use a list a TableLayout could display your data as well. 如果您不想/必须使用列表,TableLayout也可以显示您的数据。 But that would be the manual approach since you'd have to fill each cell. 但这将是手动方法,因为您必须填充每个单元格。

Best of success. 最好的成功。

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

您可以在XML android中简单地添加它:numColumns =“2”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM