人详细应用

时间:2015-01-31 09:50:44

标签: java android

我对Android编程很新(7个月),我在创建应用时遇到了问题。 我想要做的是为我自己的Android学习创建一个“人物细节”测试应用程序,在主活动上使用EditText并在其旁边显示默认值(首先显示默认值),打开另一个名为EditData的活动(带有主菜单上的按钮,显示用户在EditText中输入的值(通过Parcelable),返回主Activty(通过另一个按钮),并在EditText旁边的TextViews中显示输入的值(在“更新信息”上) “按钮单击”而不是默认值。除了显示更改的值之外,我已设法使一切正常工作。一切都检查出来,LogCat中没有错误,但是当用户/我点击更新按钮时,显示的是默认值。有人能帮我吗? 这是我的主要活动代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
    final Person firstPerson = new Person();
    TextView displayName, displayAge, displayHeight, displayWeight;
    EditText editName, editAge, editHeight, editWeight;
    Button editActivity, update;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    instantiateUi();
    editActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            firstPerson.setPersonName(editName.getText().toString());
            firstPerson.setPersonAge(Integer.parseInt(editAge.getText().toString()));
            firstPerson.setPersonHeight(Integer.parseInt(editHeight.getText().toString()));
            firstPerson.setPersonWeight(Integer.parseInt(editWeight.getText().toString()));
            Intent editIntent = new Intent(MainActivity.this, EditData.class);
            editIntent.putExtra("First_Person_Data", firstPerson);
            MainActivity.this.startActivity(editIntent);
        }
    });
    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayName.setText(firstPerson.getPersonName());
            displayAge.setText(String.valueOf(firstPerson.getPersonAge()));
            // String.valueOf is needed to set int for TextViews
            displayHeight.setText(String.valueOf(firstPerson.getPersonHeight()));
            displayWeight.setText(String.valueOf(firstPerson.getPersonWeight()));
        }
    });
}

private void instantiateUi() {
    editName = (EditText)findViewById(R.id.edit_name);
    editAge = (EditText)findViewById(R.id.edit_age);
    editHeight = (EditText)findViewById(R.id.edit_height);
    editWeight = (EditText)findViewById(R.id.edit_weight);
    displayName = (TextView)findViewById(R.id.display_name);
    displayAge = (TextView)findViewById(R.id.display_age);
    displayHeight = (TextView)findViewById(R.id.display_height);
    displayWeight = (TextView)findViewById(R.id.display_weight);
    editActivity = (Button)findViewById(R.id.edit_activity);
    update = (Button)findViewById(R.id.update_info);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}
}    

我的EditData活动代码:

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.widget.Button;
import android.widget.TextView;


public class EditData extends ActionBarActivity {
    TextView changedName, changedAge, changedHeight, changedWeight;
    Button MainActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_activity);
    Person incomingObject = getIntent().getParcelableExtra("First_Person_Data");
    instantiateUi();
    changedName.setText(incomingObject.getPersonName());
    changedAge.setText(String.valueOf(incomingObject.getPersonAge()));
    changedHeight.setText(String.valueOf(incomingObject.getPersonHeight()));
    changedWeight.setText(String.valueOf(incomingObject.getPersonWeight()));
    MainActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent EditActivity = new Intent(EditData.this, MainActivity.class);
            EditData.this.startActivity(EditActivity);
        }
    });
}

private void instantiateUi() {
    changedName = (TextView)findViewById(R.id.changed_name);
    changedAge = (TextView)findViewById(R.id.changed_age);
    changedHeight = (TextView)findViewById(R.id.changed_height);
    changedWeight = (TextView)findViewById(R.id.changed_weight);
    MainActivity = (Button)findViewById(R.id.main_activity);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.edit_data_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}    

我的Parcelable类用于传输数据值:

import android.os.Parcel;
import android.os.Parcelable;

public class Person implements Parcelable{
    String personName;
    int personAge, personHeight, personWeight;

public Person(){
    personName = "No Name!";
    personAge = 0;
    personHeight = 0;
    personWeight = 0;
}

public Person(String pName, int pAge, int pHeight, int pWeight) {
    personName = pName;
    personAge = pAge;
    personHeight = pHeight;
    personWeight = pWeight;
}

public Person(Parcel in) {
    personName = in.readString();
    personAge = in.readInt();
    personHeight = in.readInt();
    personWeight = in.readInt();
}

public String getPersonName() {
    return personName;
}

public void setPersonName(String personName) {
    this.personName = personName;
}

public int getPersonAge() {
    return personAge;
}

public void setPersonAge(int personAge) {
    this.personAge = personAge;
}

public int getPersonHeight() {
    return personHeight;
}

public void setPersonHeight(int personHeight) {
    this.personHeight = personHeight;
}

public int getPersonWeight() {
    return personWeight;
}

public void setPersonWeight(int personWeight) {
    this.personWeight = personWeight;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(personName);
    dest.writeInt(personAge);
    dest.writeInt(personHeight);
    dest.writeInt(personWeight);
}

public static final Parcelable.Creator<Person> CREATOR =
        new Parcelable.Creator<Person>() {
            @Override
            public Person createFromParcel(Parcel in) {
                return new Person(in);
            }

            @Override
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
}    

对不起,如果有问题,这是我的第一个问题,我非常感谢你能给予菜鸟C0D3R的帮助:P

1 个答案:

答案 0 :(得分:1)

您不应该使用大写字母来启动变量名称。您的按钮MainActivity非常混乱。更好地命名为btMainActivity之类的东西。

它认为您的问题是,您忘记为MainActivity添加额外费用,就像您在启动EditData时所做的那样:

editActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            firstPerson.setPersonName(editName.getText().toString());
            firstPerson.setPersonAge(Integer.parseInt(editAge.getText().toString()));
            firstPerson.setPersonHeight(Integer.parseInt(editHeight.getText().toString()));
            firstPerson.setPersonWeight(Integer.parseInt(editWeight.getText().toString()));
            Intent editIntent = new Intent(MainActivity.this, EditData.class);
            editIntent.putExtra("First_Person_Data", firstPerson);
            MainActivity.this.startActivity(editIntent);
        }
    });

MainActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent EditActivity = new Intent(EditData.this, MainActivity.class);
            // put your extra here 
            EditData.this.startActivity(EditActivity);
        }
    });

您也可以使用startActivityForResult。看看吧!您还应该查看AndroidAnnotations。它可以保护你很多代码和时间。我无法想象不再使用Android应用程序。