0

I am reading UDP packets and i wanna display that info on UI as table in android app. Here is my code,

    try { 
            byte buffer[] = new byte[10000];<br/>
            InetAddress address = InetAddress.getByName("192.168.xx.xx");<br/>
            int port = xxx;<br/>
            Log.d("..........","What will Happen ?? ");<br/>
            for(int k=0;k<50;k++) { // 50 rows are added , This i wanna make it 5000+ rows so it takes plenty of time to load that table <br/>
                DatagramPacket p = new DatagramPacket(buffer, buffer.length, address, port);<br/>
                DatagramSocket ds = new DatagramSocket(port);<br/>
                Log.d("..........","Perfect Binding .... Waiting for Data");<br/>
                ds.receive(p);<br/>
                Log.d("..........","Packet Received");<br/>
                byte[] data = p.getData();<br/>
                String result = "";<br/>
                int b[] = new int[data.length];</br>
                for (int i=0; i < 150; i++) {<br/>
                    result += Integer.toString( ( data[i] & 0xff ) + 0x100, 16).substring( 1 );<br/>
                    result += "_";<br/>
                }<br/>

                Log.d("Result => ",result); <br/>
                TableLayout tl=(TableLayout)findViewById(R.id.TableLayout01);<br/>
                TableRow tr=new TableRow(this);
                TextView tv= new TextView(this);
                TextView tv2 = new TextView(this);
                tv.setPadding(5, 0, 5, 0);
                tv2.setPadding(5,0,5,0);

                String k1 = Integer.toString(k);
                tv.setText(k1);
                tv2.setText(it_version);
                tr.addView(tv);
                tr.addView(tv2);

                tl.addView(tr,1);
                ds.close();
            }     


        } catch (Exception e) {
            Log.e("UDP", "Client error", e);
        }

If i keep 50 rows am able to display it properly without any time delay, if i put 3000 rows its taking too long time and sometimes app is hanging... I wanna add 50 entries to a table and load the table and again read 50 entries and append to the table without touching any button or anything so i have a table in UI and it will update automatically by reading UDP packets ... how i can achieve that ?? Any clue appreciated.

or once i read the UDP packet i wanna display it on UI[appending to the table],How i can do this ??[Scrolling and all i will take care] please let me know I already tried using threads but no use

Mahesh
  • 705
  • 3
  • 8
  • 17
  • Please format your code correctly. – Jonathon Reinhart Jun 25 '13 at 05:43
  • Yes ... I format the code now @JonathonReinhart – Mahesh Jun 25 '13 at 05:45
  • 1
    "I already tried using threads but no use" what do you mean? You should use AsyncTask. – Michał Z. Jun 25 '13 at 05:49
  • 1
    I have successfully done a infinite list with ListView. It's not hard. Same concepts apply here. You want AsyncTask or threads to load the data from the socket, then when you have new data notify your UI thread, try running a runnable from the network thread to the uithread via Activity.runOnUithread(runnable), in that runnable call a function of the activity and pass in the data so the activity can add a view. Something like this would, and I have made, work. – WIllJBD Jun 25 '13 at 05:58
  • @MichałZ. if i use Asnc task both are running simultaneously , Not able to read the UDP packet details for table row – Mahesh Jun 25 '13 at 06:15
  • @WIllJBD If u don't mind can i see your code for reference ?? – Mahesh Jun 25 '13 at 06:17

2 Answers2

1

Basically, you need to implement an infinite listview. There are a couple strategies to do this:

  • You can get all the data and store it in a database and only show the user 50 at a time.
  • You can fetch only 50 at first and then fetch the next 50 when the user scrolls past them.
  • You can fetch 100, show 50 and then show next 50 when the user scrolls past the first 50. Pre-fetch the next 100 to show next and so on.

Once you figured out your fetching strategy, you need to implement the actual adapter and listview. Here's a good technique to do this. I would recommend that you don't re-invent the wheel and use this great library called EndlessAdapter unless you want to implement it for learning purposes.

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Plus one for saying to use what is already there, but infinite lists is such a tiny simple thing that I usually like to just make my own. But I do agree that for more complex things like WheelView that it is much better to just use a existing library. – WIllJBD Jun 25 '13 at 06:48
0

Something like this is what you might use in order to get a infinite list effect when you don't have a cursor.

Please note this is a very rough draft since I deleted the code only relevant to my app, to help for you clarity, and for my privacy and the apps privacy. Also it may not be the best way of doing everything, but it worked the first time I wrote it (which took like 10 minutes) and worked beautifully for a very complex list, so I haven't bothered coming back to it.

    class AsyncGetUpdates extends AsyncTask<Void, Void, List<UpdateDTO>>
    {

        @Override
        protected void onPreExecute()
        {
            showDialog();
            super.onPreExecute();
        }

        @Override
        protected List<UpdateDTO> doInBackground(Void... params)
        {
            return APIHelper.getUpdates(count);
        }

        @Override
        protected void onPostExecute(List<UpdateDTO> result)
        {
            killDialog();
            isCurrentlyUpdating = false;
            setAdapterData(result);
            super.onPostExecute(result);
        }
    }

    public void setAdapterData(List<UpdateDTO> result)
    {
        killDialog();
        if (this != null && this.getActivity() != null)
        {
            Log.d(TAG, "setAdapterData");
            if (lvUpdatesList.getAdapter() != null)
            {
                // save index and top position
                int index = lvUpdatesList.getFirstVisiblePosition();
                View v = lvUpdatesList.getChildAt(0);
                int top = (v == null) ? 0 : v.getTop();

                updateListAdapter = new UpdateListAdapter(this.getActivity().getLayoutInflater(), result, this);
                lvUpdatesList.setAdapter(updateListAdapter);
                lvUpdatesList.refreshDrawableState();

                lvUpdatesList.setSelectionFromTop(index, top);
            }
            else
            {
                updateListAdapter = new UpdateListAdapter(this.getActivity().getLayoutInflater(), result, this);
                lvUpdatesList.setAdapter(updateListAdapter);
                lvUpdatesList.refreshDrawableState();
            }
        }

        // add in a listener to know when we get to the bottom
        lvUpdatesList.setOnScrollListener(new OnScrollListener()
        {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState)
            {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
            {
                // we do not want to update if we already are
                if (isCurrentlyUpdating == false)
                {
                    if (lvUpdatesList.getAdapter() != null && lvUpdatesList.getAdapter().getCount() == count)
                    {
                        final int lastItem = firstVisibleItem + visibleItemCount;
                        if (lastItem == totalItemCount)
                        {
                            isCurrentlyUpdating = true;
                            // add to the count of views we want loaded
                            count += 20;
                            // start a update task
                            new AsyncGetUpdates().execute();
                        }
                    }
                }
            }
        });
    }

Finally I would like to say that copy pasting might get you the results you want, but it will hinder you future ability. I would say study, read, learn, try, fail, and try again.

WIllJBD
  • 6,144
  • 3
  • 34
  • 44