为spinner项设置onClickListener?

Vol*_*myr 26 android listener spinner

我有spinner从数据库填充:

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);
Run Code Online (Sandbox Code Playgroud)

我想AlertDialog在选择最后一项(Add new category...)时打电话.
添加新类别后,我希望"item(Add new category...)"再次成为最后一个.
我怎么能这样做?

Bra*_*raj 81

不应该打电话OnItemClickListener给旋转器.Spinner不支持项目点击事件.调用此方法将引发异常.检查一下.

你可以申请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) 
    {

    }           
});
Run Code Online (Sandbox Code Playgroud)

至于在列表末尾添加"添加新类别",我认为你最好选择自定义适配器,在添加所有项目后,你可以添加该常量("添加新类别")到结尾数组,所以它应该总是最后.


Eld*_*abu 6

钩到Spinner的OnItemClickListener.然后检查所选项目是否为"添加新类别".

如果是,请显示添加新项目的对话框.

在添加新项目时,

  1. 删除最后一项"添加新类别".
  2. 添加输入的新类别.
  3. 然后再次添加"添加新类别"项.

这将使"添加新类别"项目成为最后一项.

代码示例:

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

活动类:

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

}
Run Code Online (Sandbox Code Playgroud)