如何在基本适配器中按下按钮时添加新项目

问题描述 投票:0回答:1

我正在尝试创建在按钮上创建新的ListView项的东西,我发现我有一些bug,但是由于我是这个主题的新手,所以我不知道在哪里。

我尝试重写代码多次,尝试使用notifyDataSetChanged(); -它什么也没做尝试谷歌搜索在这里寻找其他主题...这是我的MainActivity.java:

public Button btn;
private ListView lv;
private CustomeAdapter customeAdapter;
public ArrayList<EditModel> editModelArrayList;

int populateListMaxNum  =3;
int listNumber = populateListMaxNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.listView);
    btn = (Button) findViewById(R.id.btn);

    editModelArrayList = populateList();
    customeAdapter = new CustomeAdapter(this,editModelArrayList);
    lv.setAdapter(customeAdapter);

    /* TODO activate button */

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           addToList();
            Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show();

        }
    });


}

private ArrayList<EditModel> populateList(){ //this part works perfectly

    ArrayList<EditModel> list = new ArrayList<>();

    for(int i = 0; i < populateListMaxNum; i++){
        EditModel editModel = new EditModel();
        //editModel.setEditTextValue(String.valueOf(i));
        list.add(editModel);
    }

    return list;
}
/*TODO make it work = expand */

private void addToList(){  // this part doesn't work nor it doesn't fail

    EditModel editModel = new EditModel();

    editModelArrayList.add(editModel);
    customeAdapter.notifyDataSetChanged();
}

}

这是我的CustomeAdapter.java类:公共类CustomeAdapter扩展BaseAdapter {

    private Context context;
    public static ArrayList<EditModel> editModelArrayList;

    public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) {

        this.context = context;
        CustomeAdapter.editModelArrayList = editModelArrayList;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getCount() {
        return editModelArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return editModelArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lv_item, null, true);

            holder.editText = convertView.findViewById(R.id.editid);

            convertView.setTag(holder);
        }else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder)convertView.getTag();
        }

        holder.editText.setText(editModelArrayList.get(position).getEditTextValue());

        holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }


        });

        return convertView;
    }

    private class ViewHolder {

        protected EditText editText;

    }

}

我希望创建一个新的列表项(EditText + TextView),但是什么也没有发生(Toast消息除外)(经过一些调整后,应用程序由于arrayindexoutofboundsexception length = 3 index = 3错误而崩溃,但不在此设置下)这是所有必要文件的全部:https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965

java android arraylist android-listview
1个回答
0
投票

lv_item.xml中的主要线性布局具有match_parent高度。尝试将其更改为wrap_content。似乎只有一排占据了整个屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.