2013-03-23 53 views
-2

我试图让用户用价格填写我的EditText ...即"20.00",并将该编辑文本的值作为字符串获取。然后使用该字符串作为数据上传到“我的服务器”a.k.a parse.com下的"Price"密钥下。每当我运行我的模拟器时,在编辑文本中填写"20.00",并检查我的服务器一个新条目永远不会弹出。我的logcat返回:将EditText转换为字符串,但仍然返回LogCat上的EditText

03-23 21:14:20.607: V/EditText(1697): 20.00 

如果我在上面创建另一个字符串,只需给它一个值。然后把它放在"Price"的密钥下,而不是myString,然后运行模拟器,我的服务器会收到这个,一切都会运行。

由于下方的按键"Price"设置的值是一个字符串,我logcat的是返回一个EditText每当我使用myString,这是导致我相信,我使用的是EditText代替String即使我已经看过了多个教程/答案,所有说,为了从编辑文本中获得字符串,你必须使用:

price = (EditText) findViewById(R.id.editText1); 
String newString = price.getText().toString(); 

我在我的代码中。

此外,我有两个SearchView s和两个ListView上面有搜索功能,所以我的代码是有点冗长。我的代码根本不会出错,除了这个小小的打嗝外,它还能正常工作。

TapDeal.java - 问题类:

package com.alpha.dealtap; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.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:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- Editext for Search --> 

    <EditText 
     android:id="@+id/inputSearch" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Brand of Alcohol" 
     android:inputType="textVisiblePassword" /> 

    <!-- List View --> 

    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="fill_parent" 
     android:layout_height="52dp" /> 

    <EditText 
     android:id="@+id/inputSearch2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:hint="Enter the Size" 
     android:inputType="textVisiblePassword" /> 

    <ListView 
     android:id="@+id/list_view2" 
     android:layout_width="fill_parent" 
     android:layout_height="54dp" 
     android:layout_weight="0.16" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:ems="10" 
     android:hint="Enter the Price" 
     android:inputType="numberDecimal" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="90dp" 
     android:text="Tap it" 
     android:textSize="23dp" /> 

</LinearLayout> 

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.alpha.dealtap" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".Main" 
      android:label="@string/app_name" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".Search_Page" 
      android:label="@string/app_name" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.SEARCH_PAGE" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".DealPage" 
      android:label="@string/app_name" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.DEALPAGE" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".StorePage" 
      android:label="@string/app_name" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.STOREPAGE" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".Map" 
      android:label="@string/app_name" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.MAP" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".TapDeal" 
      android:label="TapDeal" 
      android:windowSoftInputMode="stateHidden" > 

      <intent-filter> 

       <action android:name="com.alpha.dealtap.TAPDEAL" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

谢谢您的帮助!

回答

2

从你的问题来看,你的问题并不完全清楚发生了什么以及你期望发生什么,但是你的监听器的onClick()方法只输出到日志似乎很奇怪。我的猜测是,你希望所有这些代码:

String newString = price.getText().toString(); 

ParseObject dealinfo = new ParseObject("Deals"); 
dealinfo.put("Brand", "Budweiser"); 
dealinfo.put("Size", "6"); 
dealinfo.put("Price", newString); 
dealinfo.saveInBackground(); 
onClick()方法

,以便它发生在当按钮被点击,而不是在onCreate()方法,因为它是现在。如果这不是你想要达到的目标,那么你将不得不编辑你的问题,使其更清晰。

+0

工作完美....对不起,如果我不清楚,我是全新的Android/Java – 2013-03-24 00:26:35

相关问题