字符串数组中微调 [英] string array in spinner

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

问题描述

我这是在微调适配器填充的数组。
现在我想改变数组的大小!可能吗?
帮帮我!
谢谢
`公共无效classpopulate(){
        如果(PEP.getUser()== NULL){
            返回;
        }

i have an array which is populated in the spinner adapter. now i wanna change the size of the array! is it possible? help! thank u `public void classpopulate() { if (PEP.getUser() == null) { return; }

    classdetails = masterDataManager.getClassSections(PEP.getUser()
            .getUsername(), getApplicationContext());
    spnrClass = (Spinner) findViewById(R.id.spnrClass);
    spnrSubject = (Spinner) findViewById(R.id.spnrSubject);
    classname = new String[classdetails.size() + 1];
    classname[0] = "SELECET CLASS";

    for (int i = 1; i < classdetails.size() + 1; i++) {

        classname[i] = classdetails.get(i - 1).getClass_name() + "  "
                + classdetails.get(i - 1).getSection_name().toString();

    }

    ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>(
            getApplicationContext(), R.layout.spinner_item_class,
            R.id.spinnerclasstxt, classname);
    spnrClass.setAdapter(adapterClasses);
    spnrClass.setSelection(0);

    spnrClass
            .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int pos, long arg3) {
                    // LinearLayout layoutSpinnersubj = (LinearLayout)
                    // findViewById(R.id.layout_spinner);
                    // RelativeLayout subject_Text
                    // =(RelativeLayout)findViewById(R.id.layoutsubjecttext);
                    int selectedindex = pos;
                    if (selectedindex == 0) {
                        spnrSubject.setVisibility(View.INVISIBLE);
                    } else {
                        spnrSubject.setVisibility(View.VISIBLE);
                        selectedClass = classdetails.get(selectedindex - 1);

                        subjectpopulate(selectedClass);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }

            });

`

推荐答案

您可以使用 ArrayAdapter 方法明确添加插入和/或删除来操纵适配器的数据。

You can use the ArrayAdapter methods clear, add, insert and/or remove to manipulate the adapter's data.

编辑:从您的意见,这听起来像你最终将需要编写自己的自定义适配器(可能通过延长 BaseAdapter ),特别是如果你想有禁用(分离)行,等等。你将有什么获取显示更多的控制,以及如何它看起来。在网页中搜寻的Android的自定义列表适配器的找到很多关于如何写一个例子。这不是太困难的。

From your comments, it sounds like you are eventually going to need to write your own custom adapter (probably by extending BaseAdapter), particularly if you want to have disabled (separator) rows, etc. You will have much more control over what gets displayed and how it looks. Search the web for "android custom list adapter" to find lots of examples of how to write one. It's not too difficult.

不过,如果你想使用 ArrayAdapter ,这里的删除列表中的第一个元素的一个例子,以保持(它假定适配器已被保存在一个成员字段适配器

Nevertheless, if you want to keep using an ArrayAdapter, here's an example of removing the first element of the list (it assumes that the adapter has been saved in a member field adapter):

adapter.remove(adapter.getItem(0));

有关更复杂的变化(如加载完全不同的数据集),你可能会做这样的事情:

For more complicated changes (like loading a completely different set of data), you might do something like this:

String[] newData = . . .
adapter.setNotifyOnChange(false); // temporarily turn off auto-notification
adapter.clear();
adapter.addAll(newData);
adapter.notifyDataSetChanged(); // notifies and turns auto-notifications back on

这是很好的做法,暂停自动通知,当你在做序数的变化。

It's good practice to suspend auto-notification when you are making several changes in sequence.

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

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