Android 下拉列表代码错误
一段关于下拉列表的代码有错误,但楼主新人···一直没看出来哪里错了,求大神帮忙strings.ml件:<?xmlversion="1.0"encoding="utf-8"...
一段关于下拉列表的代码有错误,但楼主新人···一直没看出来哪里错了,求大神帮忙
strings.ml件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">bike</string>
<string name="menu_settings">Settings</string>
<string-array name="area_array">
<item>镜湖区</item>
<item>弋江区</item>
<item>东城区</item>
<item>鸠江区</item>
</string-array>
</resources>
MainActivity.java文件:
package com.example.bike;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
public Spinner spinner;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.creatFromResource(this,R.array.area_array,android.R.layout.simple_arear_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdowm_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListtener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent,View view,int position,long id){
Toast.makeText(parent.getContext(), "你所选择的地区是"+parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent){
// Do nothing.
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
} 展开
strings.ml件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">bike</string>
<string name="menu_settings">Settings</string>
<string-array name="area_array">
<item>镜湖区</item>
<item>弋江区</item>
<item>东城区</item>
<item>鸠江区</item>
</string-array>
</resources>
MainActivity.java文件:
package com.example.bike;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
public Spinner spinner;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.creatFromResource(this,R.array.area_array,android.R.layout.simple_arear_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdowm_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListtener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent,View view,int position,long id){
Toast.makeText(parent.getContext(), "你所选择的地区是"+parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent){
// Do nothing.
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
} 展开
5个回答
展开全部
你这写的是什么,前面数组在strings.xml中,下面使用时调用R.array.area_array??
追问
额···你的意思是?我初学者,不太懂你的意思···
追答
就是代码的最上面写着数组在strings.xml中定义的,而下面ArrayAdapter adapter =ArrayAdapter.creatFromResource(this,R.array.area_array,android.R.layout.simple_arear_item);使用的却是R.array.area_array
展开全部
你应该把错误贴出来的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-12-11
展开全部
吧报错的地方贴出来啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
倒数第二个参数是 由于那个游标有很多列 所以需要指定一个数组
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询