如何使用 ArrayList 动态更新微调器? [英] How do I dynamically update a spinner using ArrayList?

查看:30
本文介绍了如何使用 ArrayList 动态更新微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Spinner,它填充了一个 ArrayList.我想动态地向 ArrayList 添加值,所以 Spinner 动态填充.但是,当我尝试向 ArrayList 添加值时,我得到一个 NullPointerException.

I have created a Spinner that is populated with an ArrayList. I want to dynamically add values to the ArrayList, so the the Spinner populates dynamically. However, when I try to add values to my ArrayList, I get a NullPointerException.

我错过了什么?在修改 ArrayList 之前是否必须重置适配器?

What am I missing? Do I have to reset the adapter before amending the ArrayList?

这是我的代码:

我的微调器、数组列表和适配器:

My spinner, arraylist, and adapter:

deleteselection = (Spinner)view.findViewById(R.id.deletespinner);
portfoliosdelete = new ArrayList<String>();
portfoliosdelete.add("Select Portfolio");
adapterdeletetype = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,portfoliosdelete){

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View v = null;

        // If this is the initial dummy entry, make it hidden
        if (position == 0) {
            TextView tv = new TextView(getContext());
            tv.setHeight(0);
            tv.setVisibility(View.GONE);
            v = tv;
        }
        else {
            // Pass convertView as null to prevent reuse of special case views
            v = super.getDropDownView(position, null, parent);
        }

        // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
        parent.setVerticalScrollBarEnabled(false);
        return v;
    }
};
adapterdeletetype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deleteselection.setAdapter(adapterdeletetype);

我动态更新微调器的代码:

My code to dynamically update spinner:

 else if(users.contains(usernull)){
    pn1 = enterportfolioname.getText().toString();
    user1 = new PortfolioRecord(pn1, "someemail@gmail.com");
    users.remove(usernull);
    users.add(user1);
    portfoliosdelete.add(pn1); // <-- This causes a null pointer exception
    adapterdeletetype.notifyDataSetChanged();
    portfoliolist.invalidateViews();

推荐答案

使用下面的代码.当用户从微调器中选择并添加新项目时,此代码将添加一个新项目.

Use the code below. This code will add a new item when the user selects and add a new item from the spinner.

代码示例:

布局 main.xml:

layout main.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"
    android:weightSum="10" >

    <Spinner
        android:id="@+id/cmbNames"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

布局 spinner_item.xml

layout spinner_item.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" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

活动类:

public class MainActivity extends Activity {
    private static final String NAME = "name";
    private static final String ADD_NEW_ITEM = "Add New Item";

    private SimpleAdapter adapter;
    private Spinner cmbNames;
    private List<HashMap<String, String>> lstNames;
    private int counter;

    private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            HashMap<String, String> map = lstNames.get(arg2);
            String name = map.get(NAME);
            if (name.equalsIgnoreCase(ADD_NEW_ITEM)) {
                lstNames.remove(map);
                counter++;
                addNewName(String.valueOf(counter));
                addNewName(ADD_NEW_ITEM);
                adapter.notifyDataSetChanged();
            }
        }

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

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

        populateList();

        cmbNames = (Spinner) findViewById(R.id.cmbNames);
        adapter = new SimpleAdapter(this, lstNames, R.layout.spinner_item,
                new String[] { NAME }, new int[] { R.id.tvName });
        cmbNames.setAdapter(adapter);
        cmbNames.setOnItemSelectedListener(itemSelectedListener);
    }

    private void populateList() {
        lstNames = new ArrayList<HashMap<String, String>>();

        addNewName("abc");
        addNewName("pqr");
        addNewName("xyz");
        addNewName(ADD_NEW_ITEM);
    }

    private void addNewName(String name) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(NAME, name);
        lstNames.add(map);
    }
}

这篇关于如何使用 ArrayList 动态更新微调器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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