如何通过单击按钮使用BaseAdapter将EditText值添加到ListView

时间:2016-11-21 19:10:38

标签: android listview listview-adapter

我在我的自定义ListView中创建了三个TextView,我想在Arraylist中添加值,所以我创建了代码,我可以添加第21行的项目。但我想使用Edittext和按钮添加。所以我在ListViewAdapter中创建了函数'additem'。 如果点击memberActivity中的'btndone'按钮,'additem'将会运行。但没有任何反应,也没有任何变化......请教导如何解决..

* 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);
}

}

1 个答案:

答案 0 :(得分:0)

添加数据后调用'notifyDatasetChanged()'。它会起作用。