Sos*_*osi 16 java android android-widget
我有一个Android应用程序,显示一个网格视图,显示:
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);
Run Code Online (Sandbox Code Playgroud)
目前正在工作,但我想显示网格的foreach行,2列具有2维数组的值(类似于ASP.Net中的GridView - 作为数据源 - ).
我想表明:
1 | 人1
2 | 人2
3 | 人3
4 | 人4
任何的想法?
小智 31
它必须是网格视图吗?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>
Run Code Online (Sandbox Code Playgroud)
请注意,我使用"left"和"right"android:gravity值使左侧项左对齐,右侧项右对齐.您将需要不同的重力值用于布局,您将需要属性来控制我不需要的左侧项目的大小.
我的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()
Run Code Online (Sandbox Code Playgroud)
MatrixCursor构造函数的参数是一个字符串数组,用于定义游标中列的顺序和名称.重要提示:确保添加"_id"列,如果没有它,MatrixColumn将抛出异常而无法正常工作!
三个变量,mAuthor,mCopyright和mPrice,是我的ListAdaptor类中的三个数据成员,它们在其他地方初始化.在我的实际代码中,mAuthor实际上是从作者名称列表中构建的.作者名称连接成一个字符串,使用"\n"作为名称之间的分隔符.这会导致多个作者姓名出现在同一TextView中的不同行上.
SimpleCursorAdapter ctor参数是用于每个List行的View的ID,包含数据的游标,一个字符串数组,其中每个元素是游标中列的名称(按获取顺序排列)和相应的视图的视图ID数组,用于"列表"行中的每个项目.
| 归档时间: |
|
| 查看次数: |
83181 次 |
| 最近记录: |