Android的微调页脚 [英] Android Spinner Footer

查看:162
本文介绍了Android的微调页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来添加微调框下拉列表内的页脚?例如,说我从数据库填充微调,但我想在列表的底部选择不管是如何安排的名单。列表可能是数据库项1,2 ..,3 ...,并在该列表的底部页脚选择添加项到数据库。

到目前为止,我已经用你的CustomSpinner类,如下所示:

 公共类CustomSpinner扩展微调实现OnItemClickListener {私人AlertDialog myDialog = NULL;
私人OnClickListener myButtonClickListener = NULL;
私人GrainSpinnerAdapter适配器= NULL;公共CustomSpinner(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共无效setButtonClickListener(OnClickListener监听器){
    myButtonClickListener =侦听器;
}公共无效setAdapter(GrainSpinnerAdapter适配器){
    this.adapter =适配器;
}@覆盖
公共布尔performClick(){
    上下文的背景下=的getContext();    //充气布局
    最终LayoutInflater充气= LayoutInflater.from(的getContext());
    最后的视图V = inflater.inflate(R.layout.my_custom_spinner,NULL);    //设置列表视图
    最终的ListView LV =(ListView控件)v.findViewById(R.id.list);
    lv.setAdapter(适配器);
    lv.setSelection(getSelectedItemPosition());
    lv.setOnItemClickListener(本);    //设置按钮
    最终按钮BTN =(按钮)v.findViewById(R.id.addButton);
    btn.setOnClickListener(myButtonClickListener);    //建立我们的对话
    AlertDialog.Builder建设者=新AlertDialog.Builder(背景);    //显示提示,就像我们的父母微调不
    如果(getPrompt()!= NULL){
        builder.setTitle(getPrompt());
    }    //创建并显示对话框
    myDialog = builder.setView(ⅴ).create();
    myDialog.show();    返回true;
}@覆盖
公共无效onItemClick(适配器视图<>查看,查看ItemView控件,INT位置,长的id){
    setSelection(位置);
    如果(myDialog!= NULL){
        myDialog.dismiss();
        myDialog = NULL;
    }}

}
我想使用一个单独的适配器如本

 公共类GrainListAdapter扩展SimpleCursorAdapter {私有静态最后弦乐DEFAULT_UNITS =美国;
私人按钮upButton;
私人语境myContext;
私人RecipeGrainActivity parentActivity;
私人按钮downButton;
私人字符串单位;
私人双getLbs;公共GrainListAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []到){
    超(背景下,布局,C,从,到);
    myContext =背景;
    parentActivity =(RecipeGrainActivity)myContext;    //公制preF检查。
    共享preferences我的preFS = preferenceManager.getDefaultShared preferences(背景);
    单位=将String.valueOf(我的prefs.getString(context.getString(R.string pref_measurement),DEFAULT_UNITS));
}@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
    INT idColumn = cursor.getColumnIndex(_ ID);
    最终诠释的getId = cursor.getInt(idColumn);
    最终双增量= 0.25;    UnitsConversions转换=新UnitsConversions();    INT名称列= cursor.getColumnIndex(名称);
    字符串的getName = cursor.getString(名称列);
    TextView的名字=(TextView中)view.findViewById(R.id.GrainName);
    name.setText(的getName);    INT originColumn = cursor.getColumnIndex(原产地);
    字符串getOrigin = cursor.getString(originColumn);
    TextView的起源=(TextView中)view.findViewById(R.id.GrainOrigin);
    origin.setText(getOrigin);    如果(units.equals(公制)){
        //设置标签指标。
        。字符串公斤= context.getResources()的getString(R.string.kilograms);
        TextView的weightLabel =(TextView中)view.findViewById(R.id.GrainLbsLabel);
        weightLabel.setText(公斤);
    }
}@覆盖
公共查看NewView的(上下文的背景下,光标指针,最终的ViewGroup父){
    查看查看= View.inflate(背景下,R.layout.grain_list_item,NULL);
    返回视图。
}

}

这让我建立一个自定义列在每个微调列表项。我想我可以用我的活动范围内customspinner.setadapter()适配器设置为CustomSpinner。然而,在CustomSpinner类中定义的ListView中所使用的类中引用的适配器。我如何通过我的适配器插入类,以便它可以使用它?

下面是我不得不自定义项添加到我的布局:

 < com.bluelightuniverse.android.brewmobile.CustomSpinner
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / GrainNameSpinner
    机器人:layout_toRightOf =@ ID / GrainOriginSpinner
    机器人:layout_toLeftOf =@ + ID / AddGrainButton>
< /com.bluelightuniverse.android.brewmobile.CustomSpinner>


解决方案

我一直在尝试与纱厂对话框使更多复杂的看法。

要你解释过什么,我做了微调的子类。我检查了 并overrided的 performClick 来基本上做你想要什么:一个自定义视图来填充自己的对话框

 包me.ribose.example; //你就必须改变这种进口android.app.AlertDialog;
进口android.content.Context;
进口android.database.DataSetObserver;
进口android.util.AttributeSet;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.Spinner;
进口android.widget.SpinnerAdapter;公共类CustomSpinner扩展微调实现OnItemClickListener {
    私人AlertDialog支持mDialog = NULL;
    私人OnClickListener mButtonClickListener = NULL;    公共UnitSelectionSpinner(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公共无效setButtonClickListener(OnClickListener监听器){
        mButtonClickListener =侦听器;
    }    @覆盖
    公共布尔performClick(){
        上下文的背景下=的getContext();        //获取一组适配器
        最后DropDownAdapter适配器=新DropDownAdapter(getAdapter());        //膨胀我们的布局
        最终LayoutInflater充气= LayoutInflater.from(的getContext());
        最后的视图V = inflater.inflate(R.layout.customSpinner,NULL);        //设置列表视图
        最终的ListView LV =(ListView控件)v.findViewById(R.id.list);
        lv.setAdapter(适配器);
        lv.setSelection(getSelectedItemPosition());
        lv.setOnItemClickListener(本);        //设置按钮
        最终按钮BTN =(按钮)v.findViewById(R.id.addButton);
        btn.setOnClickListener(mButtonClickListener);        //建立我们的对话
        AlertDialog.Builder建设者=新AlertDialog.Builder(背景);        //显示提示,就像我们的父母微调不
        如果(getPrompt()!= NULL){
            builder.setTitle(getPrompt());
        }        //创建并显示对话框
        支持mDialog = builder.setView(ⅴ).create();
        mDialog.show();        返回true;
    }    @覆盖
    公共无效onItemClick(适配器视图<>查看,
                            查看ItemView控件,INT位置,长的id){
        setSelection(位置);
        如果(支持mDialog!= NULL){
            mDialog.dismiss();
            支持mDialog = NULL;
        }
    }    / **
     *< P>包装类的适配器。变换嵌入式适配器实例
     *成ListAdapter&下; / P>
     * /
    私有静态类DropDownAdapter实现ListAdapter,SpinnerAdapter {
        私人SpinnerAdapter mAdapter;        / **
         *< P>创建指定适配器一个新的ListAddapter包装< / P>
         *
         * @参数适配器适配器转换成ListAdapter
         * /
        公共DropDownAdapter(SpinnerAdapter适配器){
            this.mAdapter =适配器;
        }        公众诠释的getCount(){
            返回mAdapter == NULL? 0:mAdapter.getCount();
        }        公共对象的getItem(INT位置){
            返回mAdapter == NULL?空:mAdapter.getItem(位置);
        }        众长getItemId(INT位置){
            返回mAdapter == NULL? -1:mAdapter.getItemId(位置);
        }        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            返回getDropDownView(位置,convertView,父母);
        }        公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup){
            返回mAdapter == NULL?空值 :
                    mAdapter.getDropDownView(位置,convertView,父母);
        }        公共布尔hasStableIds(){
            返回mAdapter = NULL&放大器;!&安培; mAdapter.hasStableIds();
        }        @覆盖
        公共无效registerDataSetObserver(DataSetObserver观察者){
            如果(mAdapter!= NULL){
                mAdapter.registerDataSetObserver(观察员);
            }
        }        @覆盖
        公共无效unregisterDataSetObserver(DataSetObserver观察者){
            如果(mAdapter!= NULL){
                mAdapter.unregisterDataSetObserver(观察员);
            }
        }        / **
         *< P>始终返回false< / P>
         *
         返回:假的
         * /
        公共布尔areAllItemsEnabled(){
            返回true;
        }        / **
         *< P>始终返回false< / P>
         *
         返回:假的
         * /
        公共布尔isEnabled(INT位置){
            返回true;
        }        公众诠释getItemViewType(INT位置){
            返回0;
        }        公众诠释getViewTypeCount(){
            返回1;
        }        公共布尔的isEmpty(){
            返回getCount将()== 0;
        }
    }
}

您需要我customSpinner.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
    < ListView控件
            机器人:ID =@ + ID /列表
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =1
            机器人:layout_marginTop =5像素
            机器人:cacheColorHint =@空
            机器人:背景=@机器人:彩色/ background_light
            机器人:分=@机器人:可绘制/ divider_horizo​​ntal_bright
            机器人:滚动条=垂直>
    < /&的ListView GT;
    <按钮
            机器人:ID =@ + ID / Add按钮
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=添加到资料库>
    < /按钮>
< / LinearLayout中>

当然,作出这样的的android:文本字符串资源

在这方面的更多的话:只要一个 CustomSpinner 在活动的布局,因为你需要它,并调用 setButtonClickListener (也许不远的地方你设置的适配器)来添加一个回调的按钮,也许会提示你需要什么用户添加或。当然, notifyDataSetChanged()添加到数据库后,您的适配器。上一个微调的工作就可以工作了与此自定义微调一模一样。

附注(修订版):你必须确保你的适配器列表中的项目有黑色文本(例如:机器人:文字颜色=@机器人:彩色/ primary_text_light_nodisable )!我改变上述所以它并不需要的物品白色背景(然后让项目将突出显示不工作)的XML。


code注:因为谁做Android的人决定不使一个简单的方法来定制微调对话框(硬codeD使用 AlertDialog.Builder 使用 setSingleChoiceItems()以及私有类转换 SpinnerAdapter 取值 ListAdapter S),我不得不复制他们的整个内部类,以获得预期的效果。还要注意他们是坏的资料员(参见 DropDownAdapter isEnabled / areAllItemsEnabled 一天的笑!)。

Is there a way to add a footer inside a spinner drop down list? For instance, say I am populating a spinner from a database, but I want a selection at the bottom of the list regardless of how I arrange the list. The list might be "Database Item 1, 2.., 3..." and at the bottom of the list a footer choice of "Add Item to Database."

So far I have used your CustomSpinner class as follows:

public class CustomSpinner extends Spinner implements OnItemClickListener {

private AlertDialog myDialog = null;
private OnClickListener myButtonClickListener = null;
private GrainSpinnerAdapter adapter = null;

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setButtonClickListener(OnClickListener listener) {
    myButtonClickListener = listener;
}

public void setAdapter(GrainSpinnerAdapter adapter){
    this.adapter = adapter;
}

@Override
public boolean performClick() {
    Context context = getContext();

    //Inflate the layout
    final LayoutInflater inflater = LayoutInflater.from(getContext());
    final View v = inflater.inflate(R.layout.my_custom_spinner, null);

    // set up list view
    final ListView lv = (ListView) v.findViewById(R.id.list);
    lv.setAdapter(adapter);
    lv.setSelection(getSelectedItemPosition());
    lv.setOnItemClickListener(this);

    // set up button
    final Button btn = (Button) v.findViewById(R.id.addButton);
    btn.setOnClickListener(myButtonClickListener);

    // build our dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    // show prompt, just as our Spinner parent does
    if (getPrompt() != null) {
        builder.setTitle(getPrompt());
    }

    // create and show dialog
    myDialog = builder.setView(v).create();
    myDialog.show();

    return true;
}

@Override
public void onItemClick(AdapterView<?> view, View itemView, int position, long id) {
    setSelection(position);
    if (myDialog != null) {
        myDialog.dismiss();
        myDialog = null;
    }

}

} I am wanting to use a separate adapter such as this:

public class GrainListAdapter extends SimpleCursorAdapter {

private static final String DEFAULT_UNITS = "American";
private Button upButton;
private Context myContext;
private RecipeGrainActivity parentActivity;
private Button downButton;
private String units;
private double getLbs;

public GrainListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    myContext = context;
    parentActivity = (RecipeGrainActivity) myContext;

    //Checks for metric pref.
    SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    units = String.valueOf(myPrefs.getString(context.getString(R.string.pref_measurement), DEFAULT_UNITS));
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int idColumn = cursor.getColumnIndex("_id");
    final int getId = cursor.getInt(idColumn);
    final double increment = 0.25;

    UnitsConversions convert = new UnitsConversions();

    int nameColumn = cursor.getColumnIndex("name");
    String getName = cursor.getString(nameColumn);
    TextView name = (TextView)view.findViewById(R.id.GrainName);
    name.setText(getName);

    int originColumn = cursor.getColumnIndex("origin");
    String getOrigin = cursor.getString(originColumn);
    TextView origin = (TextView)view.findViewById(R.id.GrainOrigin);
    origin.setText(getOrigin);

    if(units.equals("Metric")){
        //Sets labels to metric.
        String kilos = context.getResources().getString (R.string.kilograms);
        TextView weightLabel = (TextView)view.findViewById(R.id.GrainLbsLabel);
        weightLabel.setText(kilos);
    }


}

@Override
public View newView(Context context, Cursor cursor, final ViewGroup parent) {
    View view = View.inflate(context, R.layout.grain_list_item, null);
    return view;
}

}

Which allows me to build a custom row for each listitem in the spinner. I was thinking I could set the adapter to the CustomSpinner using the customspinner.setadapter() within my activity. However, the ListView defined in the CustomSpinner class uses an adapter referenced within the class. How do I pass my adapter into the class so it can use it?

Here is how I had to add the custom item to my layout:

<com.bluelightuniverse.android.brewmobile.CustomSpinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/GrainNameSpinner"
    android:layout_toRightOf="@id/GrainOriginSpinner"
    android:layout_toLeftOf="@+id/AddGrainButton">
</com.bluelightuniverse.android.brewmobile.CustomSpinner>

解决方案

I've been experimenting with making more complex views in Spinners' dialogs.

To do what you've explained, I made a subclass of Spinner. I examined the source of the Android Spinner and overrided the performClick to do essentially what you want: populate your own dialog with a custom view.

package me.ribose.example; // you'll have to change this

import android.app.AlertDialog;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class CustomSpinner extends Spinner implements OnItemClickListener {
    private AlertDialog mDialog = null;
    private OnClickListener mButtonClickListener = null;

    public UnitSelectionSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setButtonClickListener(OnClickListener listener) {
        mButtonClickListener = listener;
    }

    @Override
    public boolean performClick() {
        Context context = getContext();

        // get the set adapter
        final DropDownAdapter adapter = new DropDownAdapter(getAdapter());

        // inflate our layout
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        final View v = inflater.inflate(R.layout.customSpinner, null);

        // set up list view
        final ListView lv = (ListView) v.findViewById(R.id.list);
        lv.setAdapter(adapter);
        lv.setSelection(getSelectedItemPosition());
        lv.setOnItemClickListener(this);

        // set up button
        final Button btn = (Button) v.findViewById(R.id.addButton);
        btn.setOnClickListener(mButtonClickListener);

        // build our dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // show prompt, just as our Spinner parent does
        if (getPrompt() != null) {
            builder.setTitle(getPrompt());
        }

        // create and show dialog
        mDialog = builder.setView(v).create();
        mDialog.show();

        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> view,
                            View itemView, int position, long id) {
        setSelection(position);
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * <p>Wrapper class for an Adapter. Transforms the embedded Adapter instance
     * into a ListAdapter.</p>
     */
    private static class DropDownAdapter implements ListAdapter, SpinnerAdapter {
        private SpinnerAdapter mAdapter;

        /**
         * <p>Creates a new ListAddapter wrapper for the specified adapter.</p>
         *
         * @param adapter the Adapter to transform into a ListAdapter
         */
        public DropDownAdapter(SpinnerAdapter adapter) {
            this.mAdapter = adapter;
        }

        public int getCount() {
            return mAdapter == null ? 0 : mAdapter.getCount();
        }

        public Object getItem(int position) {
            return mAdapter == null ? null : mAdapter.getItem(position);
        }

        public long getItemId(int position) {
            return mAdapter == null ? -1 : mAdapter.getItemId(position);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            return getDropDownView(position, convertView, parent);
        }

        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return mAdapter == null ? null :
                    mAdapter.getDropDownView(position, convertView, parent);
        }

        public boolean hasStableIds() {
            return mAdapter != null && mAdapter.hasStableIds();
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            if (mAdapter != null) {
                mAdapter.registerDataSetObserver(observer);
            }
        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            if (mAdapter != null) {
                mAdapter.unregisterDataSetObserver(observer);
            }
        }

        /**
         * <p>Always returns false.</p>
         *
         * @return false
         */
        public boolean areAllItemsEnabled() {
            return true;
        }

        /**
         * <p>Always returns false.</p>
         *
         * @return false
         */
        public boolean isEnabled(int position) {
            return true;
        }

        public int getItemViewType(int position) {
            return 0;
        }

        public int getViewTypeCount() {
            return 1;
        }

        public boolean isEmpty() {
            return getCount() == 0;
        }
    }
}

You'll need my customSpinner.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="5px"
            android:cacheColorHint="@null"
            android:background="@android:color/background_light"
            android:divider="@android:drawable/divider_horizontal_bright"
            android:scrollbars="vertical">
    </ListView>
    <Button
            android:id="@+id/addButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Add to Database">
    </Button>
</LinearLayout>

Of course, make that android:text a string resource.

More words on this: Just make a CustomSpinner in your activity's layout as you need it and call setButtonClickListener (probably near where you set the adapter) to add a callback for your button, that perhaps prompts the user to add or whatever you need. And of course notifyDataSetChanged() on your adapter after adding to the database. Everything that works on a Spinner should work exactly the same with this custom spinner.

Side note (UPDATED): You will have to make sure your adapter's list items have black text (ex: android:textColor="@android:color/primary_text_light_nodisable")! I changed the XML above so it doesn't require white background on items (which then makes item highlights not work).


Code notes: Since the people who made Android decided to not make an easy way to customize spinner dialogs (hard-coded use of AlertDialog.Builder using setSingleChoiceItems() as well as a private class to "convert" SpinnerAdapters to ListAdapters), I had to copy their entire inner class to get the desired effect. Also note they are bad documenters (see DropDownAdapter.isEnabled/areAllItemsEnabled for the laugh of the day!).

这篇关于Android的微调页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆