From bdb356c3968538233a47bb7e2958a1c688a8bc34 Mon Sep 17 00:00:00 2001 From: unknown <1253209032@qq.com> Date: Mon, 28 May 2018 20:23:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?#5=20#899=20=E7=AC=AC=E4=BA=94=E6=AC=A1?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/hzu/myapplication/Add.java | 88 +++++++++++++++++++ .../hzu/myapplication/MyDatabaseHelper.java | 38 ++++++++ .../com/example/hzu/myapplication/Search.java | 77 ++++++++++++++++ .../example/hzu/myapplication/ShowSearch.java | 25 ++++++ .../app/src/main/res/layout/activity_add.xml | 77 ++++++++++++++++ .../layout/activity_my_database_helper.xml | 9 ++ .../src/main/res/layout/activity_search.xml | 36 ++++++++ .../main/res/layout/activity_show_search.xml | 43 +++++++++ 8 files changed, 393 insertions(+) create mode 100644 soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Add.java create mode 100644 soft1614080902146/app/src/main/java/com/example/hzu/myapplication/MyDatabaseHelper.java create mode 100644 soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Search.java create mode 100644 soft1614080902146/app/src/main/java/com/example/hzu/myapplication/ShowSearch.java create mode 100644 soft1614080902146/app/src/main/res/layout/activity_add.xml create mode 100644 soft1614080902146/app/src/main/res/layout/activity_my_database_helper.xml create mode 100644 soft1614080902146/app/src/main/res/layout/activity_search.xml create mode 100644 soft1614080902146/app/src/main/res/layout/activity_show_search.xml diff --git a/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Add.java b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Add.java new file mode 100644 index 000000000..1db8dde94 --- /dev/null +++ b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Add.java @@ -0,0 +1,88 @@ +package com.example.administrator.myapplication; +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Handler; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +public class Add extends AppCompatActivity { + + private MyDatabaseHelper dbHelper; + + String str1,str2; + + public static final String CREATE_INFO = "create table textinfo(" + + //primary key 将id列设为主键 autoincrement表示id列是自增长的 + "id integer primary key autoincrement," + + "name text," + + "description text)"; + @Override + + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_add); + + + EditText text_nameinfo=(EditText)findViewById(R.id.nameText); + + text_nameinfo.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + str1 = s.toString(); + Log.d("EditText",str1); + } + }); + + EditText text_description=(EditText)findViewById(R.id.descriptionText); + text_description.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + str2=s.toString(); + Log.d("EditText",str2); + } + }); + //数据库建立 + dbHelper = new MyDatabaseHelper(this,"cloth",null,1); + dbHelper.getWritableDatabase(); + //上传确认 + Button btn1 =(Button)findViewById(R.id.confirmUpload); + btn1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SQLiteDatabase db=dbHelper.getWritableDatabase(); + //db.execSQL(CREATE_INFO); + //ContentValues values=new ContentValues(); + db.execSQL("insert into textinfo(name,description) values(?,?)", + new Object[]{str1,str2}); + } + }); + } +} \ No newline at end of file diff --git a/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/MyDatabaseHelper.java b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/MyDatabaseHelper.java new file mode 100644 index 000000000..1ec2cddfd --- /dev/null +++ b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/MyDatabaseHelper.java @@ -0,0 +1,38 @@ +package com.example.administrator.myapplication; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.widget.Toast; + +public class MyDatabaseHelper extends SQLiteOpenHelper { + + public static final String CREATE_INFO = "create table textinfo(" + + //primary key 将id列设为主键 autoincrement表示id列是自增长的 + "id integer primary key autoincrement," + + "name text," + + "description text)"; + + + + private Context mContext; + + //构造方法:第一个参数Context,第二个参数数据库名,第三个参数cursor允许我们在查询数据的时候返回一个自定义的光标位置,一般传入的都是null,第四个参数表示目前库的版本号(用于对库进行升级) + public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory , int version){ + super(context,name ,factory,version); + mContext = context; + } + + @Override + public void onCreate(SQLiteDatabase db) { + //调用SQLiteDatabase中的execSQL()执行建表语句。 + db.execSQL(CREATE_INFO); + //创建成功 + Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show(); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } +} \ No newline at end of file diff --git a/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Search.java b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Search.java new file mode 100644 index 000000000..096d50a58 --- /dev/null +++ b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/Search.java @@ -0,0 +1,77 @@ +package com.example.administrator.myapplication; +import android.content.Intent; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +public class Search extends AppCompatActivity { + + MyDatabaseHelper dbHelper=new MyDatabaseHelper(Search.this,"cloth",null,1); + + String str; + String table = "textinfo" ; + String[] columns = new String[] {"name","description"}; + String selection = "name=?" ; + String groupBy = null ; + String having = null ; + String orderBy = null ; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search); + + EditText SearchText=(EditText)findViewById(R.id.SearchText); + SearchText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + str=s.toString(); + } + }); + + Button btn1=(Button)findViewById(R.id.ShowAll); + btn1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + //指明去查询textinfo表。 + Cursor cursor = db.query(table,columns,selection,new String[]{str},groupBy,having,orderBy); + //Cursor cursor = db.query(table,columns,null,null,groupBy,having,orderBy); + //调用moveToFirst()将数据指针移动到第一行的位置。 + if (cursor.moveToFirst()){ + do { + //然后通过Cursor的getColumnIndex()获取某一列中所对应的位置的索引 + String name=cursor.getString(cursor.getColumnIndex("name")); + String description = cursor.getString(cursor.getColumnIndex("description")); + Log.d("MainActivity","name is "+name); + Log.d("MainActivity","description is "+description); + //Toast.makeText(Search.this, name+description, Toast.LENGTH_LONG).show(); + Intent intent=new Intent(Search.this,ShowSearch.class); + intent.putExtra("name",name); + intent.putExtra("description",description); + startActivity(intent); + }while(cursor.moveToNext()); + } + cursor.close(); + } + }); + } +} \ No newline at end of file diff --git a/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/ShowSearch.java b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/ShowSearch.java new file mode 100644 index 000000000..4b2998146 --- /dev/null +++ b/soft1614080902146/app/src/main/java/com/example/hzu/myapplication/ShowSearch.java @@ -0,0 +1,25 @@ +package com.example.administrator.myapplication; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.widget.TextView; + +public class ShowSearch extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_show_search); + + Intent intent=getIntent(); + String string1=intent.getStringExtra("name"); + String string2=intent.getStringExtra("description"); + + TextView textName=(TextView)findViewById(R.id.nameShow); + textName.setText(string1); + TextView textDescription=(TextView)findViewById(R.id.descriptionShow); + textDescription.setText(string2); + } +} \ No newline at end of file diff --git a/soft1614080902146/app/src/main/res/layout/activity_add.xml b/soft1614080902146/app/src/main/res/layout/activity_add.xml new file mode 100644 index 000000000..8cb93a5a8 --- /dev/null +++ b/soft1614080902146/app/src/main/res/layout/activity_add.xml @@ -0,0 +1,77 @@ + + +263/】 + + + + + + + + + + + + + + + + + + + + +