简单列表演示 [英] Simple List Demo
问题描述
我做了一个简单的 ListDEmo
在android..with 2 activity.in 1 活动3 edittexts和2个按钮插入,并取消有...在 2日活动,有一个的ListView
和一个按钮后退是存在的,结果
现在我想的是,当插入
按钮pressed..The值3 EditTexts
应显示在其他Activity..my code的ListView控件
是如下:
I have made a simple ListDEmo
in android..with two activity.in 1st activity 3 edittexts and 2 buttons insert and ,cancel are there...and in 2nd activity ,there is a ListView
and a button "back" is there,
Now i want is that when "Insert"
button pressed..The values of three EditTexts
should display on ListView
of other Activity..my code is as below:
MainActivity.java
package com.example.listdemo;
import java.util.ArrayList;
import android.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button b1,b2;
EditText e1,e2,e3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e1=(EditText)findViewById(R.id.editText1);
final EditText e2=(EditText)findViewById(R.id.editText2);
final EditText e3=(EditText)findViewById(R.id.editText3);
// final ArrayList<String> noteList = new ArrayList<String>();
//final ArrayAdapter<String> aa;
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String s1 = e1.getText().toString();
String s2 = e1.getText().toString();
String s3 = e1.getText().toString();
//Intent i =new Intent(MainActivity.this,SecondActivity.class);
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("edit1", s1);
i.putExtra("edit2", s2);
i.putExtra("edit3", s3);
startActivity(i);
startActivity(i);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
});
}
}
SecondActivity.java
package com.example.listdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class SecondActivity extends ListActivity {
ListView lv;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv=(ListView)findViewById(R.id.listView1);
String[] list = new String[3];
list[0] = getIntent().getStringExtra("edit1");
list[1] = getIntent().getStringExtra("edit2");
list[2] = getIntent().getStringExtra("edit3");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,list));
// Toast.makeText(getApplicationContext(), st1, Toast.LENGTH_LONG).show();
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(SecondActivity.this,MainActivity.class);
startActivity(i);
}
});
}
}
请帮我frens ..!谢谢大家。
please help me frens..! thank you all
推荐答案
首先,你需要初始化 B1
&安培; B2
,然后在点击监听器插入
按钮,从编辑文本读取的值,并使用通过他们意图
..
First you need to initialize b1
& b2
, then in the click listener of insert
button, read the values from the edit texts, and pass them using the intent
..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s1 =e1.getText().toString();
String s2 =e2.getText().toString();
String s3 =e3.getText().toString();
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("edit1", s1);
i.putExtra("edit2", s2);
i.putExtra("edit3", s3);
startActivity(i);
}
});
}
在第二个活动从意向读出的值,并把它们传递到列表中的适配器。
in the second activity read the values from the intent, and pass them to the list adapter..
的方式在第二活动回
按钮的动作,可以简单完成()
by the way the back
button action in the second activity, can be simply finish()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv=(ListView)findViewById(R.id.listView1);
String[] list = new String[3];
list[0] = getIntent().getStringExtra("edit1");
list[1] = getIntent().getStringExtra("edit2");
list[2] = getIntent().getStringExtra("edit3");
lv.setListAdapter(new ArrayAdapter<String>(this, R.layout.simplerow,list));
bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
}
这篇关于简单列表演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!