如何在Android中单击按钮分别显示在列表视图的EditText价值 [英] How to show value of EditText in listview each time on button click in android
问题描述
我有两个领域的EditText 即名称和标记和一个Add按钮。
我要显示的EditText每一个时间值,只要添加按钮被点击。
不过,我只能够显示只有一个列表视图单个值。
当我点击再次添加按钮,它的previous价值将被删除,更新的值将被显示在列表视图。
我想填充列表视图整个列表。
公共类MainActivity延伸活动{
的EditText名1;
的EditText标志1;
私人的ListView LV;
ArrayAdapter<串GT; AA;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
的setContentView(R.layout.activity_main); TextView的markshee =(的TextView)findViewById(R.id.textView3);
markshee.setText(); 按钮btnAdd =(按钮)findViewById(R.id.button1);
btnAdd.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(视图v){ 尝试{
名1 =(EditText上)findViewById(R.id.editText1);
字符串名称= name1.getText()的toString()。 标志1 =(EditText上)findViewById(R.id.editText2);
字符串标记= marks1.getText()的toString()。
如果(name.equals()|| marks.equals()){ 字符串str =不留任何字段为空!; 吐司面包= Toast.makeText(MainActivity.this,STR,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
其他{ TextView的marksheet =(的TextView)findViewById(R.id.textView3);
marksheet.setText(标志表);
marksheet.setTextColor(Color.BLUE); TextView的南=(的TextView)findViewById(R.id.textView4);
nam.setText(姓名);
nam.setTextColor(Color.RED); TextView的月=(的TextView)findViewById(R.id.textView5);
mar.setText(标志);
mar.setTextColor(Color.RED); name1.setText();
marks1.setText(); LV =(ListView控件)findViewById(R.id.listView1);
lv.setItemsCanFocus(真); ArrayList的<串GT;数据=新的ArrayList<串GT;();
data.add(+姓名++引号);
AA =
新ArrayAdapter<串GT;(MainActivity.this,android.R.layout.simple_list_item_1,数据); lv.setAdapter(AA); }
}赶上(异常前)
{
的System.out.println(ex.getStackTrace());
}
}
});}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
}
试试这个:
声明ArrayList是公共否则会造成每一次点击和sotre最后项仅
公共类MainActivity延伸活动{
的EditText名1;
的EditText标志1;
私人的ListView LV;
ArrayAdapter<串GT; AA;
ArrayList的<串GT;数据=新的ArrayList<串GT;();
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
的setContentView(R.layout.activity_main);TextView的markshee =(的TextView)findViewById(R.id.textView3);
markshee.setText();按钮btnAdd =(按钮)findViewById(R.id.button1);
btnAdd.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(视图v){ 尝试{
名1 =(EditText上)findViewById(R.id.editText1);
字符串名称= name1.getText()的toString()。 标志1 =(EditText上)findViewById(R.id.editText2);
字符串标记= marks1.getText()的toString()。
如果(name.equals()|| marks.equals()){ 字符串str =不留任何字段为空!; 吐司面包= Toast.makeText(MainActivity.this,STR,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
其他{ TextView的marksheet =(的TextView)findViewById(R.id.textView3);
marksheet.setText(标志表);
marksheet.setTextColor(Color.BLUE); TextView的南=(的TextView)findViewById(R.id.textView4);
nam.setText(姓名);
nam.setTextColor(Color.RED); TextView的月=(的TextView)findViewById(R.id.textView5);
mar.setText(标志);
mar.setTextColor(Color.RED); name1.setText();
marks1.setText(); LV =(ListView控件)findViewById(R.id.listView1);
lv.setItemsCanFocus(真);
data.add(+姓名++引号);
AA =
新ArrayAdapter<串GT;(MainActivity.this,android.R.layout.simple_list_item_1,数据); lv.setAdapter(AA); }
}赶上(异常前)
{
的System.out.println(ex.getStackTrace());
}
}
});}
I have two EditText fields i.e name and marks and one Add button.
I have to display EditText values each and every time whenever Add button is clicked.
However,I am only able to display only one single value on listview.
When i clicked again on Add button,its previous value get erased and newer value gets displayed in listview.
I wanna populate whole list in listview.
public class MainActivity extends Activity {
EditText name1;
EditText marks1;
private ListView lv;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView markshee = (TextView)findViewById(R.id.textView3);
markshee.setText("");
Button btnAdd = (Button) findViewById(R.id.button1);
btnAdd.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
name1 = (EditText)findViewById(R.id.editText1);
String name = name1.getText().toString();
marks1 = (EditText)findViewById(R.id.editText2);
String marks = marks1.getText().toString();
if(name.equals("") || marks.equals("")){
String str="Don't Leave any field blank !";
Toast toast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
TextView marksheet = (TextView)findViewById(R.id.textView3);
marksheet.setText("Marks Sheet");
marksheet.setTextColor(Color.BLUE);
TextView nam = (TextView)findViewById(R.id.textView4);
nam.setText("Name");
nam.setTextColor(Color.RED);
TextView mar = (TextView)findViewById(R.id.textView5);
mar.setText("Marks");
mar.setTextColor(Color.RED);
name1.setText("");
marks1.setText("");
lv = (ListView) findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
ArrayList<String> data = new ArrayList<String>();
data.add(" "+name+" "+marks);
aa =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, data);
lv.setAdapter(aa);
}
}catch(Exception ex)
{
System.out.println(ex.getStackTrace());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Try this: you declare ArrayList is public otherwise it will create each and every time clicking and sotre last items only
public class MainActivity extends Activity {
EditText name1;
EditText marks1;
private ListView lv;
ArrayAdapter<String> aa;
ArrayList<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView markshee = (TextView)findViewById(R.id.textView3);
markshee.setText("");
Button btnAdd = (Button) findViewById(R.id.button1);
btnAdd.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
name1 = (EditText)findViewById(R.id.editText1);
String name = name1.getText().toString();
marks1 = (EditText)findViewById(R.id.editText2);
String marks = marks1.getText().toString();
if(name.equals("") || marks.equals("")){
String str="Don't Leave any field blank !";
Toast toast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
TextView marksheet = (TextView)findViewById(R.id.textView3);
marksheet.setText("Marks Sheet");
marksheet.setTextColor(Color.BLUE);
TextView nam = (TextView)findViewById(R.id.textView4);
nam.setText("Name");
nam.setTextColor(Color.RED);
TextView mar = (TextView)findViewById(R.id.textView5);
mar.setText("Marks");
mar.setTextColor(Color.RED);
name1.setText("");
marks1.setText("");
lv = (ListView) findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
data.add(" "+name+" "+marks);
aa =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, data);
lv.setAdapter(aa);
}
}catch(Exception ex)
{
System.out.println(ex.getStackTrace());
}
}
});
}
这篇关于如何在Android中单击按钮分别显示在列表视图的EditText价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!