Search code examples
androidlistviewandroid-asynctaskcustom-adapter

What am I missing to enable transfer of details from Custom Adapter to ListView?


I'm transferring as much info for my ListView from doInBackground in NewContact.java to my custom adapter (SelectPhoneContactAdapter) as possible because I've read it's better to keep as much of the logical in there as possible.

I did have most of it in my doInBackground and it was working ok but now with the code in my Custom Adapter my ListView is coming up empty. Can you tell me what I need to put in my doInBackground to make it work?

My app runs with no errors, and when debugging with System.out.print I can see my variables in my Custom Adapter have valuables so it shouldn't be empty in the ListView.

Here's my NewContact.java, that contains the ListView:

// Load data in background
    class LoadContact extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

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


            //selectPhoneContacts is an empty array list that will hold our SelectPhoneContact info
            selectPhoneContacts = new ArrayList<SelectPhoneContact>();

            listView = (ListView) findViewById(R.id.listviewPhoneContacts);

            return null;

        }


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            adapter = new SelectPhoneContactAdapter(selectPhoneContacts, NewContact.this);

            adapter.notifyDataSetChanged();

            listView.setAdapter(adapter);

        }
    }

    @Override
    protected void onResume() {

        super.onResume();

        LoadContact loadContact = new LoadContact();
        loadContact.execute();
    }

And here's my Custom Adapter, SelectPhoneContactAdapter.java:

public class SelectPhoneContactAdapter extends BaseAdapter {

    //define a list made out of SelectPhoneContacts and call it theContactsList
    public List<SelectPhoneContact> theContactsList;
    //define an array list made out of SelectContacts and call it arraylist
    private ArrayList<SelectPhoneContact> arraylist;
    Context _c;

    ArrayList<String> allPhonesofContacts;
    String phoneNumberofContact;
    String[] phoneNumberofContactStringArray;

    public SelectPhoneContactAdapter(final List<SelectPhoneContact> selectPhoneContacts, Context context) {
        theContactsList = selectPhoneContacts;
        _c = context;
        this.arraylist = new ArrayList<SelectPhoneContact>();
        this.arraylist.addAll(theContactsList);

        //we are fetching the array list allPhonesofContacts, created in VerifyUserPhoneNumber.
        //with this we will put all phone numbers of contacts on user's phone into our ListView in NewContact activity
        SharedPreferences sharedPreferencesallPhonesofContacts = PreferenceManager.getDefaultSharedPreferences(_c);
        Gson gson = new Gson();
        String json = sharedPreferencesallPhonesofContacts.getString("allPhonesofContacts", "");
        Type type = new TypeToken<ArrayList<String>>() {
        }.getType();
        allPhonesofContacts = gson.fromJson(json, type);
        System.out.println("SelectPhoneContactAdapter allPhonesofContacts :" + allPhonesofContacts);

        phoneNumberofContactStringArray = new String[allPhonesofContacts.size()];

        //phoneNumberofContactStringArray will contain all the values in allPhonesofContacts
        phoneNumberofContactStringArray = allPhonesofContacts.toArray(phoneNumberofContactStringArray);

        //for every string value in the phoneNumberofContactStringArray call it phoneNumberofContact
        for (int i = 0; i < phoneNumberofContactStringArray.length; i++) {

            phoneNumberofContact = phoneNumberofContactStringArray[i];

            {
                System.out.println("SelectPhoneContactAdapter: the phone numbers are : " + phoneNumberofContactStringArray[i]);
            }


            SelectPhoneContact selectContact = new SelectPhoneContact();

                selectPhoneContacts.add(selectContact);

                selectContact.setPhone(phoneNumberofContact);
        }
    }


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

    @Override
    public Object getItem(int i) {
        return theContactsList.get(i);
    }

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

    static class ViewHolder {
        //        In each cell in the listview show the items you want to have
//        Having a ViewHolder caches our ids, instead of having to call and load each one again and again
        TextView phone;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            //if there is nothing there (if it's null) inflate the view with the layout
            LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.phone_inflate_listview, null);

            viewHolder = new ViewHolder();

            viewHolder.phone = (TextView) convertView.findViewById(R.id.no);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }
//        store the holder with the view
        final SelectPhoneContact data = (SelectPhoneContact) theContactsList.get(i);
        viewHolder.phone.setText(data.getPhone());

        return convertView;

    }
    }

Solution

  • You are not adding any data in your array list check it in your doInBackground method.

    You have to add some data in your array list before setting adapter to your list view.

    sample code

    add some data in your selectPhoneContacts like below:

    @Override
    protected Void doInBackground(Void... voids) {
        //selectPhoneContacts is an empty array list that will hold our SelectPhoneContact info
        selectPhoneContacts = new ArrayList<SelectPhoneContact>();
    
        // add some data in to your list view here  
        SelectPhoneContact model= new SelectPhoneContact();                  
        model.setPhone("123456789");
        selectPhoneContacts.add(model);
        listView = (ListView) findViewById(R.id.listviewPhoneContacts);
    
        return null;
    }