java - 无法在回收器 View 上接受 float 输入

标签 java android android-recyclerview

我正在开发一个评论应用程序,在提交评论时,评论者必须包含餐厅的坐标,然后该坐标将填充 map 。

但是我正在努力解决如何接受浮点输入,字符串输入工作正常。我在下面的代码中突出显示了我遇到困难的地方,我尝试使用“Float.valueOf”,但这导致我的应用程序崩溃。

感谢任何帮助,谢谢。

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_new_note, container, false);
    mAttractionName = view.findViewById(R.id.Submit_attractionName);
    mReview = view.findViewById(R.id.Submit_review);

    mAttractionAddress = 
    view.findViewById(R.id.Submit_attraction_Address);
    mCuisine = view.findViewById(R.id.Submit_Cuisine);
    mLat = view.findViewById(R.id.Submit_latitude);
    mLong = view.findViewById(R.id.Submit_longitude);
    mCreate = view.findViewById(R.id.create);
    mCancel = view.findViewById(R.id.cancel);

    mCancel.setOnClickListener(this);
    mCreate.setOnClickListener(this);

    getDialog().setTitle("New Attraction");

    return view;
   }

    @Override
   public void onClick(View view) {
    switch (view.getId()){

        case R.id.create:{

            // insert the new note

            String attractionName = mAttractionName.getText().toString();
            String review = mReview.getText().toString();
            String attractionAddress = enter code 
            heremAttractionAddress.getText().toString();
            String cuisine = mCuisine.getText().toString();
            Float latitude = mLat.getText().toString(); (ISSUES HERE)
            Float longitude = mLong.getText().toString();



            if(!attractionName.equals("")){
                mSUBMITACTIVITY.createNewNote(attractionName, re`enter 
        code here`view, cuisine, attractionAddress, latitude, longitude);
                getDialog().dismiss();
            }
            else{
                Toast.makeText(getActivity(), "Enter an Attraction", 
         Toast.LENGTH_SHORT).show();
            }
            break;
        }

        case R.id.cancel:{
            getDialog().dismiss();
            break;
          }
       }
   }

EditText 的 XML

<android.support.design.widget.TextInputLayout
    android:id="@+id/content_wrapper4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/Submit_latitude"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/note_latitude"
        android:imeOptions="actionNext"
        android:inputType="numberDecimal|numberSigned" />
  </android.support.design.widget.TextInputLayout>

   <android.support.design.widget.TextInputLayout
    android:id="@+id/content_wrapper5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/Submit_longitude"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/note_longitude"
        android:imeOptions="actionNext"
        android:inputType="numberDecimal|numberSigned" />

最佳答案

String latitude = mLat.getText().toString();
float f = Float.parseFloat(latitude);

您必须以字符串形式输入,然后将其转换。

关于java - 无法在回收器 View 上接受 float 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879897/

相关文章:

java - 用于实现嵌套 map 之类的数据结构?

java - Android 原子提要

java - 安卓打开失败: EACCES (Permission denied) when try to read a file in data/data

java - 具有自定义数据源的 Android 分页库在 RecyclerView 中偏移项目

android - WearableRecyclerView .scrollToPosition 不适用于磨损?

android - 如何在 RecyclerView 中使用 LoadMore?

java - 如何在java中打印循环列表中的数据?

java - 使用 JasperReports 的最佳方法是什么?

java - 将外部 JAR 添加到 OpenJDK

android - 适用于 Android 的 Actionscript : How to store "default data" in sql? 有什么意义?