enter image description here Person是结构数据模型getter和setter 我想制作这个dyanamic,即想从api中提取数据。 实际上我正在使用这个多自动完成选择像gmail所以当我传递列表时它会抛出错误,代码是为这个数组设置的,我试图paas list和这个数组但仍然是空指针错误
Person[] people;
people = new Person[]{
new Person("Marshall Weir", "marshall@example.com"),
new Person("Margaret Smith", "margaret@example.com"),
new Person("Max Jordan", "max@example.com"),
new Person("Meg Peterson", "meg@example.com"),
new Person("Amanda Johnson", "amanda@example.com"),
new Person("Terry Anderson", "terry@example.com"),
new Person("Siniša Damianos Pilirani Karoline Slootmaekers",
"siniša_damianos_pilirani_karoline_slootmaekers@example.com")
};
完整代码
public class TokenActivity extends Activity implements TokenCompleteTextView.TokenListener<Person> {
ContactsCompletionView completionView;
Userfunctions userfunctions;
// Person[] people;
List<Person> people = new ArrayList<>();
ArrayAdapter<Person> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userfunctions = new Userfunctions(this);
// getdata();
// people = new Person[]{
// new Person("Marshall Weir", "marshall@example.com"),
// new Person("Margaret Smith", "margaret@example.com"),
// new Person("Max Jordan", "max@example.com"),
// new Person("Meg Peterson", "meg@example.com"),
// new Person("Amanda Johnson", "amanda@example.com"),
// new Person("Terry Anderson", "terry@example.com")
// };
userfunctions.getCustomerTags(this, "http://acme-crm.cruxservers.in/api/v1/getTags", new Userfunctions.VolleyJSONArrayCallback() {
@Override
public void onSuccess(JSONArray result) {
Person ti;
for (int i = 0; i < result.length(); i++) {
try {
ti = new Person(result.getJSONObject(i).getString("tags"));
people.add(ti);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
adapter = new FilteredArrayAdapter<Person>(this, R.layout.person_layout, people) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = l.inflate(R.layout.person_layout, parent, false);
}
Person p = getItem(position);
((TextView) convertView.findViewById(R.id.name)).setText(p.getTags());
// ((TextView)convertView.findViewById(R.id.email)).setText(p.getEmail());
return convertView;
}
@Override
protected boolean keepObject(Person person, String mask) {
mask = mask.toLowerCase();
return person.getTags().toLowerCase().startsWith(mask);
}
};
completionView = (ContactsCompletionView) findViewById(R.id.searchView);
completionView.setAdapter(adapter);
completionView.setTokenListener(this);
completionView.setTokenClickStyle(TokenCompleteTextView.TokenClickStyle.Select);
Button removeButton = (Button) findViewById(R.id.removeButton);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Person> people = completionView.getObjects();
if (people.size() > 0) {
completionView.removeObject(people.get(people.size() - 1));
}
}
});
}
private void updateTokenConfirmation() {
StringBuilder sb = new StringBuilder("Current tokens:\n");
for (Object token : completionView.getObjects()) {
sb.append(token.toString());
sb.append("\n");
}
((TextView) findViewById(R.id.tokens)).setText(sb);
}
@Override
public void onTokenAdded(Person token) {
((TextView) findViewById(R.id.lastEvent)).setText("Added: " + token);
updateTokenConfirmation();
}
@Override
public void onTokenRemoved(Person token) {
((TextView) findViewById(R.id.lastEvent)).setText("Removed: " + token);
updateTokenConfirmation();
}
}
答案 0 :(得分:1)
使用列表&lt;&gt;或ArrayList&lt;&gt;来自馆藏馆
List<Person> personList = new ArrayList<>();
添加
personList.add(new Person("Marshall Weir", "marshall@example.com"));
删除
personList.remove(0);
要检索
Person person = personList.get(0);