-2

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

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

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

由于在键下设置的值"Price"是一个字符串,并且我的 logcatEditText在我使用时返回 an myString,这让我相信我使用的是 anEditText而不是 aString即使我已经查找了多个教程/答案,所有这些都是为了从您必须使用的编辑文本中获取字符串:

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

我在我的代码中有。

另外,我上面有两个SearchViews 和两个ListViews 具有搜索功能,所以我的代码有点长。我的代码根本没有出错,除了这个小问题之外它工作得很好。

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>

感谢您的帮助!

4

1 回答 1

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()现在的方法中。如果这不是您想要实现的目标,那么您将不得不编辑您的问题以使其更清晰。

于 2013-03-23T21:49:49.540 回答