4

我是 Android 新手,我正在做的第一个项目是让玩家加入团队。我有一个从数据库中获取所有玩家的列表视图。每个名称旁边都应该有一个复选框,然后底部有一个按钮应该更新数据库。到目前为止,这就是我得到的。

http://dyp.im/W79JdmfIk

这是我的活动

 package com.example.sqllitetest;

 import android.app.AlertDialog;
 import android.app.ListActivity;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.widget.ResourceCursorAdapter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.*;

public class Tester extends ListActivity {

MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new PlayersDBAdapter(this);
    mDbHelper.open();
    Cursor myCur = mDbHelper.fetchAllPlayers();
    startManagingCursor(myCur);
    setContentView(R.layout.playertoteam);

   // registerForContextMenu(getListView());
   // ((Tester) context).setContentView(R.layout.playertoteam);
    //getListView().addFooterView(button);
    mListAdapter = new MyAdapter(Tester.this, myCur);
    setListAdapter((ListAdapter) mListAdapter);

    Button button = (Button) findViewById(R.id.alertBox);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });

}


private class MyAdapter extends ResourceCursorAdapter {

    public MyAdapter(Context context, Cursor cur) {
        super(context, R.layout.playertoteam, cur);
    }

    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) {
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return li.inflate(R.layout.playertoteam, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cur) {
        TextView tvListText = (TextView)view.findViewById(R.id.code);
        CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);

        tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
        cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
    }




}
}

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >

 <Button
    android:id="@+id/alertBox"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/confirm" />
  <RelativeLayout
            android:id="@+id/relativeLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="top" >
            <ListView
                android:id="@android:id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/alertBox"
                />
            <TextView
                android:id="@+id/code"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

              <CheckBox android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="false"

                />

  </RelativeLayout>

</LinearLayout>

我想知道如何从行中删除按钮以及主确认按钮下方的复选框。另外我想知道这是否是做这样的事情的最佳方法,因为,再一次,我是新手,我不知道如何搜索正确的方法。

4

2 回答 2

2

ResourceCursorAdapter 为 ListView 的每一行创建视图,这些视图在 newView() 方法中膨胀。由于这个原因,你在每一行都得到了按钮。Soln:创建一个单独的 xml,定义 ListView 的行布局,并在 newView() 方法中扩展这个新布局。并且在主布局中只有按钮和 ListView,即 playertoteam.xml

playertoteam.xml

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

<Button
    android:id="@+id/alertBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/confirm" />

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@id/alertBox" />

</RelativeLayout>

row_layout.xml

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

  <TextView
    android:id="@+id/code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true"/>

  <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false" 
    android:layout_alignParentRight="true"/>

 </RelativeLayout>

您可以在此处找到 ListView 的示例

于 2013-01-31T21:09:27.797 回答
0

您可以使用 listview 的 addFooterView 方法。

View footerView =
((LayoutInflater)
ActivityContext.
getSystemService
(Context.LAYOUT_
INFLATER_SERVICE)).inflate
(R.layout.footer_
layout, null, false);
ListView.addFooterView
(footerView);
于 2013-01-31T18:28:03.753 回答