2014-11-05 35 views
1

我不知道如何打印存儲在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> 
+0

這取決於你的數據,但我認爲你需要'ListView',你必須閱讀文檔,看看哪一個更適合你 – 2014-11-05 05:44:07

+0

打印,因爲你想打印它們在LOG?或想要在屏幕上顯示它們? – Darpan 2014-11-05 06:17:14

+0

什麼試圖打印 – 2014-11-05 06:21:21

回答

0

你看[email protected] [email protected]因爲你是顯示Contact對象作爲字符串。

實現在Contact類的toString()方法:

@Override 
public String toString() { 
    return this.firstName+" "+this.lastName; // example 
} 

如果一切在你的代碼是OK(我沒有檢查),你會看到每個聯繫人的姓氏和名字你列表顯示。

+0

它的工作原理!多謝 ! – swing 2014-11-05 09:09:33

0

個類似的教程在這裏:

http://www.vogella.com/tutorials/AndroidSQLite/article.html

http://www.tutorialspoint.com/android/android_sqlite_database.htm

他們正在使用的ListView,和你要打印的數據也期待同樣的,如果你有這個打算,你可能需要編寫一個適配器除了你的Activity類,還有類。 也有顯示的ListView兩個XML佈局,還可以在ListView

下面是示例代碼爲您顯示列表項:試試吧,我hvn't測試這一點,可能有一些修正,但這個想法是存在的。

ContactListAdapter類:

public class ContactListAdapter { 

private final Activity context; 
List<Contact> contactAdapter = new ArrayList<Contact>(); 

public ContactListAdapter(Activity context,List<Contact> contactlist) { 

     super(context, R.layout.download_item, downloadList); 
     this.context = context; 
     this.contactAdapter = contactlist; 

} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 

    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.cotact_item, null, true); 

    TextView txtTitle1 = (TextView) rowView.findViewById(R.id.item1); 
    TextView txtTitle2 = (TextView) rowView.findViewById(R.id.item2); 
    TextView txtTitle3 = (TextView) rowView.findViewById(R.id.item3); 
    TextView txtTitle4 = (TextView) rowView.findViewById(R.id.item4); 
    TextView txtTitle5 = (TextView) rowView.findViewById(R.id.item5); 

    txtTitle1.setText(contactAdapter[position].getId()); 
    txtTitle2.setText(contactAdapter[position].getName()); 
    txtTitle3.setText(contactAdapter[position].getRssi()); 
    txtTitle4.setText(contactAdapter[position].getDistance()); 
    txtTitle5.setText(contactAdapter[position].getTime()); 


    return rowView; 
} 
} 

顯示

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); 

    ContactListAdapter adapter = new ContactListAdapter(Display.this, 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; 
} 
} 

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="5dp" 
android:paddingLeft="5dp" 
android:paddingRight="5dp" 
android:paddingTop="5dp" 
tools:context=".RecentActivity" > 
<ListView 
    android:id="@+id/main_download_list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</ListView> 
</RelativeLayout> 

contact_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TableRow 
    android:layout_width="fill_parent"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android: android:orientation="vertical"  

    <TextView 
     android:id="@+id/item1" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="id" /> 
    <TextView 
     android:id="@+id/item2" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="name" /> 
    <TextView 
     android:id="@+id/item3" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="rssi" /> 
    <TextView 
     android:id="@+id/item4" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="distance" /> 
    <TextView 
     android:id="@+id/item5" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="time" /> 
    </LinearLayout> 
    </TableRow> 
    </TableLayout> 
+0

我使用ListView來顯示,但結果是「@ 41e5cd78 ..」我認爲我的getAllContacts()方法可能是錯誤的....但我不確定... – swing 2014-11-05 07:04:48

+0

您是否嘗試將結果打印到控制檯或Logcat?你如何傳遞這些數據?請張貼這些代碼以供參考。希望將參數傳遞給適配器分區的問題..如果使用,則不會返回String對象的值! – 2014-11-05 07:10:13

+0

我只是在平板電腦上打印它,我不知道如何在日誌貓或控制檯上打印它...其他方法,如插入,更新似乎很好,只有打印不能工作.. – swing 2014-11-05 07:13:11

相關問題