35

データベースから入力するスピナーがあります:

catSpinner = (Spinner) findViewById(R.id.spinner1);
cursor = dataAdapter.getAllCategory();
startManagingCursor(cursor);
String[] from = new String[] { DataAdapter.CATEGORY_COL_NAME };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter catAdapter = new SimpleCursorAdapter(this,  
           android.R.layout.simple_spinner_dropdown_item, cursor, from,to, 0);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catAdapter.notifyDataSetChanged();
catSpinner.setAdapter(catAdapter);

AlertDialogそして、最後のアイテム( )を選択したときに呼び出したいですAdd new category...。新しいカテゴリを追加した後、「item( )」を最後に
戻したいと思います。 どうすればこれを行うことができますか?Add new category...

4

2 に答える 2

100

スピナーを呼び出すべきではありません。OnItemClickListenerスピナーはアイテム クリック イベントをサポートしていません。このメソッドを呼び出すと、例外が発生します。これを確認してください。

代わりに申請できますOnItemSelectedListener

編集 :

spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {
        String selectedItem = parent.getItemAtPosition(position).toString();
        if(selectedItem.equals("Add new category"))
        {
                // do your stuff
        }
    } // to close the onItemSelected
    public void onNothingSelected(AdapterView<?> parent) 
    {

    }           
});

リストの最後に「新しいカテゴリを追加」を追加することに関する限り、すべてのアイテムを追加した後、その定数(「新しいカテゴリを追加」)を最後に追加できるカスタムアダプターを使用する方がよいと思います常に最後になるように配列します。

于 2012-08-24T11:55:06.647 に答える
7

Spinner の OnItemClickListener にフックします。次に、選択した項目が「新しいカテゴリを追加」であるかどうかを確認します。

はいの場合は、ダイアログを表示して新しいアイテムを追加します。

新しいアイテムを追加しながら、

  1. 最後の項目「新しいカテゴリを追加」を削除します。
  2. 入力した新しいカテゴリを追加します。
  3. 次に、「新しいカテゴリを追加」という項目を再度追加します。

これにより、「新しいカテゴリを追加」項目が最後の項目になります。

コードサンプル:

レイアウト 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);
}

}
于 2012-08-24T11:55:37.813 に答える