yangxinhua 2018-12-31
SqliteHelper 创表页面
**
package com.example.y700_15.news_eight.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(@Nullable Context context) {
super(context, "Users.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table myworld (id integer primary key autoincrement," +
"title text," +
"img1 text," +
"img2 text," +
"img3 text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
**
Dao层 添加 删除 查询
**
package com.example.y700_15.news_eight.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.y700_15.news_eight.bean.Bean;
import java.util.ArrayList;
import java.util.List;
public class Dao {
private SqliteHelper helper;
private SQLiteDatabase database;
public Dao(Context context){
helper = new SqliteHelper(context);
database = helper.getWritableDatabase();
}
//添加
public void add(Bean.DataBean data){
ContentValues values = new ContentValues();
values.put("title",data.getTitle());
values.put("img1",data.getThumbnail_pic_s());
values.put("img2",data.getThumbnail_pic_s02());
values.put("img3",data.getThumbnail_pic_s03());
database.insert("myworld",null, values);
}
//循环添加
public void addAll(List<Bean.DataBean> list){
for (Bean.DataBean bean : list){
add(bean);
}
}
public void delAll() {
database.delete("myworld", null, null);
}
//删除
public void del(String title){
database.delete("myworld","title=?",new String[]{title});
}
//查询
public List<Bean.DataBean> query(){
List<Bean.DataBean> list = new ArrayList<>();
Cursor cursor = database.query("myworld", null, null, null, null, null, null);
while (cursor.moveToNext()){
String title = cursor.getString(cursor.getColumnIndex("title"));
String img1 = cursor.getString(cursor.getColumnIndex("img1"));
String img2 = cursor.getString(cursor.getColumnIndex("img2"));
String img3 = cursor.getString(cursor.getColumnIndex("img3"));
Bean.DataBean bean = new Bean.DataBean(title,img1,img2,img3);
list.add(bean);
}
return list;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
**
Fragment页面 数据库解析数据 + 删除数据
**
package com.example.y700_15.news_eight.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.y700_15.news_eight.R;
import com.example.y700_15.news_eight.adapter.PullAdapter;
import com.example.y700_15.news_eight.sqlite.Dao;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MovieFragment extends Fragment {
private PullToRefreshListView pull;
private Dao dao;
private PullAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.moviefragment,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pull = view.findViewById(R.id.pull);
adapter = new PullAdapter(getActivity());
pull.setAdapter(adapter);
dao = new Dao(getActivity());
pull.setMode(PullToRefreshListView.Mode.PULL_FROM_START);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
adapter.setList(dao.query());
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//dao.query();
}
});
pull.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dao.del(adapter.getItem(position).getTitle());
adapter.delete(position-1);
}
});
adapter.setList(dao.query());
}
}
---------------------
作者:破晓后的曙光
原文:https://blog.csdn.net/aishangqian/article/details/85133608
版权声明:本文为博主原创文章,转载请附上博文链接!