• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

updating listView as data is received

Nightpoison

Newbie
Aug 29, 2019
11
1
So I'm attempting to write my first android application to support another project I'm working on. I send out a command to a USB attached device. The data packets are sent back over the wire, received, parsed, added to a database, and supposed to be added to an adapter in order to display in a list view. I attempted to follow instructions I found on a skill share video in order to implement the adapter. I was able to get the application from skill share working, an rss feed, but I'm attempting to make the same functionality work, but as data comes in and stored into the database, the adapter is updated to display the most recent data on the list view.

I have the buttons, send and receive, and a couple other things working in the main activity thread. As I receive data from the attached device I'm adding packets to a Queue.

Java:
@Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UsbService.MESSAGE_FROM_SERIAL_PORT:
                    String packet = (String) msg.obj;

                    mActivity.get().dataPacket.append(packet);

                    if(lastCMD.equals("R07") || lastCMD.equals("R08" ))
                    {

                        if(mActivity.get().dataPacket.length() > 455)
                        {
                            String data = mActivity.get().dataPacket.toString();

                            data = data.replaceAll("[\\^\\*:]", "");

                            String[] split = data.split("\\r?\\n");

                            for(int i = 0; i < split.length; i++)
                            {

                                mActivity.get().dataR.add(split[i]);
                            }
                        }
                    }
...

I have another class running as its own thread. This thread handles the data that's stored into the Queue. If the Queue has data it will follow the operations to parse the packet lines and add them to the database.

Java:
class OperationThread extends Thread
{

    private MainActivity obj;

    private String data;
    private String time;
    private String temp;
    private String conc;
    private String batchNum;
    private String fileNum;
    private float temporary;

    SensorRecord record;

    OperationThread(MainActivity obj)
    {

        this.obj = obj;
    }

    public void run()
    {

        try
        {
            Thread.sleep(500);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        super.run();

        while (!Thread.currentThread().isInterrupted())
        {

            if(!obj.dataR.isEmpty())
            {
                try
                {
                    data = obj.dataR.remove();

                    data = data.replaceAll("\\s", "");

                    String[] breakout = data.split("-");
                    // all set to this point
                    if(breakout.length == 12)
                    {

                        batchNum = breakout[0];
                        fileNum = breakout[1];

                        time = breakout[2] + ":" + breakout[3] + ":" + breakout[4] + ":" + breakout[5] ;

                        if(checksum(breakout[8]))
                        {
                            temporary = (float) Integer.parseInt(breakout[8].substring(breakout[8].length()-4, breakout[8].length()-2), 16);
                            temporary = temporary * (float) .1;
                            temp = String.valueOf( temporary);
                        }
                        else
                            temp = "Chksm Err";

                        if(checksum(breakout[11]))
                        {
                            temporary = (float) Integer.parseInt(breakout[11].substring(breakout[11].length()-4, breakout[11].length()-2));
                            //temporary = temporary *
                            conc = String.valueOf(temporary);
                        }
                        else
                            conc = "Chksm Err";

                        record = new SensorRecord(batchNum, fileNum, time, temp, conc);

                        obj.db.addSensorRecord(record);

                   }
                }
                catch (NoSuchElementException e)
                {}

                obj.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run ()
                    {
         
                        obj.adapter.notifyDataSetChanged();
                    }
                });
            }// end of if
        }
    }

    private boolean checksum(String data)
    {

        int length = data.length();
        boolean ck = false;
        int checksum = 0;
        int n = 0;

        String iChksm = data.substring(data.length()-2);

        for (n = 0; n < length - 2; n++)
        {
            checksum = checksum + data.charAt(n);
        }
        checksum = 255 - checksum + 1;
        checksum = checksum + 256 + 256;

        if (Integer.parseInt(iChksm, 16) == checksum)
            ck = true;

        return ck;
    }
}

I use runOnUiThread to add new requests to the stack of UI Thread. The call I thought would work was the notifyDataSetChanged(). But it doesn't update with any of the data coming in over the wire. All I see is blank lines, hundreds of them.

I create and set the adapter in the onCreate of the activity.

All functionality of the main activity has worked thus far. But after hitting the Read Sensor button, nothing populates in the List view and then, all functionality from Sync or the settings activity fail to work.

Could someone lend some insight as to where I should be looking. I'm not getting any errors, Its hard to debug when Its not connected to Android Studio. With that being said, I have a second application that I use to test all of the data parsing and verify that the data coming in over the wire is what its suppose to be. I have tested all the calls, send/receive, parsing, and etc on this second application first.

Here is the Adapter class

Java:
package com.ascenzi.rhmps;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class SensorFeedAdapter extends ArrayAdapter<SensorRecord>
{
    Context context;
    String aBatchNum;
    String aFileNum;
    String aTime;
    String aTemp;
    String aConc;


    public SensorFeedAdapter (Context c, List<SensorRecord> records)
    {
        super(c, 0, records);


    }


    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {

        SensorRecord record = getItem(position);

        if (convertView == null)
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.sensor_feed_item_row, parent, false);

        TextView batch =convertView.findViewById(R.id.batchTextView);
        TextView file = convertView.findViewById(R.id.fileTextView);
        TextView time = convertView.findViewById(R.id.timerTextView);
        TextView temp = convertView.findViewById(R.id.tempTextView);
        TextView conc = convertView.findViewById(R.id.concenTextView);

        batch.setText(aBatchNum);
        file.setText(aFileNum);
        time.setText(aTime);
        temp.setText(aTemp);
        conc.setText(aConc);

        return convertView;
    }
}

and here is the instantiation of the adapter class.

Java:
        adapter = new SensorFeedAdapter(this, records);
        sensorFeedList.setAdapter(adapter);
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones