我缺少什么来启用从自定义适配器到 ListView 的详细信息传输?

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

我正在将我的 ListViewdoInBackgroundNewContact.java 的尽可能多的信息传输到我的自定义适配器 (SelectPhoneContactAdapter),因为我已经阅读了它最好尽可能多地保留逻辑。

我的 doInBackground 中确实有大部分内容,并且工作正常,但现在我的自定义适配器中的代码让我的 ListView 变空了。你能告诉我我需要在我的 doInBackground 中放入什么才能让它工作吗?

我的应用程序运行没有错误,并且在使用 System.out.print 进行调试时,我可以看到我的自定义适配器中的变量有价值,因此它在 ListView.[=24 中不应为空=]

这是我的 NewContact.java,其中包含 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();
    }

这是我的自定义适配器,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;

    }
    }

您没有在数组列表中添加任何数据,请在您的 doInBackground 方法中检查它。

在将适配器设置为列表视图之前,您必须在数组列表中添加一些数据。

示例代码

在您的 selectPhoneContacts 中添加一些数据,如下所示:

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