如何通过单击按钮使用 BaseAdapter 将 EditText 值添加到 ListView
How to add EditText value to ListView using BaseAdapter by clicking Button
我在我的自定义 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);
}
}
添加数据后调用'notifyDatasetChanged()'。它会起作用。
我在我的自定义 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);
}
}
添加数据后调用'notifyDatasetChanged()'。它会起作用。