1

In my app I store 8 static dns fields in my database.This just a name value pair.And on button click I get name and ip addresses stored in database into arraylist and display them in listview using base adapter. Everything works fine but after pressing button it almost takes 4-5 seconds to show listview . Is this time taken is normal or should be less?? Moreover I am also adding items in that list from other part of code therefore on button click everytime I have to get list from database again. If this time is more and if not what can I do to reduce this time??

here is the code that runs on button click

public void LoadPrimaryDnsList(){

    dnsName=dataBaseHelper.getDnsName();
    ipAddress=dataBaseHelper.getIpAddress();
    id=dataBaseHelper.getId();
    dnsListAdapter = new DnsListAdapter(getActivity(), dnsName, ipAddress,id);
    dnsListAdapter.notifyDataSetChanged();
    android.support.v7.app.AlertDialog.Builder builder;
    final android.support.v7.app.AlertDialog alertDialog;
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View layout = inflater.inflate(R.layout.browse_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_dialog));
    listView = (ListView) layout.findViewById(R.id.dns_list);

    builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
    builder.setView(layout);
    alertDialog = builder.create();

    alertDialog.show();
    listView.setAdapter(dnsListAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String ip = ipAddress.get(position);
            EDITDNS1.setText(ip);
            alertDialog.dismiss();
        }

    });
}

And this is the adapter class

public class DnsListAdapter extends BaseAdapter {
    Context context;

    LayoutInflater layoutInflater;

    ArrayList<String> dnslist=new ArrayList<>();
    ArrayList<String> iplist=new ArrayList<>();
    ArrayList<String> ids=new ArrayList<>();
    public DnsListAdapter(Context context, ArrayList<String> dns, ArrayList<String> ip,ArrayList<String> id) {
        this.context = context;
        dnslist=dns;
        iplist=ip;
        ids=id;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return dnslist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.dialog_row, parent, false);
            holder.tv_name = (TextView) convertView.findViewById(R.id.dnsName);
            holder.tv_ip= (TextView) convertView.findViewById(R.id.ip_address);
            holder.iv_cross= (ImageView) convertView.findViewById(R.id.iv_cross);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(dnslist.get(position));
        if (holder.tv_name.getText().toString().equalsIgnoreCase("Google 1")||holder.tv_name.getText().toString().equalsIgnoreCase("Google 2")||holder.tv_name.getText().toString().equalsIgnoreCase("Level3 1")
                ||holder.tv_name.getText().toString().equalsIgnoreCase("Level3 2")||holder.tv_name.getText().toString().equalsIgnoreCase("DNS Watch 1")||holder.tv_name.getText().toString().equalsIgnoreCase("DNS Watch 2")
                ||holder.tv_name.getText().toString().equalsIgnoreCase("OpenDNS 1")||holder.tv_name.getText().toString().equalsIgnoreCase("OpenDNS 2"))
        {
            holder.iv_cross.setVisibility(View.INVISIBLE);
        }
        holder.tv_ip.setText(iplist.get(position));
        Fonts.setHelveticaFont(getActivity(), holder.tv_name);
        Fonts.setHelveticaFont(getActivity(), holder.tv_ip);
        holder.iv_cross.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBaseHelper.deleteRow(id.get(position));
                dnsName.remove(position);
                ipAddress.remove(position);
                id.remove(position);
                dnsListAdapter.notifyDataSetChanged();

            }
        });
        return convertView;
    }

}
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

2 Answers2

1

I suggest you load your data and populate the ListView asynchronously using AsyncTask.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
  • that would be a work around for it like showing user loading while data loads . I want to know that am I doing something wrong or doing this work will take this much time – Vivek Mishra Dec 23 '15 at 09:36
  • It's an efficient way. unless you just want to append something directly using your adapter, i.e. `adapter.addItem()` – Kyle Emmanuel Dec 23 '15 at 09:45
  • so this means this much time will be required to fetch data and show it – Vivek Mishra Dec 23 '15 at 09:46
  • Try to remove that layout inflating part and turn it into a field instead and see if it loads faster. – Kyle Emmanuel Dec 23 '15 at 09:48
  • Your way can work for a couple of items, but if it starts to grow, you're going to have to change it. try and remove this line: `dnsListAdapter.notifyDataSetChanged();` and see if it helps. – Kyle Emmanuel Dec 23 '15 at 09:56
  • removing it makes it faster but then I am not able to show the updated result in my listview – Vivek Mishra Dec 23 '15 at 10:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98798/discussion-between-kyle-emmanuel-and-vivek-mishra). – Kyle Emmanuel Dec 23 '15 at 10:11
0

If you want speed you should pre-load all the things.

@Override
public void onCreate(Bundle s) {
    dnsName=dataBaseHelper.getDnsName();
    ipAddress=dataBaseHelper.getIpAddress();
    id=dataBaseHelper.getId();
    dnsListAdapter = new DnsListAdapter(getActivity(), dnsName, ipAddress,id);
    dnsListAdapter.notifyDataSetChanged();
    android.support.v7.app.AlertDialog.Builder builder;
    final android.support.v7.app.AlertDialog alertDialog;
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View layout = inflater.inflate(R.layout.browse_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_dialog));
    listView = (ListView) layout.findViewById(R.id.dns_list);

    builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
    builder.setView(layout);
    alertDialog = builder.create();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String ip = ipAddress.get(position);
            EDITDNS1.setText(ip);
            alertDialog.dismiss();
        }

    });
}

public void LoadPrimaryDnsList(){
    alertDialog.show();
    listView.setAdapter(dnsListAdapter);
}

Apart from that if you want to understand the speed of your code, you should start to add some timing logs http://developer.android.com/reference/android/util/TimingLogger.html

Also to make your code more readable (and easier to extend / enhance) I would start learning about the Single Responsibility Principle and applying it to your methods.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • I have written that user can enter rows in database from another location and here on button click I have to show updated data so I think I have to query database on button click – Vivek Mishra Dec 23 '15 at 08:56
  • we can't actually tell what is going on from your code. What you are explaining does not match the code. / Also I think you're using this code in a Fragment? But you inflate a layout and find a view on the activity, I think you need to go back to reading some of the basics of Fragments and learning the Android lifecycle – Blundell Dec 23 '15 at 08:57
  • @VivekMishra You should not have to query the database onClick - read the database when the activity first loads and then hold an in memory store, for example a `Map` and read from that onClick – Blundell Dec 23 '15 at 08:58
  • what I want to say is that there is an add button in my app through which user can add their own dns names and both preadded and user added records are added to same table therefore I again fetch data on click in case user has added some records of his own – Vivek Mishra Dec 23 '15 at 09:21