0

我在android中做了一个简单ListDEmo的......有两个activity.in第一个activity 3个edittexts和2个按钮插入和取消......在第二个活动中,有一个ListView和一个按钮“返回”,
现在我想要是"Insert"按下按钮时..三个的值EditTexts应该显示在ListView其他活动上..我的代码如下:

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

}

请帮帮我..!谢谢你们

4

2 回答 2

0

首先你需要初始化b1& b2,然后在insert按钮的点击监听器中,从编辑文本中读取值,并使用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);
    }
});
}

在第二个活动中,从意图中读取值,并将它们传递给列表适配器。

顺便说一下back第二个活动中的按钮动作,可以简单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();
        }
    });
}
于 2013-05-10T07:59:08.447 回答
0

这真的是给你的。拿着

http://android-mantra.blogspot.in/2013/09/how-to-add-list-items-dynamically-using.html

Intent i = new Intent(MainActivity.this,SecondActivity.class);
            i.putExtra("edit1", s1);
            i.putExtra("edit2", s2);
            i.putExtra("edit3", s3);
            startActivity(i);

这在第一个活动中是正确的。

接下来在您的第二个活动中尝试此操作,然后添加到列表中。

Bundle b = getIntent().getExtras();
list[0] = b.getString("edit1");
list[1] = b.getString("edit2");
list[2] = b.getString("edit3");

我收到您的错误,请在第二个活动中将 ID 提供给按钮。IE

bt=(Button) findViewById(R.Id.button1);

现在运行项目

于 2013-09-15T15:02:13.790 回答