在项目中,下拉框所展示的信息无法满足用户需求时,需要用户自己手动输入,并且需要在下次以下拉是将上次输入的数据显示出来。下面通过spinner+edittext来实现输入,用xml将数据存储下来。
下面是实现功能截图:
当选择“请输入”时,用户自己输入地址
下一次在打开下拉框时:
具体的实现代码如下:
MainActivity .java
public class MainActivity extends Activity {
Spinner spinner1;
EditText edittext;
private ArrayAdapter<String> mAdapter;
private List<String> Counties=new ArrayList<String>();
private List<County> mcounties;
String path=Environment.getExternalStorageDirectory().toString()+"/county.xml";
XMLService xmlService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取城市数据
xmlService=new XMLService();
try {
mcounties=xmlService.getCounties(path);
Log.i("Test", mcounties.size()+"");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spinner1=(Spinner)findViewById(R.id.spinner1);
edittext=(EditText) findViewById(R.id.edittext);
if(mcounties!=null){
for(int i=0;i<mcounties.size();i++){
Counties.add(mcounties.get(i).getName());
}
}
mAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,Counties);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(mAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
// TODO Auto-generated method stub
String str=(String)spinner1.getAdapter().getItem((int)id);
if(str.equals("请输入")){
edittext.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Goto(View v){
//在这里将新编辑的数据加入到county.xml中
String s=edittext.getText().toString();
if(!s.equals("")){
int tmp=mcounties.size();
County tmpcounty=new County(tmp, s);
List<County> tmpcounties=new ArrayList<County>();
for (int i = 0; i < mcounties.size()+1; i++) {
if(i<tmp-1){
tmpcounties.add(mcounties.get(i));
}
if(i==tmp-1){
tmpcounties.add(tmpcounty);
}
if(i==tmp){
County tmpcounty1=new County(tmp+1, mcounties.get(i-1).getName());
tmpcounties.add(tmpcounty1);
}
}
File file=new File(path);
file.delete();
File xmlFile = new File(Environment.getExternalStorageDirectory().toString(), "county.xml");
Log.i("Test", Environment.getExternalStorageDirectory().toString());
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(xmlFile);
for(County county:tmpcounties){
Log.i("Test", county.getName());
}
xmlService.NewXML(tmpcounties, outStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
Toast.makeText(getApplicationContext(), "请输入地址", Toast.LENGTH_LONG).show();
}
}
}
County.java
package com.example.domain;
public class County {
int id;
String name;
public County() {
super();
}
public County(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XMLService.java
package com.example.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.example.domain.County;
import android.annotation.SuppressLint;
import android.util.Xml;
@SuppressLint("UseValueOf")
public class XMLService {
/**
* 查询XML文件下的数据
* @param path 文件路径
* @return
* @throws Exception
*/
public List<County> getCounties(String path) throws Exception{
File file=new File(path);
InputStream in=new FileInputStream(file);
List<County> counties =null;
County county=null;
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(in, "UTF-8");
int event = pullParser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_DOCUMENT:
counties = new ArrayList<County>();
break;
case XmlPullParser.START_TAG:
if("county".equals(pullParser.getName())){
int id = new Integer(pullParser.getAttributeValue(0));
county = new County();
county.setId(id);
}
if("name".equals(pullParser.getName())){
String name = pullParser.nextText();
county.setName(name);
}
break;
case XmlPullParser.END_TAG:
if("county".equals(pullParser.getName())){
counties.add(county);
county = null;
}
break;
}
event = pullParser.next();
}
return counties;
}
/**
*重新生成XML文件
* @param counties
* @param out
* @throws Exception
*/
public void NewXML(List<County> counties, OutputStream out) throws Exception{
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "counties");
for(County county : counties){
serializer.startTag(null, "county");
serializer.attribute(null, "id", county.getId()+"");
serializer.startTag(null, "name");
serializer.text(county.getName());
serializer.endTag(null, "name");
serializer.endTag(null, "county");
}
serializer.endTag(null, "counties");
serializer.endDocument();
out.flush();
out.close();
}
}
在SDcard下–county.xml:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<counties>
<county id="1">
<name>北京</name>
</county>
<county id="2">
<name>西安</name>
</county>
<county id="3">
<name>上海</name>
</county>
<county id="4">
<name>惠州</name>
</county>
<county id="5">
<name>请输入</name>
</county>
</counties>