2013-03-23 27 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

相關問題