java将EditText转换为字符串,但仍在LogCat上返回EditText
我正试图让一个用户在我的EditText
中填写一个价格。。。i、 e."20.00"
并从编辑文本中以字符串形式获取值。然后将该字符串用作数据,作为值上载到“我的服务器”a.k.a解析。com在"Price"
键下。每当我运行emulator时,在编辑文本中填写"20.00"
,然后检查我的服务器,一个新条目永远不会弹出。我的logcat返回:
03-23 21:14:20.607: V/EditText(1697): 20.00
如果我在上面创建另一个字符串并简单地给它一个值。然后将它放在"Price"
键下而不是myString
并运行模拟器,我的服务器将收到此消息,一切都将正常运行
由于在"Price"
键下设置的值是一个字符串,并且每当我使用myString
时,我的logcat都返回一个EditText
,这让我相信我使用的是EditText
而不是String
,尽管我已经查找了多个教程/答案,它们都说,为了从编辑文本中获取字符串,您必须使用:
price = (EditText) findViewById(R.id.editText1);
String newString = price.getText().toString();
我的代码里有
另外,我上面有两个SearchView
和两个ListView
具有搜索功能,因此我的代码有点长。我的代码一点也不出错,除了这个小问题之外,它工作得很好
TapDeal.java
-问题类别:
package com.alpha.dealtap;
import java.util.ArrayList;
import java.util.HashMap;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.text.Editable;
import 安卓.text.TextWatcher;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ListView;
import com.parse.Parse;
import com.parse.ParseObject;
public class TapDeal extends Activity {
Button b1;
String newString;
// List view
private ListView lv;
private ListView lv2;
// Listview Adapter
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter2;
// Search EditText
EditText inputSearch;
EditText inputSearch2;
EditText price;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
ArrayList<HashMap<String, String>> productList2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tapdeal);
// Listview Data
String products[] = { "Dubra", "Keystone Light", "Keystone",
"Smirnoff", "Jack Daniels", "Captain Morgan", "Grey Goose",
"Burnetts", "Kettle One", "Corona", "Franzia", "Budweiser" };
String size[] = { "6 Pack", "12 Pack", "30 Pack", "750ml", "Handle",
"1 liter", "3 Liter Box", "Half Pint", "1 Pint" };
lv = (ListView) findViewById(R.id.list_view);
lv2 = (ListView) findViewById(R.id.list_view2);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch2 = (EditText) findViewById(R.id.inputSearch2);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.listitem,
R.id.product_name, products);
adapter2 = new ArrayAdapter<String>(this, R.layout.size, R.id.size,
size);
lv.setAdapter(adapter);
lv2.setAdapter(adapter2);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
TapDeal.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
inputSearch2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
TapDeal.this.adapter2.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
b1 = (Button) findViewById(R.id.button1);
price = (EditText) findViewById(R.id.editText1);
String newString = price.getText().toString();
Parse.initialize(this, "xxxx", "yyyy");
ParseObject dealinfo = new ParseObject("Deals");
dealinfo.put("Brand", "Budweiser");
dealinfo.put("Size", "6");
dealinfo.put("Price", newString);
dealinfo.saveInBackground();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText", price.getText().toString());
}
});
}
}
("xxxx"
和"yyyy"
是我的私钥)
tapdeal.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical" >
<!-- Editext for Search -->
<EditText
安卓:id="@+id/inputSearch"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:hint="Brand of Alcohol"
安卓:inputType="textVisiblePassword" />
<!-- List View -->
<ListView
安卓:id="@+id/list_view"
安卓:layout_width="fill_parent"
安卓:layout_height="52dp" />
<EditText
安卓:id="@+id/inputSearch2"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dp"
安卓:hint="Enter the Size"
安卓:inputType="textVisiblePassword" />
<ListView
安卓:id="@+id/list_view2"
安卓:layout_width="fill_parent"
安卓:layout_height="54dp"
安卓:layout_weight="0.16" />
<EditText
安卓:id="@+id/editText1"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="50dp"
安卓:ems="10"
安卓:hint="Enter the Price"
安卓:inputType="numberDecimal" />
<Button
安卓:id="@+id/button1"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="90dp"
安卓:text="Tap it"
安卓:textSize="23dp" />
</LinearLayout>
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.alpha.dealtap"
安卓:versionCode="1"
安卓:versionName="1.0" >
<uses-sdk
安卓:minSdkVersion="8"
安卓:targetSdkVersion="15" />
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE" />
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name=".Main"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name=".Search_Page"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.SEARCH_PAGE" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
安卓:name=".DealPage"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.DEALPAGE" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
安卓:name=".StorePage"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.STOREPAGE" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
安卓:name=".Map"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.MAP" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
安卓:name=".TapDeal"
安卓:label="TapDeal"
安卓:windowSoftInputMode="stateHidden" >
<intent-filter>
<action 安卓:name="com.alpha.dealtap.TAPDEAL" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
谢谢你的帮助
# 1 楼答案
您的问题并不完全清楚正在发生什么以及您期望发生什么,但是您的侦听器的
onClick()
方法只输出到日志,这似乎很奇怪。我猜您需要所有这些代码:在
onClick()
方法中,使其在单击按钮时发生,而不是像现在那样在onCreate()
方法中发生。如果这不是你想要达到的目标,那么你必须编辑你的问题,使其更加清晰