如何从另一个活动获取数据到微调器?

时间:2014-10-13 06:42:10

标签: android eclipse sharedpreferences android-spinner

请帮我从ManageClass获取包含editextclassno的数据。 to ManageStudents包含spinnerclassno。先谢谢你了

ManageClass

package com.e_attendance;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ManageClass extends Activity implements OnClickListener
{

    EditText editClassno;
    Button btnAdd, btnDelete, btnModify, btnViewAll;
    SQLiteDatabase db;
    public static final String PREFS_CLASS = "MyPreferencesFile";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classdetail);

        editClassno = (EditText) findViewById(R.id.editClassno);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnModify = (Button) findViewById(R.id.btnModify);
        btnViewAll = (Button) findViewById(R.id.btnViewAll);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);

        db = openOrCreateDatabase("ClassDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS class (classno VARCHAR);");
    }

    @Override
    public void onClick(View view) {

        if (view == btnAdd) {

            if (editClassno.getText().toString().trim().length() == 0)

            {

                showMessage("Error", "Enter a Class No please");
                return;
            }

            db.execSQL("INSERT INTO class VALUES('" + editClassno.getText()
                    + "');");
            showMessage("Success", "Record added");
            clearText();
        }

        if (view == btnDelete) {
            if (editClassno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Classno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM class WHERE classno='"
                    + editClassno.getText() + "'", null);
            if (c.moveToFirst()) {
                db.execSQL("DELETE FROM class WHERE classno='"
                        + editClassno.getText() + "'");
                showMessage("Success", "Record Deleted");
            } else {
                showMessage("Error", "Invalid Classno");
            }
            clearText();
        }

        if (view == btnModify) {
            if (editClassno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Classno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM class WHERE classno='"
                    + editClassno.getText() + "'", null);

            if (c.moveToFirst()) {
                db.execSQL("UPDATE class SET class WHERE classno='"
                        + editClassno.getText() + "'");
                showMessage("Success", "Record Modified");

            } else {
                showMessage("Error", "Invalid Clasno");
            }
            clearText();
        }

        if (view == btnViewAll) {
            Cursor c = db.rawQuery("SELECT * FROM class", null);
            if (c.getCount() == 0) {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (c.moveToNext()) {
                buffer.append("Classno: " + c.getString(0) + "\n");
                ;
            }
            showMessage("Classes No.", buffer.toString());
        }

    }

    public void clearText() {
        editClassno.setText("");

        editClassno.requestFocus();
    }

    public void showMessage(String title, String message) {
        Builder builder = new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
}

ManageStudents

package com.e_attendance;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ManageStudents extends Activity implements OnClickListener {
    EditText editRollno, editName, editEmail;

    Button btnAdd, btnDelete, btnModify, btnView, btnViewAll, btnShowInfo;
    SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studentdetails);

        editRollno = (EditText) findViewById(R.id.editRollno);

        editName = (EditText) findViewById(R.id.editName);
        editEmail = (EditText) findViewById(R.id.editEmail);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnModify = (Button) findViewById(R.id.btnModify);
        btnView = (Button) findViewById(R.id.btnView);
        btnViewAll = (Button) findViewById(R.id.btnViewAll);

        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnView.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);

        db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,email VARCHAR);");
    }

    public void onClick(View view) {
        if (view == btnAdd) {
            if (editRollno.getText().toString().trim().length() == 0
                    || editName.getText().toString().trim().length() == 0
                    || editEmail.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter all values");
                return;
            }

            db.execSQL("INSERT INTO student VALUES('" + editRollno.getText()
                    + "','" + editName.getText() + "','" + editEmail.getText()
                    + "');");
            showMessage("Success", "Record added");
            clearText();
        }
        if (view == btnDelete) {
            if (editRollno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM student WHERE rollno='"
                    + editRollno.getText() + "'", null);
            if (c.moveToFirst()) {
                db.execSQL("DELETE FROM student WHERE rollno='"
                        + editRollno.getText() + "'");
                showMessage("Success", "Record Deleted");
            } else {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }

        if (view == btnModify) {
            if (editRollno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM student WHERE rollno='"
                    + editRollno.getText() + "'", null);
            if (c.moveToFirst()) {
                db.execSQL("UPDATE student SET name='" + editName.getText()
                        + "',email='" + editEmail.getText()
                        + "' WHERE rollno='" + editRollno.getText() + "'");
                showMessage("Success", "Record Modified");
            } else {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if (view == btnView) {
            if (editRollno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM student WHERE rollno='"
                    + editRollno.getText() + "'", null);
            if (c.moveToFirst()) {
                editName.setText(c.getString(1));
                editEmail.setText(c.getString(2));
            } else {
                showMessage("Error", "Invalid Rollno");
                clearText();
            }
        }
        if (view == btnViewAll) {
            Cursor c = db.rawQuery("SELECT * FROM student", null);
            if (c.getCount() == 0) {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (c.moveToNext()) {
                buffer.append("Rollno: " + c.getString(0) + "\n");
                buffer.append("Name: " + c.getString(1) + "\n");
                buffer.append("email: " + c.getString(2) + "\n\n");
            }
            showMessage("Student Details", buffer.toString());
        }

    }

    public void showMessage(String title, String message) {
        Builder builder = new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }

    public void clearText() {
        editRollno.setText("");
        editName.setText("");
        editEmail.setText("");
        editRollno.requestFocus();
    }
}

classdetail -XML

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="110dp"
        android:layout_y="10dp"
        android:text="@string/title" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="30dp"
        android:layout_y="50dp"
        android:text="@string/roll_no" />

    <Button
        android:id="@+id/btnViewAll"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="31dp"
        android:layout_y="362dp"
        android:text="@string/view_all" />

    <Button
        android:id="@+id/btnModify"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="33dp"
        android:layout_y="310dp"
        android:text="@string/modify" />

    <Button
        android:id="@+id/btnView"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="143dp"
        android:layout_y="359dp"
        android:text="@string/view" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="145dp"
        android:layout_y="310dp"
        android:text="@string/delete" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="85dp"
        android:layout_y="264dp"
        android:text="@string/add" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="31dp"
        android:layout_y="104dp"
        android:text="@string/name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="28dp"
        android:layout_y="208dp"
        android:text="Enter Classno" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="33dp"
        android:layout_y="149dp"
        android:text="@string/email" />

    <EditText
        android:id="@+id/editEmail"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="141dp"
        android:layout_y="138dp"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="140dp"
        android:layout_y="86dp"
        android:ems="10"
        android:inputType="text" />

    <EditText
        android:id="@+id/editRollno"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="142dp"
        android:layout_y="35dp"
        android:ems="10"
        android:inputType="number" />

    <Spinner
        android:id="@+id/spinnerclassno"
        android:layout_width="132dp"
        android:layout_height="38dp"
        android:layout_x="139dp"
        android:layout_y="201dp" />

</AbsoluteLayout>

studentsdetails- XML

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="110dp"
        android:layout_y="10dp"
        android:text="@string/title" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="30dp"
        android:layout_y="50dp"
        android:text="@string/roll_no" />

    <Button
        android:id="@+id/btnViewAll"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="31dp"
        android:layout_y="362dp"
        android:text="@string/view_all" />

    <Button
        android:id="@+id/btnModify"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="33dp"
        android:layout_y="310dp"
        android:text="@string/modify" />

    <Button
        android:id="@+id/btnView"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="143dp"
        android:layout_y="359dp"
        android:text="@string/view" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="145dp"
        android:layout_y="310dp"
        android:text="@string/delete" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="85dp"
        android:layout_y="264dp"
        android:text="@string/add" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="31dp"
        android:layout_y="104dp"
        android:text="@string/name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="28dp"
        android:layout_y="208dp"
        android:text="Enter Classno" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="33dp"
        android:layout_y="149dp"
        android:text="@string/email" />

    <EditText
        android:id="@+id/editEmail"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="141dp"
        android:layout_y="138dp"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="140dp"
        android:layout_y="86dp"
        android:ems="10"
        android:inputType="text" />

    <EditText
        android:id="@+id/editRollno"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="142dp"
        android:layout_y="35dp"
        android:ems="10"
        android:inputType="number" />

    <Spinner
        android:id="@+id/spinnerclassno"
        android:layout_width="132dp"
        android:layout_height="38dp"
        android:layout_x="139dp"
        android:layout_y="201dp" />

</AbsoluteLayout>

1 个答案:

答案 0 :(得分:0)

为此,您必须使用标记接口创建数据类(序列化) 例如

public class TransferData implements Serializable {
    private String Block;
    private String addressString;

    public String getAddressString() {
        return addressString;
    }
    public void setAddressString(String addressString) {
        this.addressString = addressString;
    }



    public String getBlock() {
        return block;
    }
    public void setBlock(String block) {
        this.block = block;
    }
}

将额外数据作为对象。

//传递:

TransferData obj = new TransferData();
obj.setBlock("ABC");
obj.setAddressString("ABC");
intent.putExtra("MyClass", obj);  

在另一项活动中获取额外的对象。

//在第二个Activity中检索对象

TransferData obj = getIntent().getSerializableExtra("MyClass");
System.out.println(""+obj.getBlock());