花了点时间写了一个入门级数据读写操作,是kotlin版本。教程没有涉及json的第三方解析,没有MVVM模式操作数据库,所以代码很适合了解手机开发的app是如何操作数据库的入门者阅读。
教程是采用android studio最新版下编写的,项目是用底部导航栏的系统默认模板搭建的,很容易阅读和理解。写入是通过远程php的接口写入到数据库的没有用kotlin实现,因为用php实现后端数据库操作是非常有优势的,编写代码快捷省事。完整源码打包到 专业开发网 http://www.zhuanyekaifa.com/datarwite.rar 去下载吧
看几个效果图:
项目结构图:红框部分是着重改写的部分
2 , 布局文件
xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=".ui.dashboard.DashboardFragment"> android:id="@+id/personname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="130dp" android:layout_marginEnd="24dp" android:ems="10" android:inputType="textPersonName" android:text="输入姓名" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/personage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:ems="10" android:inputType="number" android:text="输入年龄" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/personname" /> android:id="@+id/personadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:ems="10" android:inputType="textPersonName" android:text="输入住址" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/personage" /> android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:src="@android:drawable/ic_input_add" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/personadd" /> 3.数据请求,主要是看是如何请求远程数据的部分 package com.mykotlin.five_adddata.ui.dashboard import android.os.Bundle import android.os.Looper import android.text.Editable import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.mykotlin.five_adddata.databinding.FragmentDashboardBinding import okhttp3.* import org.json.JSONObject import java.io.IOException class DashboardFragment : Fragment() { private var dashboardViewModel: DashboardViewModel? = null private var _binding: FragmentDashboardBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { dashboardViewModel = ViewModelProvider(this).get(DashboardViewModel::class.java) _binding = FragmentDashboardBinding.inflate(inflater, container, false) val root: View = binding.root val pname = binding.personname.text val page = binding.personage.text val padd=binding.personadd.text binding.imageButton.setOnClickListener { getAdd(pname,page,padd) } return root } private fun getAdd(name: Editable, age: Editable, add: Editable){ **val client = OkHttpClient() var url = "http://www.kuaidian777.com/mydata.php" val urlAPI: String = url** val builder = FormBody.Builder() builder.add("hname", name.toString()) builder.add("hage", age.toString()) builder.add("hadd", add.toString()) val formBody = builder.build() val request = Request.Builder() .method("POST", formBody) .url(urlAPI).build() client.newCall(request).enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { val result = response.body?.string() var jstr="" var myjson= JSONObject(result) //println("result:" + myjson) if(myjson.getString("msg")=="ok") { Looper.prepare(); Toast.makeText(context, "输入写入成功", Toast.LENGTH_SHORT).show() Looper.loop(); }else { Looper.prepare(); Toast.makeText(context, "唉,失败了", Toast.LENGTH_SHORT).show() Looper.loop(); //println(myjson.getString("msg")) } } override fun onFailure(call: Call, e: IOException) { println("Failed request api :( " + e.message) } }) } override fun onDestroyView() { super.onDestroyView() _binding = null } } 4.点击 +按钮后 把数据写入数据库,通过调用远程http://www.kuaidian777.com/mydata.php写入的 接口内容 header('Content-Type:application/json; charset=utf-8'); $mysql_server_name = 'localhost'; //改成自己的mysql数据库服务器 $mysql_username = '。。。。。'; //改成自己的mysql数据库用户名 $mysql_password = '。。。。。'; //改成自己的mysql数据库密码 $mysql_database = '。。。。。'; //改成自己的mysql数据库名 $conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //连接数据库 //连接数据库错误提示 if (mysqli_connect_errno($conn)) { die("连接 MySQL 失败: " . mysqli_connect_error()); } mysqli_query($conn,"set names utf8"); //数据库编码格式 $hname=$_POST["hname"]; $hage=$_POST["hage"];; $hadd=$_POST["hadd"];; $sql="INSERT INTO `testme`(`hname`, `hage`, `hadd`) VALUES ('".$hname."',".$hage.",'".$hadd."')"; mysqli_query($conn,$sql); ?> 技术探讨 : qq 2047879076