Atingas
Atingas

Reputation: 1

How to add new item on button press in Base Adapter

I'm trying to crate something that creates a new ListView item on button press, I thing I have somewhere some bug, but as I'm new to this topic I don't know where.

I've tried rewriting the code several times, I've tried to use notifyDataSetChanged(); - it does nothing tried googling looking on other topics here... Here is my 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();
}

}

Here is my CustomeAdapter.java class: public class CustomeAdapter extends 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;

    }

}

I expect to create a new list item (EditText + TextView) but nothing happens (except the Toast message) (After some tweaks the app crashes due to arrayindexoutofboundsexception length=3 index=3 error, but not in this setting) here are up to all files nessessary: https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965

Upvotes: 0

Views: 40

Answers (1)

Gralls
Gralls

Reputation: 156

Main linear layout in lv_item.xml has match_parent height. Try change it to wrap_content. It seems like one row is just taking whole screen.

Upvotes: 1

Related Questions