0

I need to load a large dataset from a sqlite database. This will take too much of time to load the data. Therefore I used LoadMorelistView library to load with pagination.

I followed below link and successfully load the data from hard coded list. pull to refresh and loadmore listview like facebook

But when I tried to load from sqlite database I got the following error.

// The constructor ArrayAdapter(Activity, int, ArrayList) is Undefined

I searched the google as well as stackoverflow. But unable to find a solution.

Please find the code snippet which I used to load the data,

Code for the list loading fragment. (FragmentThree.java)

package com.load.more.list.view;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.costum.android.widget.LoadMoreListView;
import com.costum.android.widget.LoadMoreListView.OnLoadMoreListener;
import com.load.more.list.model.Customer;
import com.load.more.list.data.CustomerDS;
import com.load.more.list.data.DatabaseHelper;

import android.app.Fragment;

import com.load.more.list.control.CustomerAdapter;

public class FragmentThree extends Fragment {

    View view;

    LoadMoreListView lyt;
    ArrayAdapter<String> files;
    ArrayList<Customer> mListItems;
    CustomerDS customerDS;
    private DatabaseHelper dbHelper;

    private int visibleThreshold = 20;
    private int currentPage = 0;
    private int previousTotal = 0;
    private int firstVisibleItem = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_three, container, false);

        lyt = (LoadMoreListView) view.findViewById(R.id.lvRouteCustomers);

        mListItems = customerDS.getAllCustomersFromTo(visibleThreshold,
                firstVisibleItem);

        // Following Error Thrown from here
        // The constructor ArrayAdapter<String>(Activity, int,
        // ArrayList<Customer>) is Undefined
        files = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, mListItems);
        lyt.setAdapter(files);

        lyt.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                // TODO Auto-generated method stub
                new LoadMoreDataTask().execute();
            }
        });

        return view;
    }

    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (isCancelled()) {
                return null;
            }

            // Simulates a background task
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            // Following Error Thrown from here
            // The constructor ArrayAdapter<String>(Activity, int,
            // ArrayList<Customer>) is Undefined
            files = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, mListItems);
            lyt.setAdapter(files);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            files.notifyDataSetChanged();

            // Call onLoadMoreComplete when the LoadMore task, has finished
            lyt.onLoadMoreComplete();

            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled() {
            // Notify the loading more operation has finished
            lyt.onLoadMoreComplete();
        }
    }

}

Code for the database helper class. (DatabaseHelper)

package com.load.more.list.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    //database information
    private static final String DATABASE_NAME = "fmcgDB.db"; 
    private static final int DATABASE_VERSION = 1;


    //TABLES

    //Customer table
    public static final String TABLE_CUSTOMER = "customer";
    public static final String CUSTOMER_ID = "customer_id";
    public static final String CUSTOMER_NO = "customer_no";
    public static final String CUSTOMER_NAME = "customer_name";
    public static final String CUSTOMER_CONTACT_PERSON = "customer_contact_person";
    public static final String CUSTOMER_TELEPHONE_NO = "customer_telephone_no";
    public static final String CUSTOMER_ADDRESS = "customer_address";
    public static final String CUSTOMER_LONGITUDE = "customer_longitude";
    public static final String CUSTOMER_LATITUDE = "customer_latitude";
    public static final String CUSTOMER_TLP = "customer_tlp";
    public static final String CUSTOMER_REP_ID = "customer_rep_id";
    public static final String CUSTOMER_CATEGORY_ID = "cc_id";
    public static final String CUSTOMER_OUTLET_TYPE_ID = "ot_id";
    public static final String CUSTOMER_PERIPHERY_TYPE_ID = "pt_id";
    public static final String CUSTOMER_VOLUME_ID = "volume_id";
    public static final String CUSTOMER_MARKET_ID = "market_id";

    private static final String CREATE_CUSTOMER_TABLE = "CREATE TABLE " + TABLE_CUSTOMER + " ("
            + CUSTOMER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + CUSTOMER_NO + " TEXT, "
            + CUSTOMER_NAME + " TEXT, "
            + CUSTOMER_CONTACT_PERSON + " TEXT, "
            + CUSTOMER_TELEPHONE_NO + " TEXT, "
            + CUSTOMER_ADDRESS + " TEXT, "
            + CUSTOMER_LONGITUDE + " REAL, "
            + CUSTOMER_LATITUDE + " REAL, "
            + CUSTOMER_TLP + " INTEGER, "
            + CUSTOMER_REP_ID + " INTEGER, "
            + CUSTOMER_CATEGORY_ID + " INTEGER, "
            + CUSTOMER_OUTLET_TYPE_ID + " INTEGER, "
            + CUSTOMER_PERIPHERY_TYPE_ID + " INTEGER, "
            + CUSTOMER_VOLUME_ID + " INTEGER, "
            + CUSTOMER_MARKET_ID + " INTEGER "
            +");";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) { // this order must be followed when creating tables
        arg0.execSQL(CREATE_CUSTOMER_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        arg0.execSQL("DROP TABLE IF EXISTS " + CREATE_CUSTOMER_TABLE);
        onCreate(arg0);

    }

}

Code for the sqlite data loading. (CustomerDS.java)

package com.load.more.list.data;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.load.more.list.model.Customer;

public class CustomerDS {

    private SQLiteDatabase fmcgDB;
    private DatabaseHelper dbHelper;
    Context context;

    public CustomerDS(Context context) {
        this.context = context;
        dbHelper = new DatabaseHelper(context);
    }

    public void open() throws SQLException {
        fmcgDB = dbHelper.getWritableDatabase();
    }

    public ArrayList<Customer> getAllCustomersFromTo(int limit, int offset) {
        if (fmcgDB == null) {
            open();
        } else if (!fmcgDB.isOpen()) {
            open();
        }
        ArrayList<Customer> customersList = new ArrayList<Customer>();

        // Newly Added
        String selectQuery = "SELECT * FROM " + dbHelper.TABLE_CUSTOMER
                + " LIMIT " + limit + " OFFSET " + offset +"";

        Cursor cursor = null;
        try{
            cursor = fmcgDB.rawQuery(selectQuery, null);

        /*cursor = fmcgDB.query(dbHelper.TABLE_CUSTOMER, null, null, null,
                    null, null, null);
        */  
        while (cursor.moveToNext()) {
            Customer customer = new Customer();
            customer.setCustomer_id(cursor.getInt(cursor.getColumnIndex(dbHelper.CUSTOMER_ID)));
            customer.setCustomer_no(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_NO))));
            customer.setCustomer_name(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_NAME))));
            customer.setCustomer_contact_person(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_CONTACT_PERSON))));
            customer.setCustomer_telephone_no(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_TELEPHONE_NO))));
            customer.setCustomer_address(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_ADDRESS))));
            customer.setCustomer_longitude(cursor.getDouble((cursor.getColumnIndex(dbHelper.CUSTOMER_LONGITUDE))));
            customer.setCustomer_latitude(cursor.getDouble((cursor.getColumnIndex(dbHelper.CUSTOMER_LATITUDE))));
            int TLP = cursor.getInt((cursor.getColumnIndex(dbHelper.CUSTOMER_TLP)));
            if(TLP == 0){
                customer.setCustomer_TLP_member(true);
            }else{
                customer.setCustomer_TLP_member(false);
            }

            customersList.add(customer);
        }
        }
        finally {  
            if (cursor!=null) {
                cursor.close();
            }
            fmcgDB.close();
            return customersList;
            }
    }
}

Code for the Adapter Class. (CustomerAdapter.java)

package com.load.more.list.control;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.load.more.list.model.Customer;
import com.load.more.list.view.R;

    public class CustomerAdapter extends ArrayAdapter<Customer> {
        Context context;
        ArrayList<Customer> customerList;

        public CustomerAdapter(Context context, ArrayList<Customer> customerList){
            super(context, R.layout.item_customer, customerList);
            this.context = context;
            this.customerList = customerList;   
        }

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.item_customer, parent, false);
            TextView tvName = (TextView) row.findViewById(R.id.tvRouteCustomerListName);
            TextView tvMarket = (TextView) row.findViewById(R.id.tvRouteCustomerListMarket);
            TextView tvVolume = (TextView) row.findViewById(R.id.tvRouteCustomerListVolume);

            tvName.setText(customerList.get(position).getCustomer_name());

            return row;
        }
}

Code for the Customer Object. (Customer.java)

package com.load.more.list.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class Customer {

    private static final String TAG_CUSTOMER = "customers";
    private static final String TAG_CUSTOMER_ID = "customer_id";
    private static final String TAG_CUSTOMER_NO = "CustomerCode";
    private static final String TAG_CUSTOMER_NAME = "CustomerName";
    private static final String TAG_CUSTOMER_ADDRESS = "Address1";
    private static final String TAG_CUSTOMER_CONTACT_PERSON = "ContactPersonName";
    private static final String TAG_CUSTOMER_TELEPHONE_NO = "PhoneNo";
    private static final String TAG_CUSTOMER_LATITUDE = "CustomerLatitude";
    private static final String TAG_CUSTOMER_LONGITUDE = "CustomerLongitude";
    private static final String TAG_CUSTOMER_IS_TLP = "Istlp";
    private static final String TAG_CUSTOMER_REP_ID = "";
    private static final String TAG_CUSTOMER_PT_ID = "PheriheryId";
    private static final String TAG_CUSTOMER_CATEGORY_ID = "CatrgoryId";
    private static final String TAG_CUSTOMER_OUTLETTYPE_ID = "OutletTypeId";
    private static final String TAG_CUSTOMER_VOLUME_ID = "VolumeId";
    private static final String TAG_CUSTOMER_MARKET_ID = "MarketId";


    private int customer_id;
    private String customer_no;
    private String customer_name;
    private String customer_contact_person;
    private String customer_telephone_no;
    private String customer_address;
    private double customer_longitude;
    private double customer_latitude;
    private boolean customer_TLP_member;


    public int getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }
    public String getCustomer_name() {
        return customer_name;
    }
    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }
    public String getCustomer_contact_person() {
        return customer_contact_person;
    }
    public void setCustomer_contact_person(String customer_contact_person) {
        this.customer_contact_person = customer_contact_person;
    }
    public String getCustomer_telephone_no() {
        return customer_telephone_no;
    }
    public void setCustomer_telephone_no(String customer_telephone_no) {
        this.customer_telephone_no = customer_telephone_no;
    }
    public String getCustomer_address() {
        return customer_address;
    }
    public void setCustomer_address(String customer_address) {
        this.customer_address = customer_address;
    }
    public double getCustomer_longitude() {
        return customer_longitude;
    }
    public void setCustomer_longitude(double customer_longitude) {
        this.customer_longitude = customer_longitude;
    }
    public double getCustomer_latitude() {
        return customer_latitude;
    }
    public void setCustomer_latitude(double customer_latitude) {
        this.customer_latitude = customer_latitude;
    }

public boolean isCustomer_TLP_member() {
        return customer_TLP_member;
    }
    public void setCustomer_TLP_member(boolean customer_TLP_member) {
        this.customer_TLP_member = customer_TLP_member;
    }
    public String getCustomer_no() {
        return customer_no;
    }
    public void setCustomer_no(String customer_no) {
        this.customer_no = customer_no;
    }


}

Can anyone know how to solve this problem? If yes, please help me to solve this problem.

Thanks in advance.

Community
  • 1
  • 1
Ahamed Salik
  • 313
  • 2
  • 4
  • 18

1 Answers1

0

Finally I solved the issue,.

I have changed the type of mListItems to

ArrayList<String> mListItems;

and the return type of getAllCustomersFromTo() to String array.

It solved my issue.

Final source code of the updated classes are shown below.

Code for the list loading fragment. (FragmentThree.java)

package com.load.more.list.view;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.costum.android.widget.LoadMoreListView;
import com.costum.android.widget.LoadMoreListView.OnLoadMoreListener;
import com.load.more.list.model.Customer;
import com.load.more.list.data.CustomerDS;
import com.load.more.list.data.DatabaseHelper;

import android.app.Fragment;

import com.load.more.list.control.CustomerAdapter;

public class FragmentThree extends Fragment {

    View view;

    LoadMoreListView lyt;
    ArrayAdapter<String> files;
    ArrayList<String> mListItems;
    CustomerDS customerDS;
    private DatabaseHelper dbHelper;

    private int visibleThreshold = 20;
    private int currentPage = 0;
    private int previousTotal = 0;
    private int firstVisibleItem = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_three, container, false);

        lyt = (LoadMoreListView) view.findViewById(R.id.lvRouteCustomers);

        mListItems = customerDS.getAllCustomersFromTo(visibleThreshold,
                firstVisibleItem);

        files = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, mListItems);
        lyt.setAdapter(files);

        lyt.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                // TODO Auto-generated method stub
                new LoadMoreDataTask().execute();
            }
        });

        return view;
    }

    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (isCancelled()) {
                return null;
            }

            // Simulates a background task
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            files = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, mListItems);
            lyt.setAdapter(files);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            files.notifyDataSetChanged();

            // Call onLoadMoreComplete when the LoadMore task, has finished
            lyt.onLoadMoreComplete();

            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled() {
            // Notify the loading more operation has finished
            lyt.onLoadMoreComplete();
        }
    }

}

Code for the sqlite data loading. (CustomerDS.java)

package com.load.more.list.data;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.load.more.list.model.Customer;

public class CustomerDS {

    private SQLiteDatabase fmcgDB;
    private DatabaseHelper dbHelper;
    Context context;

    public CustomerDS(Context context) {
        this.context = context;
        dbHelper = new DatabaseHelper(context);
    }

    public void open() throws SQLException {
        fmcgDB = dbHelper.getWritableDatabase();
    }

    public ArrayList<String> getAllCustomersFromTo(int limit, int offset) {
        if (fmcgDB == null) {
            open();
        } else if (!fmcgDB.isOpen()) {
            open();
        }
        ArrayList<String> customersList = new ArrayList<String>();

        String selectQuery = "SELECT * FROM " + dbHelper.TABLE_CUSTOMER
                + " LIMIT " + limit + " OFFSET " + offset +"";

        Cursor cursor = null;
        try{
            cursor = fmcgDB.rawQuery(selectQuery, null);

        while (cursor.moveToNext()) {
            customersList.add(cursor.getString((cursor.getColumnIndex(dbHelper.CUSTOMER_NAME))));

        }
        }
        finally {  
            if (cursor!=null) {
                cursor.close();
            }
            fmcgDB.close();
            return customersList;
            }
    }
}
Ahamed Salik
  • 313
  • 2
  • 4
  • 18