0

The app works ok, but computing each listview entry takes some time (I'm doing a ping on network devices) so I'm looking for creating entries on the fly. Although I google for a solution, I'm lost: I can't find where I need to modify my code.

Also I'd like to have a textview or a progressbar to indicate the progress but all the computing is taken by the listview and I only get that ino at the end. Explanations or a tutorial would be much appreciated on these problems.

listIPitem = new ArrayList<>();

iPadaptater = new IPadaptater(getActivity(), listIPitem);
ListView list_ip = (ListView) rootView.findViewById(id.listIP);
list_ip.setAdapter(iPadaptater);
DiscoverNetwork dn = new DiscoverNetwork();
dn.passContext(context, listIPitem, iPadaptater)

Here is my adaptater:

public class IPadaptater extends BaseAdapter {

    private List<IPitem> listIPitem;
    private LayoutInflater layoutInflater;
    Context context;

    public IPadaptater(Context c, List<IPitem> objects) {
        context = c;
        listIPitem = objects;
        layoutInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return listIPitem.size();
    }

    public Object getItem(int position) {
        return listIPitem.get(position);
    }

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

    private class ViewIPHolder {
        TextView ip_disc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewIPHolder viewHolder;

        if(convertView == null) {
            viewHolder = new ViewIPHolder();

            convertView = layoutInflater.inflate(R.layout.listview_item_discovery, null);

            viewHolder.ip_disc = (TextView) convertView.findViewById(R.id.ip_disc);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewIPHolder) convertView.getTag();
        }
        viewHolder.ip_disc.setText(listIPitem.get(position).getIPaddress());

        notifyDataSetChanged();

        return convertView;
    }

}

And here is where I process my data:

   listIPitem.clear();


    for(int i=1;i<50;i++) {

        IPitem iPitem = new IPitem();
        ip = network + i;
        if(!ping(ip)) continue;
        iPitem.setIPaddress(ip);
        listIPitem.add(iPitem);
        iPadaptater.notifyDataSetChanged();
    }
Ahad
  • 674
  • 1
  • 9
  • 22
narb
  • 958
  • 1
  • 13
  • 39
  • 1) use a RecyclerView if you want to animate the insertion of data 2) don't need to call `notifyDataSetChanged` in `getView`. 3) that for loop is going to iterate over all 50 elements in less than a few seconds, so what exactly are you wanting to see? – OneCricketeer Oct 29 '16 at 17:38
  • Also, what is `ping` doing? If it's an AsyncTask, that is going to take time. Regarding the progress bar, do you have one in your XML layout? – OneCricketeer Oct 29 '16 at 17:41
  • Well it should go over a netwok 254 devices. That could take somewhere between 15sec to potentially a couple of minutes. I was going to use the isReachable method but it's not reliable. So I was thinking going down the execution of a ping command. – narb Oct 29 '16 at 17:48
  • Thanks for the comments: I'll try to search for the recycleviewer. ok for comment 2. – narb Oct 29 '16 at 17:49
  • Yeah, I didn't see the `ping` method at first, so yes, it may take longer. I think `isReachable` is the equivalent of ping – OneCricketeer Oct 29 '16 at 17:51
  • Maybe I misunderstood what you wanted, but regarding animation.. http://stackoverflow.com/questions/28986269/recyclerview-how-to-create-insert-animation-effect – OneCricketeer Oct 29 '16 at 17:54
  • just to be clear, because it can take a long time, I want to show the entry in the listview that corresponds to the pingable device. I don't want to wait till the end to show the complete listview. – narb Oct 29 '16 at 17:57
  • I want to show them 1 by 1.... – narb Oct 29 '16 at 17:57
  • Okay, I understood, then. I just don't think the `ping` method is blocking or is on another thread, for example – OneCricketeer Oct 29 '16 at 18:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126971/discussion-between-narb-and-cricket-007). – narb Oct 29 '16 at 18:56

0 Answers0