在列表视图中分隔列

发布于 2024-11-19 10:06:08 字数 3136 浏览 3 评论 0原文

我试图分隔列表视图中出现的不同数据列。适配器的代码如下:

public class SQLiteDemo extends ListActivity {

    private SQLiteDatabase database;

    private CursorAdapter dataSource;


    private static final String fields[] = { "firstname", "lastname", "resourceid", BaseColumns._ID };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        DatabaseHelper helper = new DatabaseHelper(this);
        database = helper.getWritableDatabase();
        Cursor data = database.query("cards", fields, null, null, null, null,
                null);

        dataSource = new SimpleCursorAdapter(this, R.layout.second, data, fields,
                new int[] { R.id.firstname, R.id.secondname, R.id.name });

        ListView view = getListView();
        view.setHeaderDividersEnabled(true);
        view.addHeaderView(getLayoutInflater().inflate(R.layout.second, null));

        setListAdapter(dataSource);

我在布局文件中定义了列表视图(我需要为充气器视图执行此操作吗?)

我尝试按如下方式添加分隔线......

   <ListView android:id="@android:id/list" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" 
             android:visibility="visible"
             android:divider = "@android:color/transparent"
             android:dividerHeight ="10.sp" />

似乎没有任何区别添加分隔项后;我做错了什么?

需要明确的是,我是一个彻头彻尾的 Android 菜鸟,并不是在寻找完整的答案(尽管它们有时可能很有用),只是朝着正确的方向轻推就足够了。谢谢你们!

使用 R.layout.second 进行编辑

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                android:layout_height="wrap_content" 
                android:id="@+id/rowLayout" 
                android:orientation="horizontal" 
                android:layout_width="fill_parent">
  <TextView     android:layout_width="wrap_content"
                android:layout_alignParentTop = "true"
                android:layout_alignParentLeft="true" 
                android:layout_height="wrap_content" 
                android:id="@+id/firstname" 
                android:text="FirstName"/> 
  <TextView     android:layout_width="wrap_content" 
                android:layout_toRightOf = "@+id/firstname"
                android:layout_height="wrap_content" 
                android:id="@+id/secondname" 
                android:text="SecondName" 
                />
  <TextView     android:layout_width="wrap_content" 
                android:layout_toRightOf = "@id/secondname" 
                android:layout_height="wrap_content" 
                android:id="@+id/name" 
                android:text="ResourceID" 
                />
   <ListView android:id="@android:id/list" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" 
             android:visibility="visible"
             android:dividerHeight ="10.sp" />

  </RelativeLayout>

现在我已经删除了分隔线透明度,因为我知道它的作用。

I am trying to separate different columns of data that appear in my listview. The code for the adapter is as follows:

public class SQLiteDemo extends ListActivity {

    private SQLiteDatabase database;

    private CursorAdapter dataSource;


    private static final String fields[] = { "firstname", "lastname", "resourceid", BaseColumns._ID };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        DatabaseHelper helper = new DatabaseHelper(this);
        database = helper.getWritableDatabase();
        Cursor data = database.query("cards", fields, null, null, null, null,
                null);

        dataSource = new SimpleCursorAdapter(this, R.layout.second, data, fields,
                new int[] { R.id.firstname, R.id.secondname, R.id.name });

        ListView view = getListView();
        view.setHeaderDividersEnabled(true);
        view.addHeaderView(getLayoutInflater().inflate(R.layout.second, null));

        setListAdapter(dataSource);

I defined the listview in my layout file (do I need to do this for the inflater view?)

I have tried adding the dividers as follows....

   <ListView android:id="@android:id/list" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" 
             android:visibility="visible"
             android:divider = "@android:color/transparent"
             android:dividerHeight ="10.sp" />

There does not seem to be any difference after adding the divider items; what am I doing wrong?

To be clear, I am a complete android noob, not looking for a complete answer (although they can be useful sometimes) just a nudge in the right direction would be more than enough. Thanks guys!

EDIT WITH R.layout.second

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                android:layout_height="wrap_content" 
                android:id="@+id/rowLayout" 
                android:orientation="horizontal" 
                android:layout_width="fill_parent">
  <TextView     android:layout_width="wrap_content"
                android:layout_alignParentTop = "true"
                android:layout_alignParentLeft="true" 
                android:layout_height="wrap_content" 
                android:id="@+id/firstname" 
                android:text="FirstName"/> 
  <TextView     android:layout_width="wrap_content" 
                android:layout_toRightOf = "@+id/firstname"
                android:layout_height="wrap_content" 
                android:id="@+id/secondname" 
                android:text="SecondName" 
                />
  <TextView     android:layout_width="wrap_content" 
                android:layout_toRightOf = "@id/secondname" 
                android:layout_height="wrap_content" 
                android:id="@+id/name" 
                android:text="ResourceID" 
                />
   <ListView android:id="@android:id/list" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" 
             android:visibility="visible"
             android:dividerHeight ="10.sp" />

  </RelativeLayout>

I have removed the divider transparency now that I know what it does.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-11-26 10:06:08

你有误会。您将相同的布局(R.layout.second)用于两个不同的事物。
您需要一个包含 ListView 的整个屏幕布局(即 R.layout.second)。

setContentView(R.layout.second)

然后您使用其他布局(即 mycell.xml)来定义该 ListView 中单行的外观。

new SimpleCursorAdapter(this, R.layout.mycell, data, fields, new int[] { R.id.firstname, R.id.secondname, R.id.name });

分隔线是为数据库中的每水平绘制的。你让它看不见了。

如果您想在名字、姓氏等(每列)之间有分隔线,则必须编辑用于行的布局(R.layout.mycell)。

You have a misunderstanding. You are using the same Layout (R.layout.second) for two different things.
You need one Layout for the whole screen that contains a ListView (i.e. R.layout.second).

setContentView(R.layout.second)

And then you use an other Layout (i.e. mycell.xml) to define the looks of a sigle row in that ListView.

new SimpleCursorAdapter(this, R.layout.mycell, data, fields, new int[] { R.id.firstname, R.id.secondname, R.id.name });

The divider is drawn horizontally for each row in your database. You made it invisible.

If you want to have dividers between firstname,lastname , etc (for each column) you have to edit the Layout you are using for the rows (R.layout.mycell).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文