【问题标题】:How to save a ListView into a xml file in Android?如何将 ListView 保存到 Android 中的 xml 文件中?
【发布时间】:2014-03-06 10:43:46
【问题描述】:

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

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 的适配器。 Adapter部分代码如下:

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 的所有代码!!!

【问题讨论】:

  • “将列表视图保存到文件中”到底是什么意思?
  • 我的应用程序在列表视图中显示在扫描期间检测到的 BLE 信号(还显示更多信息)。我想做的是将所有已显示的项目/信息保存到文件中。是不是更清楚了?
  • 您没有“将 ListView 保存到文件中”。您将模型数据保存到文件中。
  • 什么是模型数据?也许我在如何说出我需要什么方面有点错误。我需要的是将列表视图中每个项目的内容保存在一个文件中。这样比较好?
  • 在您的情况下,您希望保存“私人列表 数据”,因为它包含已发现的设备。

标签: android xml listview android-listview


【解决方案1】:

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

    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 将能够处理列表中的任何更改。

【讨论】:

  • 当我把你告诉我的那行丢失时,出现以下错误:“构造函数调用必须是构造函数中的第一个语句”。没有那条线,我的应用程序运行良好。但是关于我这个线程的问题,你知道listview的每一项的所有信息是怎么放到一个文件中的吗??
  • 对不起,我的错。我已经编辑了我的答案,这将修复错误。就保存到文件而言,我建议使用数据库或 SharedPreferences。
  • 它仍然给我一个问题:“构造函数 BaseAdapter(Activity, int, List) 未定义”。正如我之前告诉过你的,该应用程序运行良好。我使用了另一个类来实现 BaseAdapter。
【解决方案2】:

我将在这里做一些假设,如果我错了,请纠正我。 您可以从正在扫描设备的类访问您的适配器。我假设这就是您使用添加设备向适配器添加设备的方式。您可以在扫描完成时从此处调用适配器以获取您存储的设备列表,方法是在适配器上创建一个返回蓝牙设备列表的公共方法。 您拥有的第二个选项是,只要您将设备添加到列表中,只需在适配器中创建所需的 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);

【讨论】:

  • 您非常接近,但 addDevice 是在其他类中实现的(在本例中为 ScanBaseActivity),但正如您所说(我已修改代码以引入其他类)。正如您告诉我的那样,我不太了解您正在尝试如何做。我应该在哪里编写用于创建文件和保存列表的代码?我应该如何编写或应该使用哪些命令?很抱歉给您带来不便,但我还没有创作/写作,我有点迷茫。
  • 如果可能的话,我需要做第一种方法,在完成扫描后,它会保存列表。我应该在 StartScanLen 的 run 方法中实现这个,不是吗?
  • 是的,您可以在 postDelayed 处理程序的 run 方法中执行此操作。当扫描周期完成并且我们正在取消扫描时,它会运行。此时,适配器应该拥有所有蓝牙设备的详细信息,我们可以从中获取列表。
  • 但是列表会保存在 ScanBaseActivity 还是 ScanBleActivity 中? Zomb,你能编辑我的代码并告诉我你会怎么做吗?我将修改 ScanBaseActivity 和 ScanBleActivity 以显示所有缺少的代码,以便更清晰。
  • 我添加了一个编辑来向您展示代码的去向。在 ListAdapter 上创建一个公共方法以提供您的设备。然后只需使用该数据写入文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2011-10-10
相关资源
最近更新 更多