2012-08-24 58 views
1

我已經創建了一個Spinner,其中填充了ArrayList。我想動態地將值添加到ArrayList,所以Spinner動態填充。但是,當我嘗試將值添加到我的ArrayList時,我收到NullPointerException如何使用ArrayList動態更新微調器?

我錯過了什麼?修改ArrayList之前是否必須重置適配器?

這裏是我的代碼:

我的微調,ArrayList中,和適配器:

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); 

我的代碼,以動態更新微調:

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

這哪裏是附加的東西在哪裏?你肯定在這個時候portfoliodelete是instanciated? – njzk2

回答

1

使用下面的代碼。此代碼將在用戶從微調器中選擇並添加新項目時添加一個新項目。

代碼示例:

佈局的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

<?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); 
    } 
} 
+0

感謝您的洞察力 –

0

嘗試調用刪除adapterdeletetype代替陣列列表。如果失敗,請發佈您的logcat輸出。