在提交时传递自定义对象

时间:2015-02-18 10:19:17

标签: java android

我正在开发Android应用程序。我主要在Spring上工作,所以不太了解android开发,所以我安装了android注释和Spring Rest模板来连接服务器来发送数据。 目前我正在尝试做的是,在android应用程序中填写一些文本字段,变量在一个Java文件中提到。然后单击一个按钮,我想将该对象传递给其中一个方法,在那里我可以与服务器建立REST连接以发送数据。请仔细看看代码,并告诉我们:

MyActivity:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    // At button push, this method is called, I checked that, but I would like to receive an Object of class mentioned below. 
    public void sendMessage(View view){
        Log.d(view.toString(),"We reached sendMessage");
    }
}

餐厅类:

public class Restaurant {

    private int id;

    private String username;

    private String password;

    private String firstName;
//Getters and setters ommitted
}

布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ScrollView01"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:weightSum="1" android:gravity="center"
              android:visibility="visible">

    <TextView
            android:layout_width="193dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Text"
            android:id="@+id/textView" android:textStyle="italic" android:gravity="center_horizontal"
            android:layout_marginTop="20dp" android:textSize="30dp"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Bitte geben Sie Ihre Daten ein"
            android:id="@+id/textView2" android:layout_marginTop="20dp"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText" android:layout_marginTop="20dp"
            android:gravity="center_horizontal" android:autoText="false" android:hint="Restaurant Name"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText4" android:layout_gravity="center_horizontal" android:gravity="center_horizontal"
            android:hint="Restaurant Email" android:layout_marginTop="20dp"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText5" android:layout_gravity="center_horizontal" android:hint="Password"
            android:gravity="center_horizontal" android:layout_marginTop="20dp"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText2" android:layout_gravity="center_horizontal" android:editable="false"
            android:layout_marginTop="20dp" android:hint="Street name, number" android:gravity="center_horizontal"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText6" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp"
            android:gravity="center_horizontal" android:hint="Phone number"/>
    <EditText
            android:layout_width="310dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText3" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp"
            android:hint="@string/PLZ" android:gravity="center_horizontal"/>
    <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Standort ermitteln"
            android:id="@+id/button" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp"
            android:onClick="sendMessage"/>
</LinearLayout>
</ScrollView>

如果有人有用户Spring-rest-template和Android注释,那么详细的教程那么小的例子也会非常有用。非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

public class MyActivity extends Activity {
    EditText editText = (EditText) findViewById(R.Id.restaurantName); 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    // At button push, this method is called, I checked that, but I would like to receive an Object of class mentioned below. 
    public void sendMessage(View view){
        Log.d(view.toString(),editText.getText().toString());
    }
}