启用详细信息从自定义适配器传输到列表视图缺少什么?



我正在将尽可能多的ListView信息从NewContact.javadoInBackground传输到我的自定义适配器(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方法中检查它。

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

示例代码

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

最新更新