【android问题】两个activity,一个里面是listview和一个button,一个自定义适配器baseadapter的问题。
我想主activity按下button启动另一个activity里输入内容,按下确认后,主activity多了一行数据。可是我的在另一个activity里面输入内容按确认...
我想主activity按下button启动另一个activity里输入内容,按下确认后,主activity多了一行数据。可是我的在另一个activity里面输入内容按确认就结束了。代码public class MainActivity extends Activity {
private ListView listView;
ArrayList<String> list = new ArrayList<String>();
private Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
bt = (Button) findViewById(R.id.button1);
listView.setAdapter(adapter);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent();
myintent.setClass(MainActivity.this, Activity2.class);
MainActivity.this.startActivityForResult(myintent,10);
MainActivity.this.finish();
}});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
list.addAll(data.getStringArrayListExtra("ArrayList"));
adapter.notifyDataSetChanged();
}
private BaseAdapter adapter=new BaseAdapter(){
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
//adapter绑定数据
public View getView(int arg0, View arg1, ViewGroup arg2) {
TextView tv=new TextView(MainActivity.this);
tv.setText("第"+arg0+"项 : "+list.get(arg0));
return tv;
}
};
}
另一个activity:
public class Activity2 extends Activity {
Button bt1;
EditText name,qq;
ArrayList<String> list=new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt1=(Button)findViewById(R.id.button1);
name=(EditText)findViewById(R.id.editText1);
qq=(EditText)findViewById(R.id.editText2);
qq.setInputType(EditorInfo.TYPE_CLASS_PHONE);
bt1.setOnClickListener(new MyClickListener());
}
class MyClickListener implements OnClickListener
{
public void onClick(View e)
{
list.add(name.getText().toString()+qq.getText().toString());
Intent intent=new Intent();
intent.putStringArrayListExtra("ArrayList",list);
setResult(0, intent);
finish();
}
}
}
布局是: <Button>
<TextView>
另一个布局: <EditText>输入姓名
<EditText>输入QQ号
<Button> 确认后返回主事件 请问为啥我没有返回就结束了??? 展开
private ListView listView;
ArrayList<String> list = new ArrayList<String>();
private Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
bt = (Button) findViewById(R.id.button1);
listView.setAdapter(adapter);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent();
myintent.setClass(MainActivity.this, Activity2.class);
MainActivity.this.startActivityForResult(myintent,10);
MainActivity.this.finish();
}});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
list.addAll(data.getStringArrayListExtra("ArrayList"));
adapter.notifyDataSetChanged();
}
private BaseAdapter adapter=new BaseAdapter(){
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
//adapter绑定数据
public View getView(int arg0, View arg1, ViewGroup arg2) {
TextView tv=new TextView(MainActivity.this);
tv.setText("第"+arg0+"项 : "+list.get(arg0));
return tv;
}
};
}
另一个activity:
public class Activity2 extends Activity {
Button bt1;
EditText name,qq;
ArrayList<String> list=new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt1=(Button)findViewById(R.id.button1);
name=(EditText)findViewById(R.id.editText1);
qq=(EditText)findViewById(R.id.editText2);
qq.setInputType(EditorInfo.TYPE_CLASS_PHONE);
bt1.setOnClickListener(new MyClickListener());
}
class MyClickListener implements OnClickListener
{
public void onClick(View e)
{
list.add(name.getText().toString()+qq.getText().toString());
Intent intent=new Intent();
intent.putStringArrayListExtra("ArrayList",list);
setResult(0, intent);
finish();
}
}
}
布局是: <Button>
<TextView>
另一个布局: <EditText>输入姓名
<EditText>输入QQ号
<Button> 确认后返回主事件 请问为啥我没有返回就结束了??? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询