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

Posted

技术标签:

【中文标题】我缺少啥来启用从自定义适配器到 ListView 的详细信息传输?【英文标题】:What am I missing to enable transfer of details from Custom Adapter to ListView?我缺少什么来启用从自定义适配器到 ListView 的详细信息传输? 【发布时间】:2018-03-03 23:24:11 【问题描述】:

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

我的doInBackground 中确实有大部分它,它工作正常,但现在使用我的自定义适配器中的代码,我的ListView 是空的。你能告诉我我需要在我的doInBackground 中输入什么才能让它工作吗?

我的应用程序运行时没有错误,当使用System.out.print 进行调试时,我可以看到我的自定义适配器中的变量有值,所以它在ListView 中不应为空。

这是我的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 方法中检查它 【参考方案1】:

您没有在数组列表中添加任何数据,请在您的 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;

【讨论】:

我还建议你交换这两行adapter.notifyDataSetChanged(); listView.setAdapter(adapter); 让它们像这样listView.setAdapter(adapter); adapter.notifyDataSetChanged(); @Nilesh Rathod 我的客户适配器中有电话号码。所以看起来我必须将电话号码的字符串数组传递给我的 NewContact 活动,将电话号码单独提取为该活动中的字符串,调用它们 phoneNumberofContact,然后调用 model.setPhone(phoneNumberofContact)? @CHarris 在将适配器设置为列表视图之前,您必须在数组列表中添加一些 dtaa

以上是关于我缺少啥来启用从自定义适配器到 ListView 的详细信息传输?的主要内容,如果未能解决你的问题,请参考以下文章

从自定义 ListView 中的 TextView -w/Button 获取文本

从自定义适配器获取片段中的 UI 元素 ID

列表视图中的页脚按钮,如何从自定义列表适配器中获取值

ListView 中的可选 TextView(缺少动画)

从膨胀的 XML 将适配器设置为 ListView 以打印 PDF - 缺少项目

禁用 ListView 项目单击然后启用它