Android onclick按钮未获取api的编辑文本字符串
我正在使用API数据库开发一个卡路里应用程序。当用户单击搜索按钮时,它将获取字符串,然后搜索数据库。由于某些原因,无法检索用户编辑文本“字符串”,因此无法搜索api数据库。当我进行调试时,我注意到字符串“”表示空 再次非常感谢,api和android studio新手Android onclick按钮未获取api的编辑文本字符串,android,api,android-fragments,onclicklistener,Android,Api,Android Fragments,Onclicklistener,我正在使用API数据库开发一个卡路里应用程序。当用户单击搜索按钮时,它将获取字符串,然后搜索数据库。由于某些原因,无法检索用户编辑文本“字符串”,因此无法搜索api数据库。当我进行调试时,我注意到字符串“”表示空 再次非常感谢,api和android studio新手 public class AddEntry extends Fragment implements View.OnClickListener { EditText FoodET,CalorieET; Ima
public class AddEntry extends Fragment implements View.OnClickListener {
EditText FoodET,CalorieET;
ImageButton Savebtn, Cancelbtn;
Button searchbutton;
String foodET,calorieET;
//database
private DatabaseHandler dba;
public AddEntry() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView = inflater.inflate(R.layout.fragment_add_entry, container,
false);
Savebtn = (ImageButton) myView.findViewById(R.id.SaveBtn);
Savebtn.setBackgroundColor(Color.TRANSPARENT);
Savebtn.setOnClickListener(this);
searchbutton = (Button) myView.findViewById(R.id.SearchButton);
searchbutton.setOnClickListener(this);
Cancelbtn = (ImageButton) myView.findViewById(R.id.CancelBtn);
Cancelbtn.setBackgroundColor(Color.TRANSPARENT);
Cancelbtn.setOnClickListener(this);
return myView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FoodET= (EditText)view.findViewById(R.id.foodEditText);
FoodET.setInputType(InputType.TYPE_CLASS_TEXT);
CalorieET=(EditText)view.findViewById(R.id.caloriesEditText);
CalorieET.setInputType(InputType.TYPE_CLASS_NUMBER);
foodET = ((EditText)
view.findViewById(R.id.foodEditText)).getText().toString();
foodET.isEmpty();
FoodET.setText("");
CalorieET.setText("");
calorieET = ((EditText)
view.findViewById(R.id.caloriesEditText)).getText().toString();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.SearchButton:
FoodSearch search = new FoodSearch(foodET, CalorieET );
search.execute();
break;
case R.id.SaveBtn:
if (FoodET.getText().toString().equals(null) ||
CalorieET.getText().toString().equals(null)||
CalorieET.getText().toString().equals("") ||
CalorieET.getText().toString().equals("")){
Toast.makeText(getActivity(), "Please enter information",
Toast.LENGTH_LONG).show();
AlertDialog NotFound = new
AlertDialog.Builder(getContext()).create();
NotFound.setTitle("Error");
NotFound.setMessage("Food not found :(");
NotFound.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {
dialog.dismiss();
}
});
}
else
((appMain) getActivity()).loadSelection(0);
break;
case R.id.CancelBtn:
// EditText descriptionET=
(EditText)getView().findViewById(R.id.foodEditText);
//descriptionET.setText("");
//EditText calorieET=
(EditText)getView().findViewById(R.id.caloriesEditText);
//calorieET.setText("");
((appMain) getActivity()).loadSelection(0);
break;
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
private class FoodSearch extends AsyncTask<Void, Void, String> {
String food;
EditText calories;
FoodSearch(String food, EditText calories){
this.food = food;
this.calories = calories;
}
@Override
protected String doInBackground(Void... params) {
try {
food = food.replaceAll(" ", "%20");
URL url = new URL("http://api.nal.usda.gov/ndb/search/?
format=JSON&q=" + food +
"&max=1&offset=0&sort=r&api_
key=xMJV33vSmKsquFqcBwZ23oJ7DlL2abmfsrDUUx1l");
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
String result = stringBuilder.toString();
if(result.contains("zero results")) {
String s = "empty";
return s;
}
JSONObject object = (JSONObject) new
JSONTokener(result).nextValue();
JSONObject list = object.getJSONObject("list");
JSONArray items = list.getJSONArray("item");
String item = items.get(0).toString();
int i = item.indexOf("ndbno\":\"") + 8;
int f = item.indexOf("\"", i);
String ndbno = item.substring(i,f);
Log.d("DEBUG", ndbno);
URL url2 = new URL("http://api.nal.usda.gov/ndb/reports/?
ndbno=" + ndbno +
"&type=b&format=JSON&api_
key=xMJV33vSmKsquFqcBwZ23oJ7DlL2abmfsrDUUx1l");
HttpURLConnection urlConnection2 = (HttpURLConnection)
url2.openConnection();
BufferedReader bufferedReader2 = new BufferedReader(new
InputStreamReader(urlConnection2.getInputStream()));
StringBuilder stringBuilder2 = new StringBuilder();
String line2;
while ((line2 = bufferedReader2.readLine()) != null) {
stringBuilder2.append(line2).append("\n");
}
bufferedReader2.close();
String res = stringBuilder2.toString();
int index = res.indexOf("\"unit\": \"kcal\",") + 46;
int index2 = res.indexOf("\"", index);
String calories = res.substring(index,index2);
urlConnection2.disconnect();
return calories;
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
String s = "empty";
return s;
}
}
protected void onPostExecute(String response) {
if(!response.isEmpty() && !response.equals("empty")) {
calories.setText(response);
} else {
AlertDialog foodNotFound = new
AlertDialog.Builder(getContext()).create();
foodNotFound.setTitle("Error");
foodNotFound.setMessage("Food not found :(");
foodNotFound.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {
dialog.dismiss();
}
});
}
}
}
}
公共类AddEntry扩展片段实现View.OnClickListener{
EditText FoodET,Carolieet;
图像按钮保存btn,取消btn;
按钮搜索按钮;
食物串,卡路里;
//数据库
专用数据库处理程序dba;
公共附录(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图myView=充气机。充气(R.layout.fragment\u add\u条目,容器,
假);
Savebtn=(ImageButton)myView.findviewbyd(R.id.Savebtn);
Savebtn.setBackgroundColor(Color.TRANSPARENT);
Savebtn.setOnClickListener(这个);
searchbutton=(按钮)myView.findViewById(R.id.searchbutton);
searchbutton.setOnClickListener(这个);
Cancelbtn=(ImageButton)myView.findViewById(R.id.Cancelbtn);
Cancelbtn.setBackgroundColor(颜色.透明);
Cancelbtn.setOnClickListener(此);
返回myView;
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
FoodET=(EditText)view.findViewById(R.id.foodEditText);
FoodET.setInputType(InputType.TYPE\u CLASS\u TEXT);
carorieet=(EditText)view.findViewById(R.id.carorieseditText);
热量表.设置输入类型(输入类型.类型\类别\编号);
foodET=((编辑文本)
view.findviewbyd(R.id.foodEditText)).getText().toString();
foodET.isEmpty();
FoodET.setText(“”);
热量表。设置文本(“”);
卡路里表=((编辑文本)
view.findviewbyd(R.id.caroriesedittext)).getText().toString();
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.SearchButton:
FoodSearch搜索=新的FoodSearch(foodET、Carloreet);
search.execute();
打破
案例R.id.SaveBtn:
if(FoodET.getText().toString().equals(null)|
CalorieET.getText().toString().equals(null)|
CalorieET.getText().toString().equals(“”)|124;
CalorieET.getText().toString().equals(“”){
Toast.makeText(getActivity(),“请输入信息”,
Toast.LENGTH_LONG).show();
AlertDialog NotFound=新建
AlertDialog.Builder(getContext()).create();
NotFound.setTitle(“错误”);
setMessage(“未找到食物:(”);
NotFound.setButton(AlertDialog.BUTTON_中性,“确定”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,int
(其中){
dialog.dismise();
}
});
}
其他的
((appMain)getActivity()).loadSelection(0);
打破
案例R.id.CancelBtn:
//编辑文本描述网=
(EditText)getView().findViewById(R.id.foodEditText);
//descriptionET.setText(“”);
//EditText卡路里表=
(EditText)getView().findViewById(R.id.CarrioreseditText);
//热量表。设置文本(“”);
((appMain)getActivity()).loadSelection(0);
打破
}
}
@凌驾
公共空间{
super.ondestory();
}
@凌驾
公共无效连接(){
super.onDetach();
}
私有类FoodSearch扩展了AsyncTask{
串食;
编辑文本卡路里;
食物搜索(字符串食物,编辑文本卡路里){
这就是食物;
这个。卡路里=卡路里;
}
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
食物=食物。全部替换(“,“%20”);
URL=新URL(“http://api.nal.usda.gov/ndb/search/?
format=JSON&q=“+food+
“&max=1&offset=0&sort=r&api_
key=xMJV33vSmKsquFqcBwZ23oJ7DlL2abmfsrDUUx1l);
HttpURLConnection urlConnection=(HttpURLConnection)
openConnection();
试一试{
BufferedReader BufferedReader=新的BufferedReader(新的
InputStreamReader(urlConnection.getInputStream());
StringBuilder StringBuilder=新的StringBuilder();
弦线;
而((line=bufferedReader.readLine())!=null){
stringBuilder.append(行).append(“\n”);
}
bufferedReader.close();
字符串结果=stringBuilder.toString();
if(result.contains(“零结果”)){
字符串s=“空”;
返回s;
}
JSONObject对象=(JSONObject)新建
JSONTokener(result).nextValue();
JSONObject list=object.getJSONObject(“list”);
JSONArray items=list.getJSONArray(“item”);
String item=items.get(0.toString();
int i=item.indexOf(“ndbno\:\”)+8;
int f=项的索引值(“\”,i);
字符串ndbno=项.子字符串(i,f);
Log.d(“调试”,ndbno);
URL url2=新URL(“http://api.nal.usda.gov/ndb/reports/?
ndbno=“+ndbno+
“&type=b&format=JSON&api_
key=xMJV33vSmKsquFqcBwZ23oJ7DlL2abmfsrDUUx1l);
HttpURLConnection urlConnection2=(HttpURLConnec
String FoodName = FoodET.getText().toString().trim();
String calString = CalorieET.getText().toString().trim();
foodET = ((EditText) view.findViewById(R.id.foodEditText)).getText().toString();
calorieET = ((EditText) view.findViewById(R.id.caloriesEditText)).getText().toString();
FoodSearch search = new FoodSearch(foodET, CalorieET );
case R.id.SearchButton:
String foodEtString=FoodET.getText().toString();
FoodSearch search = new FoodSearch(foodEtString, CalorieET );
search.execute();
break;
FoodET= (EditText)view.findViewById(R.id.foodEditText);
FoodET.setInputType(InputType.TYPE_CLASS_TEXT);
CalorieET=(EditText)view.findViewById(R.id.caloriesEditText);
CalorieET.setInputType(InputType.TYPE_CLASS_NUMBER);