Android 选择微调器和微调器
如果我有2个微调器,类型ArrayList>Android 选择微调器和微调器,android,Android,如果我有2个微调器,类型ArrayList> spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();
for (HashMap<String, String> map2 : arrList) {
String value = map2.get("SectionID");
// Do something
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, value, duration);
toast.show();
Log.d("wwwwwwwwwwwwwwwwwwww: ", value);
// Do something
}
}
@Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
spinner2.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共位置(AdapterView AdapterView、视图视图、整型位置、长id){
ArrayList arrList=新的ArrayList();
for(HashMap映射2:arrList){
字符串值=map2.get(“SectionID”);
//做点什么
Context=getApplicationContext();
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、值、持续时间);
toast.show();
Log.d(“WWWWWW:,值);
//做点什么
}
}
@凌驾
未选择公共无效(AdapterView适配器){
}
});
我确实喜欢这样,但是kothing发生了,logcat没有错误
在用户单击微调器中的项目后,我不知道如何获取此数组中带有标记(courseid)的项目
如果只想从每行获取courseid
,则不需要自定义OnItemSelectedListener。简单使用:
// Let's use the regular listener vvvvvvvvvvvvvvvvvvvvvv
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
HashMap<String, String> map = arrList.get(position);
String id = map.get("courseid");
// Do something
}
@Override
public void onNothingSelected(AdapterView<?> adapter) {}
});
您需要使用创建SimpleAdapter时使用的ArrayList
new SimpleAdapter(this, arrList, ...);
这是您必须使用的数组列表,并且它必须是类变量 你的帖子有点难理解,但我想我有个主意。您有一个由arraylist填充的微调器,该微调器上每个“梯级”的每个索引处都有多个hashmaps。因此,当项目被选中时,您希望获得选中的单个键并对其进行处理。正如我们所知,
hashMap
并不是真正的索引,所以我们必须使用其他方法来获取它,对吗
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// for each key in the hashMap at this position..
for (String key : arrList.get(position).keySet()) {
if (key == "Calculus") {
// do something
} else if (key == "Physics") {
// do something else
}
}
}
ArrayList arrList=new ArrayList();
spinner.setOnItemSelectedListener(新的CustomOnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View、,
内部位置,长id){
//对于hashMap中此位置的每个键。。
for(字符串键:arrList.get(position.keySet()){
如果(键==“微积分”){
//做点什么
}否则,如果(键==“物理”){
//做点别的
}
}
}
这里的想法非常简单。您可以在微调器上选择的位置的索引处获得hashMap的整个键集,并根据其中的每个键所说的内容对其执行某些操作。假设您只有一个键值对,那么这应该不会太麻烦
但是我必须说,你真的应该重新考虑一下设计。你有多个hashMap容器,每个容器只保存一件东西。这真的是浪费资源。很难推荐一个替代方案,因为我不知道这些数据是如何被使用的,但是你应该知道用java制作对象是不免费的。I不明白你可以解释一下…我的意思是我会得到位置然后课程ID你的arraylist在每个索引处有一个hashmap,只有一个键值对吗?[link]我的数组列表是coursesList并做这个map.put(TAG_CourseID,CourseID);map.put(TAG_Name,Name);//将HashList添加到arraylist coursesList.add(map);你能帮我更多的忙吗?这个例子和你说的不一样,但我把它改成for(字符串键:arrList.get(position.get(“courseid”)),并且只有在数组或java.lang.iterablei的实例上才会出现错误。如果你保持原样,也许把“演算”改成“courseid”,它就会工作。你的问题是,你正在试图获取一个键(本例中为字符串),这是不可编辑的。代码所做的是获取所有键(无论有多少键)并对特定键进行迭代。如果键为“courseid”,则按您的方式执行不是最好的只有在arrayList的一个索引中,除非您特别想知道它是否存在。我在CustomOnItemSelectedListene类中写了什么?您没有使用添加任何特殊功能,因此不需要使用CustomOnItemSelectedListener。您可以使用常规OnItemSelectedListener。
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// for each key in the hashMap at this position..
for (String key : arrList.get(position).keySet()) {
if (key == "Calculus") {
// do something
} else if (key == "Physics") {
// do something else
}
}
}