android:如何使用基本适配器在顶部的“列表”视图中添加按钮

问题描述

|| 在我的应用程序中,我想将按钮添加到顶部的“列表视图”中。在这种情况下,它在每个列表项而不是顶部显示按钮。如何仅在顶部显示 性别.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=\"fill_parent\"
  android:orientation=\"Vertical\"

 >

<Button
        android:id=\"@+id/b1
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\" />


<LinearLayout
  android:layout_width=\"fill_parent\"
  android:layout_height=\"fill_parent\"
  android:orientation=\"horizontal\"

 >

     <TextView android:id=\"@+id/text\"
        android:layout_gravity=\"center_vertical\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1.0\"
        android:layout_height=\"wrap_content\" />


        <CheckBox android:id=\"@+id/checkBox\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"

        />     

</LinearLayout>
</LinearLayout>
Egender.java
package com.Elgifto;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class Egender extends ListActivity {

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);           
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.listadapter#getCount()
         */
        public int getCount() {
            return GENDER.length;
        }

        /**
         * Since the data comes from an array,just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure,we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.listadapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.listadapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.listadapter#getView(int,android.view.View,*      android.view.ViewGroup)
         */
        public View getView(int position,View convertView,ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null,we can reuse it directly,there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.gender,null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.cb=(CheckBox) convertView.findViewById(R.id.checkBox);


                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(GENDER[position]);
            //holder.text.setTextColor(Color.BLACK);
            if(holder.cb.isChecked())
            {
                holder.cb.setChecked(false);
                String st=GENDER[position];
                System.out.println(\"String:\"+st);
            }

                 return convertView;
        }

        static class ViewHolder {

            TextView text;
            CheckBox cb;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setlistadapter(new EfficientAdapter(this));
    }

      private static final String[] GENDER = new String[] {
       \"Male\",\"Female\"
    };
}
谢谢     

解决方法

除非我弄错了,否则您将使用sex.xml作为ListView中每个行/项目的布局。因此,您可能希望从性别.xml布局中删除Button声明。 如果希望将Button固定在ListView上方,那么假设您具有这样的xml布局,则可能要编辑声明ListView的布局以也声明Button。 例如,带有ListView的xml布局可能看起来类似于以下内容。
<LinearLayout orientation=\"horizontal\">
    <Button 
        id=\"my_button\"
        width=\"wrap_content\"
        height=\"wrap_content\"
        layout_gravity=\"center\"
        text=\"my button\" />
    <ListView 
        id=\"my_listview\"
        width=\"fill_parent\"
        height=\"0dip\"
        weight=\"1\" />
</LinearLayout>
(注意:此xml布局不完整,缺少诸如xmlns声明之类的片段。有关更完整的示例,请查看“滚动按钮上方的文本,固定在底部的按钮。”)     ,您还可以将标题添加到列表视图:
Button btn=new Button(this);
list.addHeaderView(btn);
这将成为您列表视图的第一项 您也可以通过以下方式使其无法选择
list.addHeaderView(vbtn,null,false);
并发送了一个onItemClickListener。