Skip to content

Commit

Permalink
Merge pull request #2437 from ZhuHongen/master
Browse files Browse the repository at this point in the history
#5 #899 第五次实验
  • Loading branch information
zengsn authored Jun 2, 2018
2 parents 51b2b4d + f2bbbed commit 2b92859
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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});
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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) {

}
}
Original file line number Diff line number Diff line change
@@ -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();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
77 changes: 77 additions & 0 deletions soft1614080902146/app/src/main/res/layout/activity_add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:layout_weight="3"
android:orientation="vertical"
tools:context=".Add">
263/】
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻标题" />

<EditText
android:id="@+id/nameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻内容" />

<EditText
android:id="@+id/descriptionText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" />

<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图片上传" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">

<ImageView
android:id="@+id/upload"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
app:srcCompat="@drawable/upload" />


</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/confirmUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="确认上传" />
</LinearLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MyDatabaseHelper">

</android.support.constraint.ConstraintLayout>
36 changes: 36 additions & 0 deletions soft1614080902146/app/src/main/res/layout/activity_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".Search">

<EditText
android:id="@+id/SearchText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="162dp"
android:ems="10"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="230dp"
android:text="请输入关键词" />

<Button
android:id="@+id/ShowAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="显示" />
</RelativeLayout>
Loading

0 comments on commit 2b92859

Please sign in to comment.