Android对象没有添加任何变量

时间:2014-04-18 21:35:15

标签: android arraylist android-activity add

所以我有这个节目基本上是一个生日提醒,虽然我得到了大部分的想法,但遗憾的是当我想要添加一个朋友而不是,例如,Rick J. 28,1,1980,它添加朋友[name = null,day = 0,month = 0,year = 0]

以下是添加假设的代码:

package com.example.birthdayminder;
import java.util.Date;
import java.util.GregorianCalendar;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends ActionBarActivity {

private int year;
private int month;
private int day;
private String name;
private boolean isOK = false;
GregorianCalendar gregCal;
Date date;
FriendList friendlist;
Friend friend;
EditText text;
CharSequence charSequence;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

}

public void checkFriend(){
    boolean duplicate = false;
    for(int i = 0; i<friendlist.getList().size(); i++){

        if(friendlist.getList().get(i).equals(friend) == true) {
            duplicate = true;
        }
    }
    if (name == null){
        charSequence = (CharSequence)"Name is empty!";
        Toast.makeText(this, charSequence, Toast.LENGTH_SHORT);

    }
    if(day == 0){
        charSequence = (CharSequence)"Not a valid day!";
        Toast.makeText(this, charSequence, Toast.LENGTH_SHORT); 
    }
    if(month == 0){
        charSequence = (CharSequence)"Not a valid month!";
        Toast.makeText(this, charSequence, Toast.LENGTH_SHORT); 
    }
    if(year == 0){
        charSequence = (CharSequence)"Not a valid year!";
        Toast.makeText(this, charSequence, Toast.LENGTH_SHORT); 
    }
    else if(duplicate==true){

        Toast.makeText(this, "This person already added", Toast.LENGTH_SHORT).show();
        isOK =false;

    }
else {
        isOK=true;
    }
}


public void test(View view){
    Toast.makeText(this, "testing",Toast.LENGTH_SHORT).show();
}


public void addClick(View view){
    text = (EditText)findViewById(R.id.namefield);

    name = findViewById(R.id.namefield).toString();
    DatePicker datepick = (DatePicker)findViewById(R.id.datePicker1);
    day = datepick.getDayOfMonth();
    month = datepick.getMonth();
    year = datepick.getYear();
    friend = new Friend(name, day, month, year);
    friendlist = FriendList.getInstance();
    //the toast is to check if the object is added correctly
    Toast.makeText(this, friend.toString(), 4).show();
    checkFriend();
    if(isOK==true){
        friendlist.addFriend(new Friend(name, day, month, year));
        finish();
    }
}

//    This is the original code of the adding process, i attempted to re-do it to try and see if that could help me solve the problem
//    public void saveBirthday(View view){
//        text = (EditText)findViewById(R.id.namefield);
//
//        DatePicker datePick = (DatePicker)findViewById(R.id.datePicker1);
//        name = findViewById(R.id.namefield).toString();
//        day = datePick.getDayOfMonth();
//        month = datePick.getMonth();
//        year = datePick.getYear();
//        fl = FriendList.getInstance();
//        friend = new Friend(name,day,month,year);
//        Toast.makeText(this, friend.toString(), 3).show();
//        checkFriend();
//        if(isOK==true){
//            fl.addFriend(friend);
//            finish();
//        }
//        
//
//    }
}

对于应该存储对象的列表,可以在列表视图中查看它们:

package com.example.birthdayminder;

import java.util.ArrayList;

public class FriendList {

    ArrayList<Friend> friendList = new ArrayList<Friend>();
    private static FriendList instance = null;
    protected FriendList(){
        //prevents accidental creation of new instances
    }

    public static FriendList getInstance(){
        if(instance == null){
            instance = new FriendList();
        }
        return instance;
    }

    //Removes an entry and ads it to the end of the list
    public void moveToEnd(int friendNo){
        Friend chosenFriend =friendList.get(friendNo);
        friendList.remove(friendNo);
        friendList.add(chosenFriend);
    }

    public boolean addFriend(Friend friend){
        return friendList.add(friend);
    }

    public ArrayList getList(){
        return friendList;
    }

    @Override
    public String toString() {
        String name = friendList.get(0).getName();
        String bDay = Integer.toString(friendList.get(0).getMonth());
        String bMonth = Integer.toString(friendList.get(0).getMonth()+1);
        String bYear = Integer.toString(friendList.get(0).getYear());
        return name + "," + bDay + "," + bMonth + "," + bYear;
    }

    public void setList(ArrayList<Friend> friendArray) {
        this.friendList = friendArray;
    }



}

总的来说,我有10个班级,其中3个是不同的班级,列表视图的排序方法,如果问题似乎不在上述任何一个类中,我可以将它们全部发布

1 个答案:

答案 0 :(得分:0)

不确定你要做什么 - 特别是addClick(我不确定这个onClick for DatePicker或任何其他按钮)。无论如何,请看以下两件事:

  1. 将初始化名称和datePicker(使其为类级别)移至onCreate。
  2. 如果没有完成,也会在Friend类中覆盖toString()。
  3. 试试这段代码:

    public class AddActivity extends ActionBarActivity {
    
    private int year;
    private int month;
    private int day;
    private String name;
    private boolean isOK = false;
    GregorianCalendar gregCal;
    Date date;
    FriendList friendlist;
    Friend friend;
    EditText text;
    CharSequence charSequence;
    
    DatePicker datepick;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
    
        text = (EditText) findViewById(R.id.namefield);
    
    
        datepick = (DatePicker) findViewById(R.id.datePicker1);
    
    
    }
    
    public void checkFriend() {
        boolean duplicate = false;
        for (int i = 0; i < friendlist.getList().size(); i++) {
    
            if (friendlist.getList().get(i).equals(friend) == true) {
                duplicate = true;
            }
        }
        if (name == null) {
            charSequence = (CharSequence) "Name is empty!";
            Toast.makeText(this, charSequence, Toast.LENGTH_SHORT);
    
        }
        if (day == 0) {
            charSequence = (CharSequence) "Not a valid day!";
            Toast.makeText(this, charSequence, Toast.LENGTH_SHORT);
        }
        if (month == 0) {
            charSequence = (CharSequence) "Not a valid month!";
            Toast.makeText(this, charSequence, Toast.LENGTH_SHORT);
        }
        if (year == 0) {
            charSequence = (CharSequence) "Not a valid year!";
            Toast.makeText(this, charSequence, Toast.LENGTH_SHORT);
        } else if (duplicate == true) {
    
            Toast.makeText(this, "This person already added",
                    Toast.LENGTH_SHORT).show();
            isOK = false;
    
        } else {
            isOK = true;
        }
    }
    
    public void test(View view) {
        Toast.makeText(this, "testing", Toast.LENGTH_SHORT).show();
    }
    
    public void addClick(View view) {
    
        name = text.getText().toString();
        day = datepick.getDayOfMonth();
        month = datepick.getMonth();
        year = datepick.getYear();
        friend = new Friend(name, day, month, year);
        friendlist = FriendList.getInstance();
        // the toast is to check if the object is added correctly
        Toast.makeText(this, friend.toString(), Toast.LENGTH_LONG).show();
        checkFriend();
        if (isOK == true) {
            friendlist.addFriend(new Friend(name, day, month, year));
            finish();
        }
    }
    
    // This is the original code of the adding process, i attempted to re-do it
    // to try and see if that could help me solve the problem
    // public void saveBirthday(View view){
    // text = (EditText)findViewById(R.id.namefield);
    //
    // DatePicker datePick = (DatePicker)findViewById(R.id.datePicker1);
    // name = findViewById(R.id.namefield).toString();
    // day = datePick.getDayOfMonth();
    // month = datePick.getMonth();
    // year = datePick.getYear();
    // fl = FriendList.getInstance();
    // friend = new Friend(name,day,month,year);
    // Toast.makeText(this, friend.toString(), 3).show();
    // checkFriend();
    // if(isOK==true){
    // fl.addFriend(friend);
    // finish();
    // }
    //
    //
    // }
    }