gpt4 book ai didi

java - 在提交时传递自定义对象

转载 作者:行者123 更新时间:2023-12-01 11:53:58 25 4
gpt4 key购买 nike

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

我的 Activity :

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 注释,那么详细的教程和小示例也会非常有帮助。多谢。

最佳答案

您可以尝试以下代码:

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());
}
}

关于java - 在提交时传递自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com