我不知道如何打印存儲在SQLite的Android上如何打印數據表中的SQLite在Android
首先,數據表,我寫的數據庫文件:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Localization";
// Contacts table name
private static final String TABLE_CONTACTS = "RSSIMAP";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "device_name";
private static final String KEY_RSSI = "RSSI";
private static final String KEY_DIST = "distance";
private static final String KEY_TIME = "time_stamp";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_RSSI + " TEXT," + KEY_DIST +" TEXT,"+ KEY_TIME +" TEXT"+")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getDeviceName()); // device name
values.put(KEY_RSSI, contact.getRSSI()); // RSSI
values.put(KEY_DIST, contact.getDistance()); //
values.put(KEY_TIME, contact.getTime());
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_RSSI, KEY_DIST,KEY_TIME }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3), cursor.getString(4));
// return contact
return contact;
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setDeviceName(cursor.getString(1));
contact.setRSSI(cursor.getString(2));
contact.setDistance(cursor.getString(3));
contact.setTime(cursor.getString(4));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getDeviceName());
values.put(KEY_RSSI, contact.getRSSI());
values.put(KEY_DIST, contact.getDistance());
values.put(KEY_TIME, contact.getTime());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
然後,我已經插入到數據庫中的某些行,但我仍然需要編寫一個活動,打印表格,但我很困惑,如何把它(使用的GridView或ListView?)..謝謝寫了很多
public class Display extends Activity {
private ArrayList<String> results = new ArrayList<String>();
DatabaseHandler ins_rssi_dis = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
List<Contact> contacts = ins_rssi_dis.getAllContacts();
}
然後,我使用ListV IEW顯示數據
public class Display extends ListActivity {
DatabaseHandler datasource = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
List<Contact> datadisplay = datasource.getAllContacts();
ArrayAdapter<Contact> adapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_1, datadisplay);
ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
}
但結果是不正確的,如:
聯繫@ 41e5cd78 聯繫@ 41e65ec8
我猜getAllContacts()方法是不正確的,但我不是確保...
的activity_display.xml是:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Display" >
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
這取決於你的數據,但我認爲你需要'ListView',你必須閱讀文檔,看看哪一個更適合你 – 2014-11-05 05:44:07
打印,因爲你想打印它們在LOG?或想要在屏幕上顯示它們? – Darpan 2014-11-05 06:17:14
什麼試圖打印 – 2014-11-05 06:21:21