Search code examples
androiddatabaselistviewandroid-listviewbaseadapter

Fetching List from Database and showing in Listview takes very much time


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;
    }

}

Solution

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