如何在我的应用程序中自动刷新ListView?

时间:2018-06-27 10:11:24

标签: android listview bluetooth-lowenergy

我正在建立一个活动,该活动可以从BLE设备下载一些数据。现在,我想实时在我的活动的ListView中使用此值。 但是我无法做到这一点。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble);

        listView = (ListView) findViewById(R.id.list_view);
        listView.setOnScrollListener(new OnListListener());
        adapter = new SensorDataAdapter(classe, R.layout.sensordata_row, new LinkedList<SensorData>());
        listView.setAdapter(adapter);
        refreshListViewByPosition(null);
        adapter.notifyDataSetChanged();
}

public void refreshListViewByPosition(Integer pos) {
        String tipoCaratteristica=null;
        if(pos!=null && pos ==0)
            tipoCaratteristica = CHARACTERISTIC_FORZA_STRING;
        else if(pos!=null && pos ==1)
            tipoCaratteristica = CHARACTERISTIC_TEMPERATURA_STRING;
        listaDati = db.fetchSensorData(tipoCaratteristica);
        if(listaDati== null || listaDati.size() == 0){
            listaDati = new LinkedList<SensorData>();
            SensorData s = new SensorData("NESSUN ELEMENTO MEMORIZZATO");
            listaDati.add(s);
        }
        adapter.getLista().clear();
        adapter.getLista().addAll(listaDati);
        // fire the event
        adapter.notifyDataSetChanged();
    }

现在,当我检索特征值时,我将拥有该代码:

@Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            //RECUPERO IL CORPO DEL MESSAGGIO
            try{
                ByteBuffer buffer = ByteBuffer.wrap(characteristic.getValue()).order(ByteOrder.LITTLE_ENDIAN);

                byte[] value=buffer.array();

                int valore1 = new Integer(value[3]) * 256 + new Integer(value[2]);
                int valore2 = new Integer(value[5]) * 256 + new Integer(value[4]);
                int valore3 = new Integer(value[7]) * 256 + new Integer(value[6]);
                int valore4 = new Integer(value[9]) * 256 + new Integer(value[8]);
                int valore5 = new Integer(value[11]) * 256 + new Integer(value[10]);
                int valore6 = new Integer(value[13]) * 256 + new Integer(value[12]);
                int valore7 = new Integer(value[15]) * 256 + new Integer(value[14]);
                int valore8 = new Integer(value[17]) * 256 + new Integer(value[16]);

        SensorData mSenData = new SensorData(contatore,valore1+"",valore2+"",valore3+"",
                        valore4+"",valore5+"",valore6+"",valore7+"",valore8+"",
                        Constants.CHARACTERISTIC_FORZA_STRING,
                        db.getDateTime(true));
                mSenData.setValueTimestamp(db.getDateTime(true));

                refreshListView(mSenData);

                db.insertSensorData(mSenData);
            }catch(Exception e){
                Log.e(TAG, e.getMessage());
            }

        }

在此方法中,我具有refreshListView方法,并将新的SensorData对象传递给它。这是方法:

public void refreshListView(SensorData newRelevation) {
        adapter.getLista().add(0,newRelevation);
        adapter.notifyDataSetChanged();
    }

但是,如果我尝试启动此代码,则没有任何错误,但listview不会自动刷新。

考虑我每5到10毫秒会收到一个新值。

1 个答案:

答案 0 :(得分:0)

我建议使用ScheduledExecutorService,因为它非常准确并且可以将UI卸载到线程中。这是一个示例:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 1 );

然后调用此函数:

 private final int INTERVAL = 1000; // milli-seconds

private void scheduleAtFixedRate() {
    final Runnable r = new Runnable() {
        public void run() {
            adapter.notifyDataSetChanged();
        }
    };
    scheduler.scheduleAtFixedRate(r, 0, INTERVAL , MILLISECONDS);
}

onDestroy通话中:

scheduler.shutdown();