如何将删除项目选项添加到微调框下拉列表 [英] How to add delete item option to Spinner dropdown list

查看:87
本文介绍了如何将删除项目选项添加到微调框下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户有可能删除微调器下拉列表中显示的项目,方法是单击每个项目右侧的"X" 按钮,请参见下面的示例.

I want to let the user the possibility to delete the items shown in dropdown list of the spinner if clicks on the "X" button on the right side of each item, see the sample below.

如果用户单击该项目,则Spinner的行为应类似于经典的Android默认Spinner.

If the user click on the item the Spinner should act like the classic Android default Spinner.

我应该如何扩展和重新定义Spinner以这种方式工作?

How should I extend and re-define the Spinner to work in this way?

推荐答案

我认为您正在寻找:

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spnView3 = (Spinner) findViewById(R.id.spnView3);

    ArrayList<String> aList = new ArrayList<String>();
    aList.add("--Select--");
    aList.add("Md. Shahadat Sarker");
    aList.add("Developer");
    aList.add("ErrrorPoint");

    spnView3.setAdapter(new SpinnerAdapter(this, R.layout.spinner_row, aList, aList));

    Toast.makeText(this, "Selected: " + spnView3.getSelectedItem(), 500).show();

}

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spnView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left" />

</RelativeLayout>

spinner_row.xml(布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="9"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/spnItemName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="9"
            android:text="Item Name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/spnItemDel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="X"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

</LinearLayout>

适配器:

public View getCustomView(final int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        View rowView =  inflater.inflate(R.layout.spinner_row, parent, false);

        spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
        spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);

        spnItemName.setText(iName.get(position)+"");

        spnItemDel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
                //iName[position] = null;
                iName.remove(position);
                notifyDataSetChanged();
            }
        });
        return rowView;
    }

完整示例为此处...下载

已编辑部分:

   if( /*Your condition goes here*/ ){
        spnItemName.setVisibility(View.GONE);
    } else{
        spnItemName.setVisibility(View.VISIBLE);
    }

这篇关于如何将删除项目选项添加到微调框下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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