androidlistviewlistview-adapter

How to add EditText value to ListView using BaseAdapter by clicking Button


I have created three TextView in my custom ListView, and I want to add value in Arraylist so I made code and i can add item like where line 21. but I want add using Edittext and button. so I made function 'additem' in ListViewAdapter. if click 'btndone' button in memberActivity, 'additem' will run. But nothing happens and nothing change.....please teach how to fix..

*memberActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member);

    final ListView listview;
    final ListViewAdapter adapter;
    final ArrayList<String> items = new ArrayList<String>();

    mylist = (ListView) findViewById(R.id.listview1);
    final LinearLayout linewarLayout1 = (LinearLayout) findViewById(R.id.addmember);
    final LinearLayout linewarLayout2 = (LinearLayout) findViewById(R.id.buttongroup);

    // Create Adapter
    adapter = new ListViewAdapter();

    // Refer to list view and adapter
    listview = (ListView) findViewById(R.id.listview1);
    listview.setAdapter(adapter);

    // add 1st item.
    adapter.addItem("오정엽",
            "201102087", "컴퓨터공학");
    // add 2nd item.
    adapter.addItem("임경원",
            "201203074", "소프트웨어공학");
    // add 3rd item.
    adapter.addItem("심준식",
            "201405938", "소프트웨어공학");


    Button addButton = (Button) findViewById(R.id.add);
    addButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            mylist.setVisibility(View.INVISIBLE);
            linewarLayout2.setVisibility(View.INVISIBLE);
            linewarLayout1.setVisibility(View.VISIBLE);
        }
    });

    final EditText name = ((EditText) findViewById(R.id.etName));
    final EditText ID = ((EditText) findViewById(R.id.etID));
    final EditText Major = ((EditText) findViewById(R.id.etMajor));

    Button btnDone = (Button) findViewById(R.id.btnDone);
    btnDone.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

            TextView cnttxt = (TextView)findViewById(R.id.count);
            cnttxt.setText(adapter.getCount());

            adapter.addItem(name.getText().toString(), ID.getText().toString(), Major.getText().toString());
            name.setText("");
            ID.setText("");
            Major.setText("");
            mylist.setVisibility(View.VISIBLE);
            linewarLayout2.setVisibility(View.VISIBLE);
            linewarLayout1.setVisibility(View.INVISIBLE);
        }
    });

*ListViewItem.java

public void setname(String name) {
    nameStr = name ;
}
public void setID(String ID) {
    IDStr = ID ;
}
public void setmajor(String major) {
    majorStr = major ;
}

public String getname() {
    return this.nameStr ;
}
public String getID() {
    return this.IDStr ;
}
public String getmajor() {
    return this.majorStr ;
}

*ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {
private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>() ;

public ListViewAdapter() {

}
    TextView nameTextView = (TextView) convertView.findViewById(R.id.list_name) ;
    TextView IDTextView = (TextView) convertView.findViewById(R.id.list_ID) ;
    TextView majorTextView = (TextView) convertView.findViewById(R.id.list_major) ;

    ListViewItem listViewItem = listViewItemList.get(position);

    nameTextView.setText(listViewItem.getname());
    IDTextView.setText(listViewItem.getID());
    majorTextView.setText(listViewItem.getmajor());

    return convertView;
}

public void addItem(String name, String ID, String major) {
    ListViewItem item = new ListViewItem();

    item.setname(name);
    item.setID(ID);
    item.setmajor(major);

    listViewItemList.add(item);
}

}


Solution

  • call 'notifyDatasetChanged()' after adding data. It will Work.