概述
Android资讯新闻类App(头条、网易等),频道管理集成之数据库存储
发布时间:2019-01-16 19:55,
浏览次数:305
, 标签:
Android
App
*
经常看新闻类的大家都知道这个频道管理。
图片转载https://blog.csdn.net/u011916937/article/details/50483278
*
我这里是用的Github,开源项目,导入的依赖,下属必须配置。
allprojects { repositories { google() jcenter() //工程下的build.gradle maven { url
"https://jitpack.io" } } }
app下的build.gradle
implementation 'com.github.andyoom:draggrid:v1.0.1'
清单文件:
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
/>
Activity内写法:
/此activity用数据库存储数据 public class Main2Activity extends AppCompatActivity {
private List tabs = new ArrayList<>(); private TabLayout tab; private
ViewPager vp; private DBDao dbDao; private List list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tab
= findViewById(R.id.tab); vp = findViewById(R.id.vp); dbDao = new DBDao(this);
//先查询数据库有没有数据,有数据取出选中的标签 list = dbDao.queryData(); if (list.size() != 0) { for
(int i = 0; i < list.size(); i++) { ChannelBean channelBean = list.get(i);
boolean select = channelBean.isSelect(); String name = channelBean.getName();
if (select) { //为true添加给标签 tabs.add(name); } } } else { //第一次运行程序 初始化数据并插入数据库
for (int i = 0; i < 10; i++) { ChannelBean bean = new ChannelBean("item-" + i,
i < 5 ? true : false); dbDao.addData(bean); } //查询出数据 list = dbDao.queryData();
//遍历数据找出状态为true的能标签 for (int i = 0; i < list.size(); i++) { ChannelBean
channelBean = list.get(i); boolean select = channelBean.isSelect(); String name
= channelBean.getName(); if (select) { //为true添加给标签 tabs.add(name); } } }
tab.setTabMode(TabLayout.MODE_SCROLLABLE); //创建适配器 MainViewPagerAdapter adapter
= new MainViewPagerAdapter(getSupportFragmentManager(), tabs);
vp.setAdapter(adapter); tab.setupWithViewPager(vp); //频道管理 Button bt_go =
findViewById(R.id.bt_go); bt_go.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { //每次点击更新最新数据 list = dbDao.queryData();
ChannelActivity.startChannelActivity(Main2Activity.this, list); } }); } //回传数据
@Override protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
//清空数据 tabs.clear(); dbDao.delete(); Log.i("xxx", data.getStringExtra("json"));
//返回json数据 String json = data.getStringExtra("json"); try { JSONArray jsonArray
= new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) {
ChannelBean channelBean = null; JSONObject jsonObject =
jsonArray.getJSONObject(i); String isSelect = jsonObject.getString("isSelect");
String name = null; if (isSelect.equals("true")) { //为true添加给标签 name =
jsonObject.getString("name"); tabs.add(name); channelBean = new
ChannelBean(name, true); } else { name = jsonObject.getString("name");
channelBean = new ChannelBean(name, false); } //从新把数据添加给数据库
dbDao.addData(channelBean); } //把标签给tablayout for (int i = 0; i < tabs.size();
i++) { tab.addTab(tab.newTab().setText( tabs.get(i))); } //创建适配器
MainViewPagerAdapter adapter = new
MainViewPagerAdapter(getSupportFragmentManager(), tabs);
vp.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } } }
Sql_Dao层写法:
public class DBDao { private final DBHelper helper; public DBDao(Context
context) { helper = new DBHelper(context); } //添加 public void
addData(ChannelBean channelBean) { SQLiteDatabase db =
helper.getWritableDatabase(); ContentValues values = new ContentValues();
values.put("name", channelBean.getName()); //可以储存boolean类型
values.put("isSelect", channelBean.isSelect()); db.insert("cache", null,
values); db.close(); } //查询 public List queryData() {
SQLiteDatabase db = helper.getWritableDatabase(); List list = new
ArrayList<>(); Cursor cursor = db.query("cache", null, null, null, null, null,
null); while (cursor.moveToNext()) { ChannelBean channelBean = null; String
name = cursor.getString(cursor.getColumnIndex("name")); //根据解析获取字段 int anInt =
cursor.getInt(2); //0代表是false if (anInt == 0) { channelBean = new
ChannelBean(name, false); //1代表true } else { channelBean = new
ChannelBean(name, true); } list.add(channelBean); } return list; } //删除 public
void delete() { SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from cache"); } }
最后
以上就是个性耳机为你收集整理的android新闻管理,Android资讯新闻类App(头条、网易等),频道管理集成之数据库存储...的全部内容,希望文章能够帮你解决android新闻管理,Android资讯新闻类App(头条、网易等),频道管理集成之数据库存储...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复