Skip to content

Commit de30b5e

Browse files
committed
重新设计feed.db的数据库,增加相应的Dao层
1 parent 5d44a69 commit de30b5e

16 files changed

+226
-87
lines changed

AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
</activity>
4141
<activity android:name="com.dreamteam.app.ui.GuideActivity" >
4242
</activity>
43-
<activity android:name="com.dreamteam.app.ui.FeedCategory" >
43+
<activity android:name="com.dreamteam.app.ui.FeedCategoryUI" >
4444
</activity>
45-
<activity android:name="com.dreamteam.app.ui.CategoryDetail" >
45+
<activity android:name="com.dreamteam.app.ui.FeedUI" >
4646
</activity>
4747
<activity android:name="com.dreamteam.app.ui.Setting" >
4848
</activity>

assets/feed.db

-6 KB
Binary file not shown.

bin/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
</activity>
4141
<activity android:name="com.dreamteam.app.ui.GuideActivity" >
4242
</activity>
43-
<activity android:name="com.dreamteam.app.ui.FeedCategory" >
43+
<activity android:name="com.dreamteam.app.ui.FeedCategoryUI" >
4444
</activity>
45-
<activity android:name="com.dreamteam.app.ui.CategoryDetail" >
45+
<activity android:name="com.dreamteam.app.ui.FeedUI" >
4646
</activity>
4747
<activity android:name="com.dreamteam.app.ui.Setting" >
4848
</activity>

sql/feed.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE feed(
2+
`fid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
3+
`fname` CHARACTER(10) NOT NULL,
4+
`url` VARCHAR(255) NOT NULL,
5+
`state` TINYINT(1) DEFAULT 0,
6+
`cid` INTEGER NOT NULL
7+
);
8+
9+
CREATE TABLE feed_category(
10+
`cid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
11+
`cname` CHARACTER(10) NOT NULL
12+
);

src/com/dreamteam/app/adapter/FeedCategoryAdapter.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.dreamteam.app.adapter;
22

3+
import java.util.ArrayList;
4+
35
import android.content.Context;
46
import android.view.LayoutInflater;
57
import android.view.View;
68
import android.view.ViewGroup;
79
import android.widget.BaseAdapter;
810
import android.widget.TextView;
911

12+
import com.dreamteam.app.dao.FeedCategoryDao;
13+
import com.dreamteam.app.entity.FeedCategory;
1014
import com.dreateam.app.ui.R;
1115

1216
/**
@@ -16,28 +20,25 @@
1620
*/
1721
public class FeedCategoryAdapter extends BaseAdapter
1822
{
19-
private Context context;
2023
private LayoutInflater inflater;
21-
private String[] categories_zh;
22-
24+
private ArrayList<FeedCategory> fcList;
2325

24-
public FeedCategoryAdapter(Context context)
26+
public FeedCategoryAdapter(Context context, ArrayList<FeedCategory> fcList)
2527
{
26-
this.context = context;
27-
categories_zh = context.getResources()
28-
.getStringArray(R.array.feed_category);
28+
this.fcList = fcList;
29+
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2930
}
3031

3132
@Override
3233
public int getCount()
3334
{
34-
return categories_zh.length;
35+
return fcList.size();
3536
}
3637

3738
@Override
3839
public Object getItem(int position)
3940
{
40-
return categories_zh[position];
41+
return fcList.get(position);
4142
}
4243

4344
@Override
@@ -49,12 +50,10 @@ public long getItemId(int position)
4950
@Override
5051
public View getView(int position, View convertView, ViewGroup parent)
5152
{
52-
ViewHolder viewHolder = null;
53+
ViewHolder viewHolder;
5354

5455
if(convertView == null)
55-
{
56-
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
57-
convertView = inflater.inflate(R.layout.category_item, null);
56+
{ convertView = inflater.inflate(R.layout.category_item, null);
5857
viewHolder = new ViewHolder();
5958
viewHolder.categoryTitle = (TextView) convertView.findViewById(R.id.category_title);
6059
convertView.setTag(viewHolder);
@@ -63,11 +62,11 @@ public View getView(int position, View convertView, ViewGroup parent)
6362
{
6463
viewHolder = (ViewHolder) convertView.getTag();
6564
}
66-
viewHolder.categoryTitle.setText(categories_zh[position]);
65+
viewHolder.categoryTitle.setText(fcList.get(position).getName());
6766
return convertView;
6867
}
6968

70-
private static final class ViewHolder
69+
private final static class ViewHolder
7170
{
7271
TextView categoryTitle;
7372
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.dreamteam.app.dao;
2+
3+
import java.util.ArrayList;
4+
5+
import android.content.Context;
6+
import android.database.Cursor;
7+
import android.database.sqlite.SQLiteDatabase;
8+
9+
import com.dreamteam.app.db.DbManager;
10+
import com.dreamteam.app.db.FeedDBManager;
11+
import com.dreamteam.app.entity.FeedCategory;
12+
13+
public class FeedCategoryDao
14+
{
15+
public static final String TABLE_NAME = "feed_category";
16+
private Context context;
17+
18+
public FeedCategoryDao(Context context)
19+
{
20+
this.context = context;
21+
}
22+
23+
public ArrayList<FeedCategory> getList()
24+
{
25+
ArrayList<FeedCategory> fcList = new ArrayList<FeedCategory>();
26+
27+
DbManager helper = new DbManager(context, FeedDBManager.DB_NAME, null, 1);
28+
SQLiteDatabase db = helper.getWritableDatabase();
29+
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
30+
if (cursor.moveToFirst())
31+
{
32+
for (int i = 0, n = cursor.getCount(); i < n; i++)
33+
{
34+
FeedCategory fc = new FeedCategory();
35+
fc.setId(cursor.getInt(cursor.getColumnIndex("cid")));
36+
fc.setName(cursor.getString(cursor.getColumnIndex("cname")));
37+
fcList.add(fc);
38+
39+
cursor.moveToNext();
40+
}
41+
}
42+
cursor.close();
43+
db.close();
44+
45+
return fcList;
46+
}
47+
48+
public ArrayList<String> getNameList()
49+
{
50+
ArrayList<String> fcList = new ArrayList<String>();
51+
52+
DbManager helper = new DbManager(context, FeedDBManager.DB_NAME, null, 1);
53+
SQLiteDatabase db = helper.getWritableDatabase();
54+
Cursor cursor = db.query("category", null, null, null, null, null, null);
55+
if (cursor.moveToFirst())
56+
{
57+
for (int i = 0, n = cursor.getCount(); i < n; i++)
58+
{
59+
fcList.add(cursor.getString(cursor.getColumnIndex("fname")));
60+
61+
cursor.moveToNext();
62+
}
63+
}
64+
cursor.close();
65+
db.close();
66+
67+
return fcList;
68+
}
69+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.dreamteam.app.dao;
2+
3+
import java.util.ArrayList;
4+
5+
import android.content.Context;
6+
import android.database.Cursor;
7+
import android.database.sqlite.SQLiteDatabase;
8+
9+
import com.dreamteam.app.db.FeedDBManager;
10+
import com.dreamteam.app.entity.Feed;
11+
12+
/**
13+
* @description TODO
14+
* @author zcloud
15+
* @date 2014年10月1日
16+
*/
17+
public class FeedDao
18+
{
19+
public static final String TABLE_NAME = "feed";
20+
private Context context;
21+
22+
public FeedDao(Context context)
23+
{
24+
this.context = context;
25+
}
26+
27+
public ArrayList<Feed> getListByCategoryId(String id)
28+
{
29+
ArrayList<Feed> list = new ArrayList<Feed>();
30+
FeedDBManager helper = new FeedDBManager(context, FeedDBManager.DB_NAME, null, 1);
31+
SQLiteDatabase db = helper.getWritableDatabase();
32+
33+
Cursor cursor = db.query(TABLE_NAME, null, "cid=?", new String[]{id}, null, null, null);
34+
if (cursor.moveToFirst())
35+
{
36+
for (int i = 0, n = cursor.getCount(); i < n; i++)
37+
{
38+
Feed f = new Feed();
39+
String title = cursor.getString(cursor.getColumnIndex("fname"));
40+
String url = cursor.getString(cursor.getColumnIndex("url"));
41+
int selectStatus = cursor.getInt(cursor
42+
.getColumnIndex("state"));
43+
f.setTitle(title);
44+
f.setUrl(url);
45+
f.setSelectStatus(selectStatus);
46+
list.add(f);
47+
cursor.moveToNext();
48+
}
49+
}
50+
cursor.close();
51+
db.close();
52+
return list;
53+
}
54+
}

src/com/dreamteam/app/dao/SectionDAO.java renamed to src/com/dreamteam/app/dao/SectionDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
* @author zcloud
1818
* @date 2014年9月11日
1919
*/
20-
public class SectionDAO
20+
public class SectionDao
2121
{
2222
public static final String tag = "SectionDAO";
2323
private Context context;
2424

2525

26-
public SectionDAO(Context context)
26+
public SectionDao(Context context)
2727
{
2828
this.context = context;
2929
}

src/com/dreamteam/app/db/DbConstant.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ public class DbConstant
44
{
55
public static final String FAVO_TABLE_NAME = "favorite_item";
66
public static final String SECTION_TABLE_NAME = "section";
7-
8-
97
}

src/com/dreamteam/app/db/FeedDBManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void updateState(String tableName, int state, String url)
4444
{
4545
SQLiteDatabase db = getWritableDatabase();
4646
ContentValues values = new ContentValues();
47-
values.put("select_status", state);
47+
values.put("state", state);
4848
db.update(tableName, values, "url=?", new String[]{url});
4949
db.close();
5050
}

0 commit comments

Comments
 (0)