Android Studioで家計簿アプリを作成しています。
activity_main.xmlから日付の値を受け取り、dtoから日付(Date型)の値をMainActivityに渡したいのですが、MainActivityの47行目での「txtDate.getText().toString()」の部分で下記エラーが発生してしまいました。
Required type:
Date
Provided:
String
この場合、Date型をString型に変換するなどの処理が必要でしょうか。
上記エラー解決の為、ご助力頂けますと幸いです。
※まだ作成途中の為、コード自体は中途半端な状態です。
お手数ですが、ご回答の程よろしくお願いいたします。
MainActivity.java
1package com.example.budgettrackerapptest1; 2 3import androidx.annotation.RequiresApi; 4import androidx.appcompat.app.AppCompatActivity; 5import androidx.recyclerview.widget.RecyclerView; 6 7import android.os.Build; 8import android.os.Bundle; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.Toast; 13 14import java.text.DateFormat; 15import java.text.ParseException; 16import java.text.SimpleDateFormat; 17import java.time.LocalDate; 18import java.time.format.DateTimeFormatter; 19import java.util.Date; 20import java.util.Locale; 21 22public class MainActivity extends AppCompatActivity { 23 24 Button btnViewAll, btnSelect, btnAdd, btnUpdate; 25 EditText txtDate, txtStoreName, txtProductName, txtType, txtPrice; 26 RecyclerView rv_list; 27 BudgetTrackerDao budgetTrackerDao; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 btnViewAll = findViewById(R.id.btnViewAll); 35 btnSelect = findViewById(R.id.btnSelect); 36 btnAdd = findViewById(R.id.btnAdd); 37 btnUpdate = findViewById(R.id.btnUpdate); 38 txtDate = findViewById(R.id.txtDate); 39 txtStoreName = findViewById(R.id.txtStoreName); 40 txtProductName = findViewById(R.id.txtProductName); 41 txtType = findViewById(R.id.txtType); 42 txtPrice = findViewById(R.id.txtPrice); 43 rv_list = findViewById(R.id.rv_list); 44 budgetTrackerDao = new BudgetTrackerDao(MainActivity.this); 45 46 //ShowCustomerOnListView(dataBaseHelper); need to create a new method on this class 47 48 btnAdd.setOnClickListener(new View.OnClickListener() { 49 50 @Override 51 public void onClick(View v) { 52 BudgetTrackerDto budgetTrackerDto; 53 DateFormat txtDateFormat = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); 54 Date txtDateStr = null; 55 try { 56 txtDateStr = txtDateFormat.parse(String.valueOf(txtDate)); 57 } catch (ParseException e) { 58 e.printStackTrace(); 59 } 60 61 62 try { 63 budgetTrackerDto = new BudgetTrackerDto(txtDateStr.getTime(), txtStoreName.getText().toString(), txtProductName.getText().toString(),txtType.getText().toString(), Integer.parseInt(txtPrice.getText().toString())); 64 Toast.makeText(MainActivity.this, budgetTrackerDto.toString(), Toast.LENGTH_SHORT).show(); 65 } catch (Exception e) { 66 Toast.makeText(MainActivity.this, "Error adding date", Toast.LENGTH_SHORT).show(); 67 68 } 69 //作成途中 70 71 } 72 }); 73 74 75 } 76}
BudgetTrackerDto.java
1package com.example.budgettrackerapptest1; 2 3import java.sql.Date; 4 5public class BudgetTrackerDto { 6 private Date date; 7 private String storeName; 8 private String productName; 9 private String productType; 10 private int price; 11 12 public BudgetTrackerDto (long date, String storeName, String productName, String productType, int price) { 13 this.date = date; 14 this.storeName = storeName; 15 this.productName = productName; 16 this.productType = productType; 17 this.price = price; 18 } 19 20 21 @Override 22 public String toString() { 23 return "BudgetTrackerDto{" + 24 "date=" + date + 25 ", storeName='" + storeName + '\'' + 26 ", productName='" + productName + '\'' + 27 ", productType='" + productType + '\'' + 28 ", price=" + price + 29 '}'; 30 } 31 32 public Date getDate() { 33 return date; 34 } 35 36 public void setDate(Date date) { 37 this.date = date; 38 } 39 40 public String getStoreName() { 41 return storeName; 42 } 43 44 public void setStoreName(String storeName) { 45 this.storeName = storeName; 46 } 47 48 public String getProductName() { 49 return productName; 50 } 51 52 public void setProductName(String productName) { 53 this.productName = productName; 54 } 55 56 public String getProductType() { 57 return productType; 58 } 59 60 public void setProductType(String productType) { 61 this.productType = productType; 62 } 63 64 public int getPrice() { 65 return price; 66 } 67 68 public void setPrice(int price) { 69 this.price = price; 70 } 71} 72
BudgetTrackerDao.java
1package com.example.budgettrackerapptest1; 2 3import android.content.Context; 4import android.database.sqlite.SQLiteDatabase; 5import android.database.sqlite.SQLiteOpenHelper; 6 7import androidx.annotation.Nullable; 8 9public class BudgetTrackerDao extends SQLiteOpenHelper { 10 11 public BudgetTrackerDao(@Nullable Context context) { 12 super(context, "budgetTrackerSample1.db", null, 1); 13 } 14 15 @Override 16 public void onCreate(SQLiteDatabase db) { 17 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 22 23 } 24}
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" 13 tools:layout_editor_absoluteX="157dp" 14 tools:layout_editor_absoluteY="52dp"> 15 16 <EditText 17 android:id="@+id/txtDate" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:ems="10" 21 android:hint="Date" 22 android:inputType="date" /> 23 24 <EditText 25 android:id="@+id/txtStoreName" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:ems="10" 29 android:hint="Store Name" 30 android:inputType="textPersonName" /> 31 32 <EditText 33 android:id="@+id/txtProductName" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:ems="10" 37 android:hint="Product Name" 38 android:inputType="textPersonName" /> 39 40 <EditText 41 android:id="@+id/txtType" 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:ems="10" 45 android:hint="Product Type" 46 android:inputType="textPersonName" /> 47 48 <EditText 49 android:id="@+id/txtPrice" 50 android:layout_width="match_parent" 51 android:layout_height="wrap_content" 52 android:ems="10" 53 android:hint="Price" 54 android:inputType="number" /> 55 56 <LinearLayout 57 android:layout_width="match_parent" 58 android:layout_height="wrap_content" 59 android:orientation="horizontal"> 60 61 <Button 62 android:id="@+id/btnViewAll" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:layout_margin="6dp" 66 android:layout_weight="1" 67 android:text="View All" /> 68 69 <Button 70 android:id="@+id/btnSelect" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:layout_margin="6dp" 74 android:layout_weight="1" 75 android:text="Select" /> 76 77 <Button 78 android:id="@+id/btnAdd" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:layout_margin="6dp" 82 android:layout_weight="1" 83 android:text="Add Item" /> 84 85 <Button 86 android:id="@+id/btnUpdate" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:layout_margin="6dp" 90 android:layout_weight="1" 91 android:text="Update" /> 92 </LinearLayout> 93 94 <androidx.recyclerview.widget.RecyclerView 95 android:id="@+id/rv_list" 96 android:layout_width="match_parent" 97 android:layout_height="match_parent" /> 98 </LinearLayout> 99</androidx.constraintlayout.widget.ConstraintLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/04 04:40 編集
2021/05/04 05:37
2021/05/04 21:55 編集
2021/05/05 01:55