2017-09-22 21 views
0

我尽可能多地将我的ListViewdoInBackgroundNewContact.java转移到我的自定义适配器(SelectPhoneContactAdapter),因为我已经阅读过,尽可能保持尽可能多的逻辑。我缺少什么来启用从自定义适配器到ListView的详细信息传输?

我确实大部分在我的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; 

    } 
    } 
+1

您不添加在您的数组列表中的任何数据检查它在你的doInBackground方法 –

回答

2

您不添加在您的数组列表中的任何数据检查它在你的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; 
} 
+0

我也建议你交换这两行 'adapter.notifyDataSetChanged(); listView.setAdapter(adapter);' 使它们像这样 'listView.setAdapter(adapter); adapter.notifyDataSetChanged();' –

+0

@Nilesh Rathod我在我的Custome适配器中有电话号码。所以看起来我必须将电话号码的字符串数组传递给我的NewContact活动,将该电话号码分别作为该活动中的字符串提取,并将它们分别称为phoneNumberofContact和model.setPhone(phoneNumberofContact)? – CHarris

+0

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

相关问题