在listview中显示sqlite内容

时间:2015-03-19 12:45:23

标签: android database sqlite listview android-sqlite

我是android的新手。我有2个表用于存储和显示微调器数据,另一个表用于存储选定的微调器值现在我想以listview的形式显示sqlite数据库表内容,用于android`中的以下代码。



private View.OnClickListener mCorkyListener = new View.OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked

            Log.d("button clicked", "button clicked");


            String mac_no = my_spinners.getSelectedItem().toString();
            String incharge = my_spinnerssss.getSelectedItem().toString();
            String operator_name = my_spinnerss.getSelectedItem().toString();
            String status_of_mac = my_spinnersss.getSelectedItem().toString();
            String fullName = mac_no + " " + status_of_mac;
            Log.d("fullname", fullName);

            try {
                mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
                // mydb.delete(TABLES, null, null);
                mydb.execSQL("CREATE TABLE IF  NOT EXISTS " + TABLES
                        + " (ID INTEGER PRIMARY KEY, DATE_TIME TEXT, MACHINE_NO INTEGER, OPERATOR_NAME TEXT, STATUS TEXT, INCHARGE TEXT);");
                Calendar c = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String strDate = sdf.format(c.getTime());
                Toast.makeText(getApplicationContext(), "The String Date is " + strDate, Toast.LENGTH_LONG).show();
                ContentValues data = new ContentValues();
                data.put("ID", ID);
                data.put("DATE_TIME", strDate);
                data.put("MACHINE_NO", mac_no);
                data.put("OPERATOR_NAME", operator_name);
                data.put("STATUS", status_of_mac);
                data.put("INCHARGE", incharge);
                if (mydb == null) {
                    // make sure that your db is properly initialised
                }
                mydb.insert("RECORD_TABLES", "nullColumnHack", data);
                Log.d("Inserted into record table", "Inserted into record table");

                mydb.close();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error in creating table",
                        Toast.LENGTH_LONG);
            }
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) { // Device does not support Bluetooth }
            }
            Log.d("Bluetooth Adapter is active", "Bluetooth Adapter is active");
            try {
                if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, 1);
                    Log.d("Intent is enabled", "Intent is enabled");
                }
                Log.d("Intent is correct", "Intent is correct");
            } catch (NullPointerException e) {
            }
            try {


                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                if (pairedDevices.size() > 0) {
                    for (BluetoothDevice device : pairedDevices) {
                        BluetoothDevice mDevice = device;
                        String name = mDevice.getName();
                        Log.d("mDevice", name);

                        ConnectThread mConnectThread = new ConnectThread(mDevice, fullName);
                        mConnectThread.start();
                        exportDB();
                        try {
                            mConnectThread.write(fullName);
                        } catch (IOException nn) {
                        }


                    }
                }

            } catch (NullPointerException n) {
            }


        }


    };
&#13;
&#13;
&#13;

`

1 个答案:

答案 0 :(得分:0)