Spinner默认值选择错误android

时间:2013-08-09 10:37:14

标签: android

我正在使用微调器和自定义适配器(没有单选按钮)。微调器正在选择第一个值并传递一个不应发生的意图。

我的要求是当我点击微调器中的某个项目时,只会发生相应的操作。

我的代码如下。

CustomAdapter<String> adapter = new CustomAdapter<String>(this,                 android.R.layout.simple_spinner_dropdown_item,
                new String[] { "Add/Delete symbols", "forex","metal" });
        adapter.setDropDownViewResource(R.drawable.spinner_button);
chooseMarket.setPrompt("Currency settings");
        chooseMarket.setAdapter(adapter);

chooseMarket.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {

String selectedItem = chooseMarket.getSelectedItem().toString();
if (selectedItem.equalsIgnoreCase("Add/Delete symbols")) {
//Intent passing here
}

@Override
    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }

});

//我的自定义适配器

private static class CustomAdapter<T> extends ArrayAdapter<String> {
    public CustomAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view
                .findViewById(android.R.id.text1);
        // RadioButton rb = view.findViewById(R.id.)
        textView.setText("");
        return view;
    }
}

请帮忙

1 个答案:

答案 0 :(得分:0)

尝试使用它:

private static class CustomAdapter<T> extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId,
        String[] objects) {
    super(context, textViewResourceId, objects);
}

public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView textView = (TextView) view
            .findViewById(android.R.id.text1);
    // RadioButton rb = view.findViewById(R.id.)
    textView.setText(objects[position]);

    view.setOnclickListner(new OnClickListener){

    String selectedItem = textView.getText().toString();
    if (selectedItem.equalsIgnoreCase("Add/Delete symbols")) {
       //Intent passing here
    }
    }

    return view;
}}