我發展的Couchbase精簡版database.My問題的應用程序是,當我通過ArrayAdapter的數據看像下面形象。ArrayAdapter代碼查詢從couchbase精簡版數據庫中的數據和設置數據到列表視圖中如何使用UsersAdapter將數據設置爲列表視圖?
CouchDb couchDb = new CouchDb(getApplicationContext());
ArrayList<ContactData> data = couchDb.getData();
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_simple,R.id.textname,data);
list.setAdapter(adapter);
克服這曾經創建的自定義類UsersAdapter
public class UsersAdapter extends ArrayAdapter<ContactData>{
public UsersAdapter(MainActivity mainActivity, ArrayList<ContactData> data) {
super(mainActivity,R.layout.list_simple,data);
}
public class ViewHolder {
TextView name;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactData contactData = getItem(position);
ViewHolder viewHolder;
if(convertView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_simple, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.textname);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(contactData.getNames());
return convertView;
}
}
在上述適配器零指示字例外發生在這條線viewHolder.name.setText(contactData.getNames());
這是聯絡資料java類
public class ContactData {
String names;
public ContactData(String names) {
this.names = names;
}
public ContactData()
{
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
}
請幫助我如何解決這個問題,也說不清爲什麼該值在UsersAdapter返回null的原因。
發生的異常是
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.seyali.couchbasedatabase2, PID: 5641 java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.seyali.couchbasedatabase2.MainActivity$UsersAdapter$ViewHolder.name' on a null object reference
at com.seyali.couchbasedatabase2.MainActivity$UsersAdapter.getView(MainActivity.java:107)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
是否'list_simple'佈局包含TextView的id爲'textname'? – Piyush
是的,它包含了ID –
,這是什麼'的ArrayList數據大小的TextView = couchDb.getData();?'是否填充? –
Piyush