设置onClickListener为微调的项目? [英] set onClickListener for spinner item?

查看:113
本文介绍了设置onClickListener为微调的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有微调其植入来自数据库:

  catSpinner =(微调)findViewById(R.id.spinner1);
光标= dataAdapter.getAllCategory();
startManagingCursor(光标);
的String []从=新的String [] {DataAdapter.CATEGORY_COL_NAME};
INT []到=新INT [] {android.R.id.text1};
SimpleCursorAdapter catAdapter =新SimpleCursorAdapter(这一点,
           android.R.layout.simple_spinner_dropdown_item,光标,从到,0);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catAdapter.notifyDataSetChanged();
catSpinner.setAdapter(catAdapter);
 

和我想打电话给 AlertDialog 当我选择最后一项(添加新类别... )。< BR> 之后,我添加了新的类我想要那个项(添加新类别... )是最后一次。
我怎样才能做到这一点?

解决方案

勾微调的OnItemClickListener。然后检查选择的项目是否是添加新类别。

如果有,显示对话框添加新的项目。

同时加入了新的项目,

  1. 删除最后出现的添加新类别。
  2. 添加进入了新的类别。
  3. 然后添加项目添加新类别了。

这将使添加新类别项作为最后一个。

code样品:<​​/ P>

布局main.xml中:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:weightSum =10&GT;

&LT;微调
    机器人:ID =@ + ID / cmbNames
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/&GT;

&LT; / LinearLayout中&GT;
 

布局spinner_item.xml

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直&GT;

&LT;的TextView
    机器人:ID =@ + ID / tvName
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/&GT;

&LT; / LinearLayout中&GT;
 

活动类:

 公共类MainActivity延伸活动{

私有静态最后字符串名称=名称;
私有静态最后弦乐ADD_NEW_ITEM =添加新项;

私人SimpleAdapter适配器;
私人微调cmbNames;
私人列表&LT; HashMap的&LT;字符串,字符串&GT;&GT; lstNames;
私人诠释柜台;

私人OnItemSelectedListener itemSelectedListener =新OnItemSelectedListener(){

    @覆盖
    公共无效onItemSelected(适配器视图&LT;&GT;为arg0,查看ARG1,INT ARG2,
            长ARG3){
        HashMap的&LT;字符串,字符串&GT;地图= lstNames.get(ARG2);
        字符串名称= map.get(名);
        如果(name.equalsIgnoreCase(ADD_NEW_ITEM)){
            lstNames.remove(图)
            反++;
            addNewName(将String.valueOf(计数器));
            addNewName(ADD_NEW_ITEM);
            adapter.notifyDataSetChanged();
        }
    }

    @覆盖
    公共无效onNothingSelected(适配器视图&LT;&GT;为arg0){
        // TODO自动生成方法存根

    }
};

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    populateList();

    cmbNames =(微调)findViewById(R.id.cmbNames);
    适配器=新SimpleAdapter(这一点,lstNames,R.layout.spinner_item,
            新的String [](名称),新的INT [] {R.id.tvName});
    cmbNames.setAdapter(适配器);
    cmbNames.setOnItemSelectedListener(itemSelectedListener);
}

私人无效populateList(){
    lstNames =新的ArrayList&LT; HashMap的&LT;字符串,字符串&GT;&GT;();

    addNewName(ABC);
    addNewName(PQR);
    addNewName(XYZ);
    addNewName(ADD_NEW_ITEM);
}

私人无效addNewName(字符串名称){
    HashMap的&LT;字符串,字符串&GT;图=新的HashMap&LT;字符串,字符串&GT;();
    map.put(名称,姓名);
    lstNames.add(图)
}

}
 

I have spinner which populates from database:

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

And I want to call AlertDialog when I select last item(Add new category...).
After I added new category I want that "item(Add new category...)" was the last again.
How I can do this?

解决方案

Hook to OnItemClickListener of Spinner. Then check whether the selected item is "Add new category".

If yes, show the dialog to add the new item.

While adding the new item,

  1. Remove the last item "Add new category".
  2. Add the new category entered.
  3. Then Add the item "Add new category" again.

This will make the "Add new category" item as last one.

Code Sample :

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>

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>

Activity class :

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

}

这篇关于设置onClickListener为微调的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆