【问题标题】:custom spinner with last item as a link以最后一项作为链接的自定义微调器
【发布时间】:2014-09-16 19:01:56
【问题描述】:

我想要一个 Spinner,其中包含最后一个 Item 作为“添加更多项目” 当我点击它时,我可以添加下一个项目。我添加的项目应该显示在微调器列表中,并且最后一项与“添加更多项目”相同。 我尝试使用Adapter,但我怎样才能将最后一个元素保留为“添加更多项目”

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == spinner.getItemIdAtPosition(spinner.getCount()))
                // my code for adding item to list using Adapter
            else
                // spinner.setSelection();
        }

我写错了吗?? 有什么帮助吗??...谢谢...

【问题讨论】:

  • 你尝试了什么?
  • 我不知道该怎么做。!!
  • 试试yourAdapter.insert(data, yourAdapter.getCount());

标签: android spinner


【解决方案1】:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

           <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:dropDownVerticalOffset="2dp"
            android:dropDownWidth="500sp"
            android:spinnerMode="dropdown" />


    </RelativeLayout>

MainActivity.java:

public class MainActivity extends Activity {

    private Spinner spinner;

    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList<String> data = new ArrayList<String>();

        for (int i = 0; i < 5; i++) {
            data.add("item " + i);
        }
        data.add("Add New Item");

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);
        spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                tv.setText(spinner.getSelectedItem().toString());

                if (spinner.getSelectedItem().toString().equals("Add New Item")) {

                    data.remove(position);
                    data.add(position, "item " + position);
                    data.add((position + 1), "Add New Item");

                    updateAdapter(data);

                    Toast.makeText(getApplicationContext(), "item " + position,
                            Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

    }

    public void updateAdapter(ArrayList<String> data) {

        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
                data);
        adapter.notifyDataSetChanged();

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多