由网友(糖不及你甜)分享简介:我在Android中非常新手程序员。我的查询来从SQLite的数据,然后反映所有的数据到列表列。现在,当我点击按钮,所有的数据必须具有良好的tabularised格式pdf格式转换......数以百万计感谢提前.. 公共类Details_List延伸活动{巴顿份额;@覆盖公共无效的onCreate(包savedI...![C 数据本地存储方案之SQLite](/d/file/2023/09-17/01b84b702c208db1196439ed70cd9e0d.png)
我在Android中非常新手程序员。我的查询来从SQLite的数据,然后反映所有的数据到列表列。现在,当我点击按钮,所有的数据必须具有良好的tabularised格式pdf格式转换......数以百万计感谢提前..
公共类Details_List延伸活动{
巴顿份额;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.details_list);
ListView的LV =(ListView控件)findViewById(R.id.listview);
份额=(按钮)findViewById(R.id.email);
share.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意图I =新的意图(Details_List.this,Email.class);
startActivity(ⅰ);
}
});
//创建网格项映射
的String []从=新的String [] {ROWID,COL_1,COL_2,col_3,col_4,col_5};
INT []到=新INT [] {R.id.item1,R.id.item2,R.id.item3,R.id.item4,R.id.item5,R.id.item6};
// prepare的所有记录的清单
名单< HashMap的<字符串,字符串>> fillMaps =新的ArrayList< HashMap的<字符串,字符串>>();
的for(int i = 1;我小于10;我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
map.put(行ID,+ I);
map.put(COL_1,col_1_item_+ I);
map.put(COL_2,col_2_item_+ I);
map.put(col_3,col_3_item_+ I);
map.put(col_3,col_4_item_+ I);
map.put(col_3,col_5_item_+ I);
fillMaps.add(图)
}
//填写grid_item布局
SimpleAdapter适配器=新SimpleAdapter(这一点,fillMaps,R.layout.grid_item,从,到);
lv.setAdapter(适配器);
}
}
解决方案
你的第一个目的获取数据的SQLite数据库到ArrayList中,这里是下面一个例子:
公共类DatabaseHelper_Notification扩展SQLiteOpenHelper
{
公共字符串标记=DatabaseHelper:;
私有静态最后弦乐DATABASE_NAME =通知;
私人最终语境cntxtDBContext;
最终静态INT DATABASE_VERSION = 2;
最后弦乐_TableName =通知;
最后弦乐_rowid =的RowID;
最后弦乐_NotificationID =NotificationID;
最后弦乐_NotificationText =NotificationText;
最后弦乐_NotificationRead =NotificationRead;
/ **
*这个类的默认构造函数
*
* @参数方面
*的上下文,其中该对象是要使用
* /
公共DatabaseHelper_Notification(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
this.cntxtDBContext =背景;
// strDB_path = cntxtDBContext.getDatabasePath(strDB_NAME)的ToString();
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
字符串CREATE_CONTACTS_TABLE =CREATE TABLE+ _TableName +(+ _rowid +INTEGER PRIMARY KEY,+ _NotificationID
+INTEGER NOT NULL DEFAULT(0),+ _NotificationText +TEXT+,+ _NotificationRead +布尔NOT NULL DEFAULT(0));
db.execSQL(CREATE_CONTACTS_TABLE);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
如果(动态网页> oldVersion)
{
db.execSQL(DROP TABLE IF EXISTS+ _TableName);
//再次创建表
的onCreate(DB);
}
}
/ **
*会从通知数据库中所有的通知消息
*
返回:在数据库中的通知present列表
* /
公众的ArrayList< NotificationCls> getAllNotifications()
{
ArrayList的< NotificationCls> alNotifications =新的ArrayList< NotificationCls>();
;
光标curData = NULL;
SQLiteDatabase DB = NULL;
尝试
{
DB = this.getReadableDatabase();
字符串strSelectQuery =SELECT * FROM+ _TableName;
curData = db.rawQuery(strSelectQuery,NULL);
如果(curData.getCount()大于0)
{
curData.moveToFirst();
做
{
NotificationCls NotI位=新NotificationCls();
noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
布尔面包= curData.getInt(curData.getColumnIndex(_NotificationRead))== 0?假:真的;
noti.setbNotificationRead(面包);
alNotifications.add(NotI位);
}
而(curData.moveToNext());
}
}
赶上(的SQLException E)
{
e.printStackTrace();
}
赶上(例外五)
{
e.printStackTrace();
}
最后
{
如果(curData!= NULL)
{
curData.close();
}
如果(DB!= NULL)
{
db.close();
}
}
返回alNotifications;
}
/ **
*插入通知消息在数据库
*
* @参数消息
*在推送通知接收的消息
* @参数ID
*该通知发送的ID
* @返回此方法将返回< code取代;真< / code取代;如果数据库事务成功否则将返回< code取代;假< / code取代;
* /
公共布尔insertNotification(字符串消息,INT ID)
{
长盖= -1;
SQLiteDatabase DB = NULL;
尝试
{
DB = this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(_NotificationID,ID);
values.put(_NotificationText,味精);
db.insert(_TableName,空,价值观);
db.close();
}
赶上(的SQLException E)
{
盖= -1;
e.printStackTrace();
}
赶上(例外五)
{
盖= -1;
e.printStackTrace();
}
最后
{
如果(DB!= NULL)
db.close();
}
返回盖== -1?假:真的;
}
/ **
*此方法将删除所有与指定的通知ID相关联的记录
*
* @参数iNotificationiID
*该通知发送的ID
* @返回此方法将返回< code取代;真< / code取代;如果数据库事务成功否则将返回< code取代;假< / code取代;
* /
公共布尔deleteNotification(INT iNotificationiID)
{
INT结果为0;
SQLiteDatabase DB = this.getWritableDatabase();
结果= db.delete(_TableName,_NotificationID +=+ iNotificationiID,新的String [] {});
db.close();
如果(结果大于0)
{
返回true;
}
其他
{
返回false;
}
}
/ **
*此方法将删除数据库中的所有记录
*
* @返回此方法将返回< code取代;真< / code取代;如果数据库事务成功否则将返回< code取代;假< / code取代;
* /
公共布尔deleteAllNotifications()
{
INT结果为0;
SQLiteDatabase DB = this.getWritableDatabase();
结果= db.delete(_TableName,空,新的String [] {});
db.close();
如果(结果大于0)
{
返回true;
}
其他
{
返回false;
}
}
/ **
*此方法将返回在数据库中的记录present数
*
返回:在数据库中记录present数
* /
公众诠释getNotificationsCount()
{
字符串countQuery =SELECT * FROM+ _TableName;
SQLiteDatabase DB = this.getReadableDatabase();
光标光标= db.rawQuery(countQuery,NULL);
cursor.close();
//返回计数
返回cursor.getCount();
}
公共布尔markNotificationAsRead(INT iNotificationiID)
{
// TODO自动生成方法存根
INT结果= -1;
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(_NotificationRead,1);
//更新行
结果= db.update(_TableName,价值观,_NotificationID +=?,新的String [] {将String.valueOf(iNotificationiID)});
db.close();
如果(结果大于0)
{
返回true;
}
其他
{
返回false;
}
}
公共布尔markAllNotificationAsRead()
{
// TODO自动生成方法存根
INT结果= -1;
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(_NotificationRead,1);
//更新行
结果= db.update(_TableName,价值观,空,新的String [] {});
db.close();
如果(结果大于0)
{
返回true;
}
其他
{
返回false;
}
}
/ **
*会从通知数据库中所有的通知消息
*
返回:在数据库中的通知present列表
* /
公众的ArrayList< NotificationCls> getAllUnreadNotifications()
{
ArrayList的< NotificationCls> alNotifications =新的ArrayList< NotificationCls>();
;
光标curData = NULL;
SQLiteDatabase DB = NULL;
尝试
{
DB = this.getReadableDatabase();
字符串strSelectQuery =SELECT * FROM+ _TableName +WHERE+ _NotificationRead += 0;
curData = db.rawQuery(strSelectQuery,NULL);
如果(curData.getCount()大于0)
{
curData.moveToFirst();
做
{
NotificationCls NotI位=新NotificationCls();
noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
alNotifications.add(NotI位);
}
而(curData.moveToNext());
}
}
赶上(的SQLException E)
{
e.printStackTrace();
}
赶上(例外五)
{
e.printStackTrace();
}
最后
{
如果(curData!= NULL)
{
curData.close();
}
如果(DB!= NULL)
{
db.close();
}
}
返回alNotifications;
}
这是实体类:
公共类NotificationCls
{
字符串strNotificationText;
INT iNotificationiID;
布尔bNotificationRead = FALSE;
公共字符串getStrNotificationText()
{
返回strNotificationText;
}
公共无效setStrNotificationText(字符串strNotificationText)
{
this.strNotificationText = strNotificationText;
}
公众诠释getiNotificationiID()
{
返回iNotificationiID;
}
公共无效setiNotificationiID(INT iNotificationiID)
{
this.iNotificationiID = iNotificationiID;
}
公共布尔isbNotificationRead()
{
返回bNotificationRead;
}
公共无效setbNotificationRead(布尔bNotificationRead)
{
this.bNotificationRead = bNotificationRead;
}
}
![C 数据本地存储方案之SQLite](/d/file/2023/09-17/01b84b702c208db1196439ed70cd9e0d.png)
要获取所有数据的SQLite在您的活动低于code ArrayList中写:
DatabaseHelper_Notification databaseHelper_notification =新DatabaseHelper_Notification(本);
ArrayList的< NotificationCls> alNotifications = databaseHelper_notification.getAllNotifications();
我希望这可以帮助你......
I am very novice programmer in Android. My query is to fetch data from SQLite and then reflect all data in to list column . now when i click on button, that all data must be converted in .pdf format with good tabularised format... Millions of thank in advance..
public class Details_List extends Activity {
Button share;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_list);
ListView lv= (ListView)findViewById(R.id.listview);
share = (Button)findViewById(R.id.email);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Details_List.this, Email.class);
startActivity(i);
}
});
// create the grid item mapping
String[] from = new String[] {"rowid", "col_1", "col_2", "col_3", "col_4", "col_5"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 1; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
map.put("col_3", "col_4_item_" + i);
map.put("col_3", "col_5_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}
解决方案
for your first purpose to get data from SQLite DB into arraylist, here is an example below:
public class DatabaseHelper_Notification extends SQLiteOpenHelper
{
public String TAG = "DatabaseHelper:";
private static final String DATABASE_NAME = "Notifications";
private final Context cntxtDBContext;
final static int DATABASE_VERSION = 2;
final String _TableName = "Notifications";
final String _RowID = "RowID";
final String _NotificationID = "NotificationID";
final String _NotificationText = "NotificationText";
final String _NotificationRead = "NotificationRead";
/**
* the default constructor of this class
*
* @param context
* the context in which this object is to be used
*/
public DatabaseHelper_Notification(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.cntxtDBContext = context;
// strDB_path = cntxtDBContext.getDatabasePath(strDB_NAME).toString();
}
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + _TableName + "(" + _RowID + " INTEGER PRIMARY KEY," + _NotificationID
+ " INTEGER NOT NULL DEFAULT (0)," + _NotificationText + " TEXT" + "," + _NotificationRead + " BOOL NOT NULL DEFAULT (0))";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (newVersion > oldVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + _TableName);
// Create tables again
onCreate(db);
}
}
/**
* gets all notification news from notifications database
*
* @return the list of the notification present in the database
*/
public ArrayList<NotificationCls> getAllNotifications()
{
ArrayList<NotificationCls> alNotifications = new ArrayList<NotificationCls>();
;
Cursor curData = null;
SQLiteDatabase db = null;
try
{
db = this.getReadableDatabase();
String strSelectQuery = "SELECT * FROM " + _TableName;
curData = db.rawQuery(strSelectQuery, null);
if (curData.getCount() > 0)
{
curData.moveToFirst();
do
{
NotificationCls noti = new NotificationCls();
noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
boolean bRead = curData.getInt(curData.getColumnIndex(_NotificationRead)) == 0 ? false : true;
noti.setbNotificationRead(bRead);
alNotifications.add(noti);
}
while (curData.moveToNext());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (curData != null)
{
curData.close();
}
if (db != null)
{
db.close();
}
}
return alNotifications;
}
/**
* insert the notification message in the database
*
* @param Msg
* the message received in push notification
* @param id
* the id on which the notification was sent
* @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
*/
public boolean insertNotification(String Msg, int id)
{
long lid = -1;
SQLiteDatabase db = null;
try
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(_NotificationID, id);
values.put(_NotificationText, Msg);
db.insert(_TableName, null, values);
db.close();
}
catch (SQLException e)
{
lid = -1;
e.printStackTrace();
}
catch (Exception e)
{
lid = -1;
e.printStackTrace();
}
finally
{
if (db != null)
db.close();
}
return lid == -1 ? false : true;
}
/**
* this method will delete all the records associated with the specified notification id
*
* @param iNotificationiID
* the id on which the notification was sent
* @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
*/
public boolean deleteNotification(int iNotificationiID)
{
int result = 0;
SQLiteDatabase db = this.getWritableDatabase();
result = db.delete(_TableName, _NotificationID + " = " + iNotificationiID, new String[] {});
db.close();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**
* this method will delete all the records from the database
*
* @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
*/
public boolean deleteAllNotifications()
{
int result = 0;
SQLiteDatabase db = this.getWritableDatabase();
result = db.delete(_TableName, null, new String[] {});
db.close();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**
* this method will return the number of records present in the database
*
* @return the number of records present in the database
*/
public int getNotificationsCount()
{
String countQuery = "SELECT * FROM " + _TableName;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public boolean markNotificationAsRead(int iNotificationiID)
{
// TODO Auto-generated method stub
int result = -1;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(_NotificationRead, 1);
// updating row
result = db.update(_TableName, values, _NotificationID + " = ?", new String[] { String.valueOf(iNotificationiID) });
db.close();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
public boolean markAllNotificationAsRead()
{
// TODO Auto-generated method stub
int result = -1;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(_NotificationRead, 1);
// updating row
result = db.update(_TableName, values, null, new String[] {});
db.close();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**
* gets all notification news from notifications database
*
* @return the list of the notification present in the database
*/
public ArrayList<NotificationCls> getAllUnreadNotifications()
{
ArrayList<NotificationCls> alNotifications = new ArrayList<NotificationCls>();
;
Cursor curData = null;
SQLiteDatabase db = null;
try
{
db = this.getReadableDatabase();
String strSelectQuery = "SELECT * FROM " + _TableName + " WHERE " + _NotificationRead + "=0";
curData = db.rawQuery(strSelectQuery, null);
if (curData.getCount() > 0)
{
curData.moveToFirst();
do
{
NotificationCls noti = new NotificationCls();
noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
alNotifications.add(noti);
}
while (curData.moveToNext());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (curData != null)
{
curData.close();
}
if (db != null)
{
db.close();
}
}
return alNotifications;
}
and this is the entity class:
public class NotificationCls
{
String strNotificationText;
int iNotificationiID;
boolean bNotificationRead = false;
public String getStrNotificationText()
{
return strNotificationText;
}
public void setStrNotificationText(String strNotificationText)
{
this.strNotificationText = strNotificationText;
}
public int getiNotificationiID()
{
return iNotificationiID;
}
public void setiNotificationiID(int iNotificationiID)
{
this.iNotificationiID = iNotificationiID;
}
public boolean isbNotificationRead()
{
return bNotificationRead;
}
public void setbNotificationRead(boolean bNotificationRead)
{
this.bNotificationRead = bNotificationRead;
}
}
to get all data from SQLite in arraylist write below code in your activity:
DatabaseHelper_Notification databaseHelper_notification = new DatabaseHelper_Notification(this);
ArrayList<NotificationCls> alNotifications = databaseHelper_notification.getAllNotifications();
I hope this helps you...
相关推荐
最新文章