Step 1:效果图
Step 2目录结构:
Step 3:AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sqlitedemo">
<application
//注意此处
android:name=".Myapp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity android:name=".ShowMainActivity"/>
<activity android:name=".UpdateMainActivity"/>
<activity android:name=".RegistActivity"/>
<activity android:name=".SendActivity"/>
</application>
</manifest>
Step 4:所有布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#018BD4">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="登入"
android:textSize="20sp"
android:gravity="center"
android:textColor="#fff"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入用户名"
android:id="@+id/uesrname"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/input_edit"
android:paddingLeft="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入密码"
android:id="@+id/password"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_edit"
android:paddingLeft="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="40dp"
android:text="立即登入"
android:background="@drawable/input_button"
android:layout_gravity="center"
android:id="@+id/loginbtn"
android:textColor="#ffffff"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:text="立即注册"
android:background="@drawable/input_button"
android:layout_gravity="center"
android:id="@+id/regbtn"
android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#018BD4">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="新闻中心"
android:textSize="20sp"
android:gravity="center"
android:paddingLeft="60dp"
android:textColor="#fff"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="发布"
android:id="@+id/sendbtn"
android:background="#018BD4"
android:textColor="#fff"/>
</LinearLayout>
<ListView
android:divider="#666"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#018BD4"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="注册"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
<EditText
android:id="@+id/reg_username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="50dp"
android:background="@drawable/input_edit"
android:hint="请输入用户名"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/reg_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_edit"
android:hint="请输入密码"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/reg_age"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_edit"
android:hint="请输入年龄"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/reg_sex"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_edit"
android:hint="请输入性别"
android:paddingLeft="10dp" />
<Button
android:id="@+id/reg_ok_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="50dp"
android:background="@drawable/input_button"
android:text="立即注册"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="发布新闻"
android:gravity="center"
android:background="#018BD4"
android:textColor="#fff"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入新闻标题"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:id="@+id/edit_title"
android:background="@null"
android:paddingLeft="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="1dp"
android:background="#018BD4"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入新闻内容"
android:paddingLeft="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:id="@+id/edit_content"
android:background="@null"/>
<TextView
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="1dp"
android:background="#018BD4"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="50dp"
android:textColor="#fff"
android:background="@drawable/input_button"
android:id="@+id/sendNews_btn"
android:text="立即发布"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#018BD4"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="新闻详情"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="新闻标题:"
android:textSize="20sp" />
<TextView
android:id="@+id/info_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="新闻内容:"
android:textSize="20sp" />
<TextView
android:id="@+id/info_content"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="新闻时间:"
android:textSize="20sp" />
<TextView
android:id="@+id/info_time"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="新闻作者:"
android:textSize="20sp" />
<TextView
android:id="@+id/info_author"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="修改新闻"
android:gravity="center"
android:background="#018BD4"
android:textColor="#fff"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入新闻标题"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:id="@+id/update_title"
android:background="@null"
android:paddingLeft="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="1dp"
android:background="#018BD4"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入新闻内容"
android:paddingLeft="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:id="@+id/update_content"
android:background="@null"/>
<TextView
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="1dp"
android:background="#018BD4"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="50dp"
android:textColor="#fff"
android:background="@drawable/input_button"
android:id="@+id/updateNews_btn"
android:text="修改"
android:gravity="center"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"
android:padding="10dp"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/item_username"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="用户"
android:textSize="20sp" />
<TextView
android:gravity="left"
android:paddingRight="20dp"
android:id="@+id/item_time"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Step 4:所有java文件
package com.example.sqlitedemo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 登入类
*/
public class LoginActivity extends Activity {
private EditText username, password;
private Button loginbtn, regbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.uesrname);
password = (EditText) findViewById(R.id.password);
loginbtn = (Button) findViewById(R.id.loginbtn);
regbtn = (Button) findViewById(R.id.regbtn);
regbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//跳转至注册界面
startActivity(new Intent(LoginActivity.this, RegistActivity.class));
}
});
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 登入
String un = username.getText().toString();
String pw = password.getText().toString();
if (TextUtils.isEmpty(un)) {
Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(pw)) {
Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
return;
} else {
//查询user表中是否存在该账号和密码
SQLiteDatabase db = Myapp.db.getReadableDatabase();
//查询的语句 条件是名字和密码
String sql = "select * from user where username= '" + un + "' and password='" + pw + "'";
db.rawQuery(sql, null);
Cursor cursor = db.rawQuery(sql, null);
if (cursor.getCount() > 0) {
Myapp.username = un;
Toast.makeText(LoginActivity.this, "登入成功", Toast.LENGTH_LONG).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
package com.example.sqlitedemo;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button sendbtnButton;
private ListView listView;
private List<Map<String, String>> list = new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendbtnButton = (Button) findViewById(R.id.sendbtn);
listView = (ListView) findViewById(R.id.listview);
getdataNews();
inirView();
}
private void inirView() {
adapter = new MyAdapter(this, list);
listView.setAdapter(adapter);
/**
* 跳转至发布Activity
*/
sendbtnButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, SendActivity.class));
}
});
/**
* 跳转至新闻详情Activity
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//将标题等参数入传入 注意传入的键值与接收的一致
Intent intent = new Intent(MainActivity.this, ShowMainActivity.class);
intent.putExtra("title", list.get(arg2).get("title"));//键名
intent.putExtra("content", list.get(arg2).get("content"));
intent.putExtra("time", list.get(arg2).get("time"));
intent.putExtra("username", list.get(arg2).get("username"));
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int postion, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setItems(new String[]{"修改", "删除"}, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1) {
case 0:
//单击修改的执行方法
Intent intent = new Intent(MainActivity.this, UpdateMainActivity.class);
intent.putExtra("title", list.get(postion).get("title"));
intent.putExtra("content", list.get(postion).get("content"));
intent.putExtra("nid", list.get(postion).get("nid"));
startActivity(intent);
break;
case 1:
//单击删除的执行方法
String sqlString = "delete from news where nid='" + list.get(postion).get("nid") + "'";
SQLiteDatabase db = Myapp.db.getWritableDatabase();
db.execSQL(sqlString);
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();
list.remove(postion);//移除list里面的数据
adapter.notifyDataSetChanged();//刷新数据库
break;
}
}
});
builder.show();
return true;
}
});
}
/**
* 获取所有新闻
*/
private void getdataNews() {
SQLiteDatabase dbDatabase = Myapp.db.getReadableDatabase();
//查询新闻表
String sqlString = "select * from news";
Cursor cursor = dbDatabase.rawQuery(sqlString, null);
while (cursor.moveToNext()) {
Map<String, String> map = new HashMap<>();
map.put("nid", cursor.getInt(cursor.getColumnIndex("nid")) + "");
map.put("title", cursor.getString(cursor.getColumnIndex("title")));
map.put("content", cursor.getString(cursor.getColumnIndex("content")));
map.put("time", cursor.getString(cursor.getColumnIndex("newstime")));
map.put("username", cursor.getString(cursor.getColumnIndex("username")));
list.add(map);
}
//注意一定要关闭cursor
cursor.close();
}
}
package com.example.sqlitedemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
/**
* Created by HLF on 2017/7/16.
* 没做优化
*/
public class MyAdapter extends BaseAdapter {
private List<Map<String, String>> list;
private Context context;
public MyAdapter(Context context, List<Map<String, String>> list) {
this.context = context;
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return getItemId(arg0);
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View v, ViewGroup arg2) {
v = LayoutInflater.from(context).inflate(R.layout.listview, null);
TextView timeTextView = (TextView) v.findViewById(R.id.item_time);
TextView title = (TextView) v.findViewById(R.id.item_title);
TextView username = (TextView) v.findViewById(R.id.item_username);
timeTextView.setText(list.get(arg0).get("time"));
title.setText(list.get(arg0).get("title"));
username.setText(list.get(arg0).get("username"));
return v;
}
}
package com.example.sqlitedemo;
import android.app.Application;
/**
* Created by HLF on 2017/7/16.
*/
public class Myapp extends Application {
public static MyDateBasrHelp db;
//名字可能很多地方会用到
public static String username;
@Override
public void onCreate() {
super.onCreate();
//创建数据库
db = new MyDateBasrHelp(getApplicationContext());
}
}
package com.example.sqlitedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by HLF on 2017/7/16.
* 表的创建
*/
public class MyDateBasrHelp extends SQLiteOpenHelper {
//数据库的名字
final static String DBNAME = "mynews.db";
//数据库的版本
final static int version = 1;
//, String name, SQLiteDatabase.CursorFactory factory, int version
public MyDateBasrHelp(Context context) {
super(context, DBNAME, null, version);
}
/**
* 初始化表
*
* @param sqLiteDatabase
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建表一张名为user的用户表 主键uid
// 这里建议将创建表的语句用拼接的方式写 多张表会重复很多代码
//参数有名字、密码、性别、年龄信息
String userspl = "create table user(uid integer primary key autoincrement,username varchar,password varchar,sex varchar,age integer)";
sqLiteDatabase.execSQL(userspl);
//创建表一张名为news的新闻表 主键nid
//参数有标题、内容、发布的时间、发布的作者名
String newsql = "create table news(nid integer primary key autoincrement,title varchar,content varchar,newstime varchar,username varchar)";
sqLiteDatabase.execSQL(newsql);
}
//表的更新
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
package com.example.sqlitedemo;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 注册类
*/
public class RegistActivity extends Activity {
private EditText username, password, sex, age;
private Button regbtnButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regeist);
username = (EditText) findViewById(R.id.reg_username);
password = (EditText) findViewById(R.id.reg_password);
sex = (EditText) findViewById(R.id.reg_sex);
age = (EditText) findViewById(R.id.reg_age);
regbtnButton = (Button) findViewById(R.id.reg_ok_btn);
regbtnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String name = username.getText().toString();
String pass = password.getText().toString();
String se = sex.getText().toString();
String ag = age.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(RegistActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(RegistActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(se)) {
Toast.makeText(RegistActivity.this, "性别不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(ag)) {
Toast.makeText(RegistActivity.this, "年龄不能为空", Toast.LENGTH_LONG).show();
return;
} else {
//插入你的数据到user表中数
SQLiteDatabase db = Myapp.db.getWritableDatabase();
//注意查入的顺序、字段名一定要一致 年龄注意是integer
String sql = "insert into user(username,password,sex,age) values" + "('" + name + "','" + pass + "','" + se + "','" + Integer.parseInt(ag) + "')";
db.execSQL(sql);//执行sql语句
Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
package com.example.sqlitedemo;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Calendar;
import static android.R.attr.start;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
/**
* 发布新闻类
*/
public class SendActivity extends Activity implements OnClickListener {
private EditText title, content;
private Button btnButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
title = (EditText) findViewById(R.id.edit_title);
content = (EditText) findViewById(R.id.edit_content);
btnButton = (Button) findViewById(R.id.sendNews_btn);
btnButton.setOnClickListener(this);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.sendNews_btn:
String titl = title.getText().toString();
String conten = content.getText().toString();
if (TextUtils.isEmpty(titl)) {
Toast.makeText(SendActivity.this, "标题不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(conten)) {
Toast.makeText(SendActivity.this, "内容不能为空", Toast.LENGTH_LONG).show();
return;
} else {
String timString = getTime();//获取时间
SQLiteDatabase db = Myapp.db.getWritableDatabase();
//提交数据到news表中 插入4个参数
String sql = "insert into news(title,content,newstime,username) values " +
"('" + titl + "','" + conten + "','" + timString + "','" + Myapp.username + "')";
db.execSQL(sql);
startActivity(new Intent(SendActivity.this, MainActivity.class));
Toast.makeText(SendActivity.this, "发布成功", Toast.LENGTH_LONG).show();
finish();
}
break;
}
}
/**
* 获取系统时间
*/
private String getTime() {
Calendar calendar = Calendar.getInstance();
String str = calendar.get(Calendar.YEAR) + "年" +
(calendar.get(Calendar.MONTH) + 1) + "月" +
calendar.get(Calendar.DAY_OF_MONTH) +
"日" + calendar.get(Calendar.HOUR_OF_DAY) + "时" +
calendar.get(Calendar.MINUTE) + "分" +
calendar.get(Calendar.SECOND) + "秒";
return str;
}
}
package com.example.sqlitedemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
/**
* 新闻详情
*/
public class ShowMainActivity extends Activity {
private TextView title,content,time,nameTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_main);
title=(TextView) findViewById(R.id.info_title);
content=(TextView) findViewById(R.id.info_content);
time=(TextView) findViewById(R.id.info_time);
nameTextView=(TextView) findViewById(R.id.info_author);
Intent intent=getIntent();
String titlestr=intent.getStringExtra("title");
String contentstr=intent.getStringExtra("content");
String timestr=intent.getStringExtra("time");
String usernamestr=intent.getStringExtra("username");
title.setText(titlestr);
content.setText(contentstr);
time.setText(timestr);
nameTextView.setText(usernamestr);
}
}
package com.example.sqlitedemo;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 更新新闻类
*/
public class UpdateMainActivity extends Activity {
private EditText title, content;
private Button updatebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_main);
title = (EditText) findViewById(R.id.update_title);
content = (EditText) findViewById(R.id.update_content);
updatebutton = (Button) findViewById(R.id.updateNews_btn);
Intent intent = getIntent();
final String tit = intent.getStringExtra("title");
final String cont = intent.getStringExtra("content");
final String nid = intent.getStringExtra("nid");
title.setText(tit);
content.setText(cont);
updatebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String strTit = title.getText().toString();
String strCont = title.getText().toString();
if (TextUtils.isEmpty(strTit)) {
Toast.makeText(UpdateMainActivity.this, "标题不能为空", Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(strCont)) {
Toast.makeText(UpdateMainActivity.this, "内容不能为空", Toast.LENGTH_LONG).show();
return;
}
//更新新闻表
String sql = "update news set title='" + strTit + "',content='" + strCont + "'where nid='" + nid + "'";
SQLiteDatabase db = Myapp.db.getWritableDatabase();
db.execSQL(sql);
Toast.makeText(UpdateMainActivity.this, "修改成功", Toast.LENGTH_LONG).show();
startActivity(new Intent(UpdateMainActivity.this, MainActivity.class));
finish();
}
});
}
}
Step 5:两个资源文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<stroke android:width="3dp"
android:color="#008AD4"/>
<solid android:color="#008AD4"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!--设置圆角半径 -->
<corners
android:radius="5dp"/>
<!-- 设置边框的厚度以及边框的颜色 -->
<stroke android:width="2dp"
android:color="#666"/>
</shape>
数据库有缓存,修改代码没效果可以先清除缓存试试!!!