开发者

Separating columns in listview

开发者 https://www.devze.com 2023-03-19 23:07 出处:网络
I am trying to separate different columns of data that appear in my listview. The code for the adapter is as follows:

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开发者_JS百科", 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消