如何显示在Android中一个两列的ListView? [英] How to display a two column ListView in Android?

查看:1241
本文介绍了如何显示在Android中一个两列的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

1

2

3

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);

通过此刻正在工作,但我想展现的foreach线网架,两列用二维数组的值(类似GridView控件在ASP.Net - 作为数据源 - )。

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 -).

我想显示:

1 |人1

2 |人2

3 |人3

4 |人4

你知道吗?

推荐答案

是否必须是一个网格视图?将一个ListView工作?

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

我写了一个ListView和ListActivity显示每行中的两个项目。我开始与所提供的SDK simple_list_item_2.xml布局,列出两个项目每行,但在其他(两行)的榜首位之一,用较小的字体的第二行。我想要的是在同一行中两个项目,一到右边,一个在左边。

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.

首先,我复制了simple_list_item_2.xml到我的项目的RES /布局目录下创建一个新的名称和更改的属性机器人:模式=twoLine到ONELINE,同时仍保持视图元素的名称为TwoLineListItem。然后我更换了两个内部因素与那些做了什么,我想要的。

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.

在code初始化列表中,我创建了一个MatrixCursor和所需的数据填充它。为了支持两个项目,在MatrixCursor每一行需要三个列,其中之一是主键,_id,另两列是,我希望被显示的项目。我当时能够使用SimpleCursorAdapter填写和控制的ListView。

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.

我的布局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.

在我ListActivity类初始化ListView控件的方法:

The method in my ListActivity class that initialized the 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()

参数的MatrixCursor构造函数定义的顺序和光标的列名称的字符串数组。重要提示:请确保添加一个_id一栏,没有它的MatrixColumn将抛出一个异常,无法正常工作

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!

这三个变量,mAuthor,mCopyright和mPrice,都在我的ListAdaptor类三个数据成员,他们在其他地方初始化。在我的实际code,mAuthor实际上是从作者姓​​名列表,在此建方法。作者姓名连接成使用\ N的名称之间的分隔符的单一字符串。这导致多个作者姓名出现在在同一TextView的不同的行。

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.

在SimpleCursorAdapter构造函数参数的查看用于每个列表行的ID,包含数据,字符串数组,其中每个元素是从游标的列名的光标(在为了得到它们)并查看ID的相应的阵列的视图以用于列表行中的每一项。

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.

这篇关于如何显示在Android中一个两列的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆