1

我想显示一些表单元素,下面我想显示一个列表视图。

我使用 CursorAdapter 方法生成列表视图。

我的布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Name:"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_marginTop="10dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp"/>

    <EditText
        android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/name"
        android:ems="10" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/startdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtName"
        android:text="Start Date:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="10dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp"/>

    <EditText
        android:id="@+id/txtStartDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/startdate"
        android:ems="10" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp"/>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/categories"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
    </TextView>

    <Button
        android:id="@+id/btnSave"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Save" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp"/>

</RelativeLayout>

生成列表的代码是:

DBAdapter db = new DBAdapter(Demo.this);
        db.open();
        final Cursor c = db.getAllCategories();
        CategoriesCursorAdapter categoriescursoradapter = new CategoriesCursorAdapter(Demo.this,c,CursorAdapter.NO_SELECTION); 
        ListView lv = getListView();
        lv.setAdapter(categoriescursoradapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                  c.moveToPosition(pos);
                  int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
                  Toast.makeText(getApplicationContext(), "rowid:"+rowId, Toast.LENGTH_LONG).show();
                }

              });  
        db.close();

分类CursorAdapter代码:

public class CategoriesCursorAdapter extends CursorAdapter {


    public CategoriesCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.activity_demo, parent, false);

        return retView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView textViewCategory = (TextView) view.findViewById(R.id.categories);
        textViewCategory.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
    }
}

当我运行应用程序时,每个表单元素都会为每个列表视图重复。

布局

相反,表单元素应生成一次,列表视图应位于该表单元素下方,保存按钮应位于页面底部。

4

0 回答 0