android 不同Activity之间数据传递

1. 传值Activity

package mydemo.mycom.demo2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import mydemo.mycom.demo2.dao.UserDao;
import mydemo.mycom.demo2.entity.UserInfo;


public class Home extends ActionBarActivity implements View.OnClickListener {

    private ListView lv;
    private Button btn_home_to_login;
    private List<UserInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        btn_home_to_login = (Button)findViewById(R.id.btn_home_to_login);
        lv = (ListView)findViewById(R.id.lv);
        btn_home_to_login.setOnClickListener(this);

        UserDao dao = new UserDao(this);
        list = dao.findAll();

        lv.setAdapter(new MyAdapter());
        lv.setOnItemClickListener(new MyItemClickListener());

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setClass(Home.this,Register.class);
        startActivity(intent);

    }


    public class MyAdapter extends BaseAdapter
    {
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            UserInfo user = list.get(i);

            View itemview = View.inflate(Home.this,R.layout.activity_item,null);

            TextView tv_id = (TextView)itemview.findViewById(R.id.tv_id);
            TextView tv_username = (TextView)itemview.findViewById(R.id.tv_username);
            TextView tv_password = (TextView)itemview.findViewById(R.id.tv_password);

            tv_id.setText(user.getId()+"");
            tv_username.setText(user.getUsername());
            tv_password.setText(user.getPassword());
            return itemview;
        }
    }


    public class MyItemClickListener implements AdapterView.OnItemClickListener
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            ListView lView = (ListView)lv;
            TextView tv_id = (TextView)view.findViewById(R.id.tv_id);
            TextView tv_username = (TextView)view.findViewById(R.id.tv_username);
            TextView tv_password = (TextView)view.findViewById(R.id.tv_password);
            String id = tv_id.getText().toString();
            String username = tv_username.getText().toString();
            String password = tv_password.getText().toString();
            Toast.makeText(getApplicationContext(), id+","+username, Toast.LENGTH_SHORT).show();
            //不同的activity之间的传值
            Intent intent = new Intent();
            intent.putExtra("username",username);
            intent.putExtra("password",password);
            intent.putExtra("id",id);
            intent.setClass(Home.this,Edit.class);
            startActivity(intent);
        }
    }
}

 

2.接受Activity

package mydemo.mycom.demo2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import mydemo.mycom.demo2.dao.UserDao;


public class Edit extends ActionBarActivity implements View.OnClickListener {

    private EditText edit_et_username;
    private EditText edit_et_password;
    private TextView edit_tv_id;
    private Button btn_edit;
    private Button btn_home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        edit_et_username = (EditText)findViewById(R.id.edit_et_username);
        edit_et_password = (EditText)findViewById(R.id.edit_et_password);
        edit_tv_id = (TextView)findViewById(R.id.edit_tv_id);
        btn_edit = (Button)findViewById(R.id.btn_edit);
        btn_home = (Button)findViewById(R.id.btn_home);

        btn_edit.setOnClickListener(this);
        btn_home.setOnClickListener(this);
        //接受activity传过来的值
        Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        String password = intent.getStringExtra("password");
        String id = intent.getStringExtra("id");

        edit_et_username.setText(username);
        edit_et_password.setText(password);
        edit_tv_id.setText(id);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_edit, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btn_edit:

                String id = edit_tv_id.getText().toString();
                String password = edit_et_password.getText().toString().trim();
                String username = edit_et_username.getText().toString().trim();

                if(TextUtils.isEmpty(password) || TextUtils.isEmpty(username))
                {
                    Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                UserDao dao = new UserDao(this);
                boolean result = dao.findByUsername(username);
                if(result)
                {
                    Toast.makeText(this,"用户已被注册",Toast.LENGTH_SHORT).show();
                    return;
                }
                dao.modify(id,username,password);
                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(this,Home.class);
                startActivity(intent);
                break;
            case R.id.btn_home:

                Intent tointent = new Intent();
                tointent.setClass(this,Home.class);
                startActivity(tointent);
                break;

        }
    }
}

 

posted @ 2015-05-19 11:35  大空白纸  阅读(323)  评论(0编辑  收藏  举报