2012-08-24 41 views
14

Tôi có spinner mà populates từ cơ sở dữ liệu:đặt onClickListener cho mục 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); 

Và tôi muốn gọi AlertDialog khi tôi chọn mục cuối cùng (Add new category...).
Sau khi tôi thêm danh mục mới, tôi muốn "mục (Add new category...)" là lần cuối cùng.
Làm cách nào tôi có thể thực hiện việc này?

Trả lời

49

Bạn NÊN KHÔNG gọi OnItemClickListener cho máy quay. Spinner không hỗ trợ các sự kiện nhấp chuột vào mục. Gọi phương thức này sẽ làm tăng Ngoại lệ. Kiểm tra this. Thay vào đó, bạn có thể áp dụng OnItemSelectedListener.

Edit:

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

     }   
    }); 

Theo như thêm "Thêm mục mới" vào cuối danh sách là có liên quan, tôi nghĩ rằng bạn tốt hơn nên đi cho adapter tùy chỉnh trong đó sau khi thêm tất cả các mục của bạn, bạn có thể thêm hằng số đó (Thêm danh mục mới) vào cuối mảng để nó luôn luôn tồn tại.

+0

Tôi biết điều này. Nhưng cách tôi có thể xử lý trong 'OnItemSelectedListener' chỉ chọn một mục? –

+1

khi bạn chọn bất kỳ mục nào, bạn sẽ nhận được mục nào được chọn. So sánh mục đã chọn với mục được xác định trước của bạn vì nó là hằng số. Nếu mục đã chọn là mục được xác định trước, hãy thực hiện tác vụ của bạn – Braj

+0

Nó không hoạt động. Tôi nghĩ vì 'parent.getItemAtPosition (vị trí) .toString();' vị trí trả về và chuyển đổi nó thành chuỗi. –

5

Hook to OnItemClickListener of Spinner. Sau đó kiểm tra xem mục đã chọn là "Thêm danh mục mới" hay chưa.

Nếu có, hãy hiển thị hộp thoại để thêm mục mới.

Trong khi thêm mục mới,

  1. Tháo mục cuối cùng "Thêm mục mới".
  2. Thêm danh mục mới được nhập.
  3. Sau đó, thêm lại mục "Thêm danh mục mới".

Điều này sẽ làm cho mục "Thêm danh mục mới" làm mục cuối cùng.

Mã mẫu:

bố trí 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> 

bố trí 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> 

Hoạt động lớp:

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

} 
+1

Tôi đã cập nhật bài đăng với mã mẫu. Hãy kiểm tra. –

+0

một onitemclicklistener không thể được nối với một spinner – SoroushA

Các vấn đề liên quan