如何在android中实现阵列动态

如何在android中实现阵列动态,android,arrays,Android,Arrays,Person是结构数据模型的获取者和设置者 我想让这个动态的,也就是说,要把数据从api。 事实上,我使用它来实现多重自动完成选择,比如gmail,所以当我传递列表时,它会抛出一个错误,代码是为这个数组设置的,我尝试paas list和这个数组,但仍然是空指针错误 Person[] people; people = new Person[]{ new Person("Marshall Weir", "marshall@example.com"), new Person("Margaret Smi

Person是结构数据模型的获取者和设置者 我想让这个动态的,也就是说,要把数据从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();
}



  }
公共类TokenActivity扩展活动实现TokenCompleteTextView.TokenListener{
联系人完成视图完成视图;
用户函数用户函数;
//人[]人;
List people=new ArrayList();
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userfunctions=新的userfunctions(此);
//getdata();
//人=新人[]{
//新人(“马歇尔·威尔”marshall@example.com"),
//新人(“玛格丽特·史密斯”margaret@example.com"),
//新人(“马克斯·乔丹”max@example.com"),
//新人(“梅格·彼得森”meg@example.com"),
//新人(“阿曼达·约翰逊”amanda@example.com"),
//新人(“特里·安德森”terry@example.com")
//        };
userfunctions.getCustomerTags(此http://acme-crm.cruxservers.in/api/v1/getTags,新的Userfunctions.VolleyJSONArrayCallback(){
@凌驾
成功时公共无效(JSONArray结果){
人ti;
for(int i=0;i0){
completionView.removeObject(people.get(people.size()-1));
}
}
});
}
私有void updateTokenConfirmation(){
StringBuilder sb=新的StringBuilder(“当前标记:\n”);
对于(对象标记:completionView.getObjects()){
sb.append(token.toString());
某人附加(“\n”);
}
((TextView)findViewById(R.id.tokens)).setText(sb);
}
@凌驾
已添加公共令牌(个人令牌){
((TextView)findViewById(R.id.lastEvent)).setText(“添加:“+令牌”);
updateTokenConfirmation();
}
@凌驾
已删除公共令牌(个人令牌){
((TextView)findViewById(R.id.lastEvent)).setText(“删除:+令牌”);
updateTokenConfirmation();
}
}
使用集合库中的列表或ArrayList

List personList=new ArrayList()

personList.add(新人物(“Marshall Weir”),“marshall@example.com"));

除去
personList.remove(0)

取回
Person-Person=personList.get(0)

首先,你必须自己尝试一下,这非常简单,因为你的慷慨回复帮助很大……实际上,我正在使用此选项进行多重自动完成选择,如gmail,因此当我传递列表时,它抛出错误,即代码是为此数组设置的,我尝试paas list和此数组,但仍然存在空指针错误此问题无效如果你听说过ArrayLista,实际上我是用它来做multiautocompleteselect,比如gmail,所以当我传递列表时,它会抛出错误,代码是为这个数组设置的,我尝试paas列表和这个数组,但仍然是空指针错误,你可以用它来做multiautocompleteselect,给它一个tryusing arraylist列表填充列表我已经做了,但是当我选择自动完成文本时,它崩溃了,表示空指针期望值,当我使用上面提到的数组时,它是静态的,但我想让它从apican变为动态的,你共享你的logcat@SaravInfern,我也添加了代码上面是logcat链接