如何从listview获取数据并将其放入数据库android

时间:2016-07-30 09:30:46

标签: java android listview bluetooth

做了一个蓝牙扫描应用程序,我想在数据库中添加那些scaned蓝牙,就像我需要一个按钮当用户点击该按钮所有scaned蓝牙在列表视图中同时去数据库我是java和android的新手 任何帮助将不胜感激

    package com.example.test.test;

    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.Toast;

    import java.util.Set;

     public class MainActivity extends Activity {


           Spinner spinner;
          ArrayAdapter<CharSequence> adapter;
          ArrayAdapter<CharSequence>adapter1;
         ArrayAdapter<CharSequence>adapter2;

          ListView listDevicesFound;
           Button btnScanDevice;
            BluetoothAdapter bluetoothAdapter;
          int REQUEST_CODE = 1;

         ArrayAdapter<String>btArrayAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        spinner=(Spinner) findViewById(R.id.spinner);
               adapter=ArrayAdapter.createFromResource(this,R.array.DEPARTMENT,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner=(Spinner) findViewById(R.id.spinner2);
        adapter1=ArrayAdapter.createFromResource(this,R.array.batch,android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter1);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner=(Spinner) findViewById(R.id.spinner3);
        adapter2=ArrayAdapter.createFromResource(this,R.array.group,android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter2);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btnScanDevice = (Button)findViewById(R.id.scandevice);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        listDevicesFound = (ListView)findViewById(R.id.devicesfound);
        btArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
        listDevicesFound.setAdapter(btArrayAdapter);
        CheckBlueToothState();

        btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener);

        registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();unregisterReceiver(ActionFoundReceiver);
    }
    private void CheckBlueToothState()
    {
        if (bluetoothAdapter == null)
        {
            Toast.makeText(getBaseContext(),"Bluetooth Not Supported",Toast.LENGTH_LONG).show();

        }
        else
        {
            if (bluetoothAdapter.isEnabled())
            {
                if(bluetoothAdapter.isDiscovering())
                {
                    Toast.makeText(getBaseContext(),"Bluetooth is currently in device discover process",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getBaseContext(),"Bluetooth is Enabled",Toast.LENGTH_LONG).show();
                    btnScanDevice.setEnabled(true);
                }
            }
            else
            {

                    Toast.makeText(getBaseContext(), "Bluetooth is not Enabled", Toast.LENGTH_LONG).show();
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_CODE);

            }
        }
    }
    private Button.OnClickListener btnScanDeviceOnClickListener= new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {

            btArrayAdapter.clear();
            if(bluetoothAdapter.startDiscovery())
            {
                Toast.makeText(getBaseContext(),"Scanning", Toast.LENGTH_LONG).show();
            }

        }};

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE)
        {
            CheckBlueToothState();
        }
    }
    private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                btArrayAdapter.notifyDataSetChanged();
            }
        }};
}

1 个答案:

答案 0 :(得分:0)

你可以这样做: 添加数据baseHelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;


public class DatabaseHelper extends SQLiteOpenHelper {

private final String TABLE_NAME = "scans"

private final String KEY_DATA = "data"

public DatabaseHelper(Context context) {
    super(context, "BluetoothData", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("
            + BaseColumns._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATA + " VARCHAR )");
}

public void addScanRecord(String record){
    SQLiteDatabase database = this.getWritableDatabase();
    db.execSQL("INSERT INTO " + TABLE_NAME + " ( " + KEY_DATA+ ") VALUES ('" + record + "')");
    database.close();
}

}

将getter添加到适配器:

public List<String> getAllData(){
      return myAdapterList;
}

的OnClick:

List<String> bluetoothRecords = btArrayAdapter.getAllData();

DatabaseHelper  bdHelper= new DatabaseHelper (this);
for(String record : bluetoothRecords){
    bdHelper.addScanRecord(record);
}