如何将ListView保存到Android中的xml文件中?

时间:2014-03-06 10:43:46

标签: android xml listview android-listview

我正在开发一个能够检测BLE信号的Android应用程序并将它们列在ListView中。扫描一段时间后,它将停止扫描。这是代码(它与开发人员页面中的代码相同):

ScanBleActivity.class(从ScanBaseActivty扩展):

public class ScanBleActivity extends ScanBaseActivity {

private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();


// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 20000;

  /* (non-Javadoc)
  * @see com.zishao.bletest.ScanBaseActivity#initScanBluetooth()
  */
 protected void initScanBluetooth() {
   BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
   mBluetoothAdapter = manager.getAdapter();
   startScanLen(true);
  }

  @Override
 protected void onDestroy() {
    super.onDestroy();
    if (mScanning) {
    startScanLen(false);
    }
}

  /**
  * 
  * @param enable
  */

  private void startScanLen(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
    new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            addDevice(device, rssi);

        }
    };

AddDevice在其他类中实现,在本例中为ScanBaseActivity:

ScanBaseActivity(从ListActivity扩展):

abstract public class ScanBaseActivity extends ListActivity {

protected LeDeviceListAdapter mLeDeviceListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices_scan);
mLeDeviceListAdapter = new LeDeviceListAdapter(this, new ArrayList<BluetoothDevice>());
this.setListAdapter(mLeDeviceListAdapter);
initScanBluetooth();
}

 /**
 * Start Scan Bluetooth
 * 
 */
abstract protected void initScanBluetooth();

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
BluetoothDevice device = (BluetoothDevice) mLeDeviceListAdapter.getItem(position);
ParcelUuid[] uuids = device.getUuids();
String uuidString = "Getting UUID's from " + device.getName() + ";UUID:";
if (null != uuids && uuids.length > 0) {
    uuidString += uuids[0].getUuid().toString();
} else {
    uuidString += "empty";
}
Toast.makeText(this, uuidString, Toast.LENGTH_LONG).show();
}

  /**
  * @param device
  */

protected synchronized void addDevice(final BluetoothDevice device, final int rssi) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mLeDeviceListAdapter.addDevice(device, rssi);
            mLeDeviceListAdapter.notifyDataSetChanged();
        }
    });
}
}

我检测到的所有信息(名称,地址,rssi等)都列在Listview中。对于ListView,我实现了一个名为BaseAdapter的适配器。适配器的部分代码如下:

public class LeDeviceListAdapter extends BaseAdapter {
private List<BluetoothDevice> data;
private Activity context;
private final HashMap<BluetoothDevice, Integer> rssiMap = new HashMap<BluetoothDevice,   Integer>();



public LeDeviceListAdapter(Activity context, List<BluetoothDevice> data) {
    this.data = data;
    this.context = context;
}

public synchronized void addDevice(BluetoothDevice device, int rssi) {
    if(!data.contains(device) ){
    data.add(device);
    }
    rssiMap.put(device, rssi);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (null == convertView) {
        LayoutInflater mInflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.leaf_devices_list_item, null);
        convertView.setTag(new DeviceView(convertView));
    }
    DeviceView view = (DeviceView) convertView.getTag();
    view.init((BluetoothDevice) getItem(position), null);
    return convertView;
}

public class DeviceView {



    private TextView title;
    private TextView status;
    private TextView type;
    private TextView address;
    private TextView rssivalue;

    public DeviceView(View view) {
        title = (TextView) view.findViewById(R.id.device_name);
        status = (TextView) view.findViewById(R.id.device_status_txt);
        type = (TextView) view.findViewById(R.id.device_type_txt);
        address = (TextView) view.findViewById(R.id.device_address_txt);
        rssivalue = (TextView) view.findViewById(id.signal_intensity_txt);
    }

当扫描完成后,我想将ListView保存到文件中(如果可以将其保存到xml文件中),但我不知道在项目中应该使用哪些代码和哪些代码来保存它。在文件中使用时间戳来知道它何时被保存也是很重要的。谁能帮助我或给我任何线索?

新功能:已编辑代码以显示ScanBaseActivity和ScanBleActivity的所有代码!!!

2 个答案:

答案 0 :(得分:0)

我发现你的构造函数存在一个问题。

    public LeDeviceListAdapter(Activity context, List<BluetoothDevice> data) {
    //EDIT::
    //add the following line
    super(context, R.layout.leaf_devices_list_item, data);

    this.data = data;
    this.context = context;

}

调用超级构造函数将数据引用传递给BaseAdapter。然后,BaseAdapter将能够处理列表中的任何更改。

答案 1 :(得分:0)

我将在这里做一些假设,如果我错了,请纠正我。 您可以从正在扫描设备的类中访问适配器。我假设您正在使用添加设备将设备添加到适配器。通过在适配器上创建返回BluetoothDevice列表的公共方法,您可以在扫描完成时从此处调用适配器以获取已存储的设备列表。 第二个选项是在将设备添加到列表时只在适配器中创建所需的xml文件。周围应该没有很多设备,所以你应该有相当少的读写。写入xml文件应遵循此答案中的步骤。 Android creating and writing xml to file

(编辑) 你会看到类似的东西:

public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                //Add the call here to save the file
                // You should be able to access the list adapter from your base class
                // that will give you the devices 
            }
        }, SCAN_PERIOD);
相关问题