簡體   English   中英

為微調項設置 onClickListener?

[英]set onClickListener for spinner item?

我有從數據庫填充的微調器:

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

當我選擇最后一項( Add new category... )時,我想調用AlertDialog
添加新類別后,我希望“項目( Add new category... )”再次成為最后一個。
我怎么能做到這一點?

不應該在微調器上調用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) 
    {

    }           
});

就在列表末尾添加“添加新類別”而言,我認為您最好選擇自定義適配器,在添加所有項目后,您可以將該常量(“添加新類別”)添加到末尾數組,以便它應該始終排在最后。

掛鈎到 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);
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM