Android EditText无法一致地获取数据
我正在使用EditText获取用户输入以创建停车场对象。编辑文本是包含所有重要信息的用户输入。然后我将这些信息添加到cloudmine数据库中。但是编辑文本不能一致地解析输入。我在cloudmine上检查它,它经常显示为null。不知道为什么 下面是代码片段Android EditText无法一致地获取数据,android,android-edittext,Android,Android Edittext,我正在使用EditText获取用户输入以创建停车场对象。编辑文本是包含所有重要信息的用户输入。然后我将这些信息添加到cloudmine数据库中。但是编辑文本不能一致地解析输入。我在cloudmine上检查它,它经常显示为null。不知道为什么 下面是代码片段 private String name; private String address; private String pricing; private String hours; private String latitude; priv
private String name;
private String address;
private String pricing;
private String hours;
private String latitude;
private String longitude;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);//calling parent constructor
setContentView(R.layout.addparking_layout);//setting up the GUI
findViewById(R.id.LotName).setOnKeyListener(this);
findViewById(R.id.LotAddress).setOnKeyListener(this);
findViewById(R.id.LotPricing).setOnKeyListener(this);
findViewById(R.id.LotHours).setOnKeyListener(this);
findViewById(R.id.LotLat).setOnKeyListener(this);
findViewById(R.id.LotLon).setOnKeyListener(this);
}
public void addParkingLot(View v)
{
new createCloudmineLot().execute();
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
System.out.println("Inside the onKey method");
EditText temp = (EditText) v;
System.out.println("Text: "+ temp.getText());
//getting the text for the appropriate info
if(temp.getId() == R.id.LotName)
{
name = "" + temp.getText();
System.out.println("name: "+name);
}
else
if(temp.getId() == R.id.LotAddress)
{
address = "" + temp.getText();
System.out.println("address: "+ address);
}
else
if(temp.getId() == R.id.LotPricing)
{
pricing = "" + temp.getText();
System.out.println("pricing: "+pricing);
}
else
if(temp.getId() == R.id.LotHours)
{
hours = "" + temp.getText();
System.out.println("hours: "+hours);
}
else
if(temp.getId() == R.id.LotLat)
{
latitude = "" + temp.getText();
System.out.println("latitude: "+latitude);
}
else
if(temp.getId() == R.id.LotLon)
{
longitude = "" + temp.getText();
System.out.println("longitude: "+longitude);
}
return false;
}
public class createCloudmineLot extends AsyncTask<Void,Void,Void>
{
private boolean success = false;
@Override
protected Void doInBackground(Void... params)
{
// TODO Auto-generated method stub
CMApiCredentials.initialize(APP_ID, API_KEY, getApplicationContext());//initializing Cloudmine connetion
SimpleCMObject parkingLot = new SimpleCMObject();
parkingLot.add("name", name);
parkingLot.add("address",address);
parkingLot.add("pricing",pricing);
parkingLot.add("hours",hours);
parkingLot.add("latitude",latitude);
parkingLot.add("longitude",longitude);
parkingLot.save(new ObjectModificationResponseCallback()
{
public void onCompletion(ObjectModificationResponse response)
{
success = response.wasSuccess();
Toast.makeText(AddParking.this, "Parking Lot was added:" + success,Toast.LENGTH_LONG).show();
}
});
return null;
}//end of doBackground
}//end on AsyncTask
}
私有字符串名称;
私有字符串地址;
私有字符串定价;
私人串小时;
私有字符串纬度;
私有字符串经度;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);//调用父构造函数
setContentView(R.layout.addparking_layout);//设置GUI
findviewbyd(R.id.LotName).setOnKeyListener(this);
findViewById(R.id.LotAddress).setOnKeyListener(this);
findviewbyd(R.id.LotPricing).setOnKeyListener(this);
findViewById(R.id.LotHours).setOnKeyListener(this);
findviewbyd(R.id.LotLat).setOnKeyListener(this);
findviewbyd(R.id.LotLon).setOnKeyListener(this);
}
公共停车场(视图五)
{
新建createCloudmineLot().execute();
}
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件)
{
//TODO自动生成的方法存根
System.out.println(“onKey方法内部”);
EditText温度=(EditText)v;
System.out.println(“文本:+temp.getText());
//获取适当信息的文本
if(temp.getId()==R.id.LotName)
{
name=“”+临时获取文本();
System.out.println(“名称:”+name);
}
其他的
if(temp.getId()==R.id.LotAddress)
{
地址=“临时获取文本()”;
System.out.println(“地址:“+地址”);
}
其他的
if(temp.getId()==R.id.LotPricing)
{
定价=”“+临时获取文本();
System.out.println(“定价:+定价”);
}
其他的
if(temp.getId()==R.id.LotHours)
{
小时数=”+临时获取文本();
System.out.println(“小时数:+小时数”);
}
其他的
if(temp.getId()==R.id.LotLat)
{
纬度=”+温度getText();
System.out.println(“纬度:”+纬度);
}
其他的
if(temp.getId()==R.id.LotLon)
{
经度=”+临时获取文本();
System.out.println(“经度:”+经度);
}
返回false;
}
公共类createCloudmineLot扩展异步任务
{
私有布尔成功=假;
@凌驾
受保护的Void doInBackground(Void…参数)
{
//TODO自动生成的方法存根
初始化(APP_ID,API_KEY,getApplicationContext());//初始化Cloudmine连接
SimpleCMObject parkingLot=新的SimpleCMObject();
停车场。添加(“名称”,名称);
停车场。添加(“地址”,地址);
停车场。添加(“定价”,定价);
停车场。添加(“小时”,小时);
停车场。添加(“纬度”,纬度);
停车场。添加(“经度”,经度);
parkingLot.save(新对象ModificationResponseCallback()
{
完成时公共无效(ObjectModificationResponse)
{
success=response.wassucture();
Toast.makeText(AddParking.this,“停车场被添加:“+success,Toast.LENGTH_LONG.show()”;
}
});
返回null;
}//多臂架末端
}//结束于异步任务
}
我想指出您的代码中有几点可能是个问题。虽然这不是一个具体的答案,但我认为它会解决你的问题并帮助你解决它。其中一个甚至可能是主要问题
首先考虑你的“OnKEY”方法。你的反应是什么?您没有处理“事件”或“密钥码”。该方法将在所有按键上调用,包括字母。我怀疑“null”值来自用户按下任何非字符键
我的建议是修改代码,以便只对enter键做出反应
if(keyCode == KeyEvent.KEYCODE_ENTER)
现在您可以确定用户按了enter键,这通常意味着他输入了一些有意义的内容
我还建议降低代码的复杂性。这很难读。我建议使用“开关”语句
switch(temp.getId())
{
case(R.id.LotName):
// Your code here
break;
case(R.id.nextEditTextID):
// etc
}
这将使您的代码更易于阅读,并且不太可能出错