Java EditText无法正确验证

时间:2017-05-12 14:03:03

标签: java android

有人可以帮忙吗?我的EditText不是空的,但吐司仍然显示出来。我的应用程序要求用户选择日期和时间,然后在列表视图中选择1项以继续。之后会弹出一个对话框。但是出于某种原因,即使我的edittext不为空,它仍然不允许我继续。我似乎无法弄清楚什么是错的,我的意思是代码就是那么简单,没什么复杂的。

final String date = textDate1.getText().toString().trim();
    final String time = textTime1.getText().toString().trim();

listViewHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    final Team team = teams.get(i);

    if(TextUtils.isEmpty(date)){
        Toast.makeText(RecreateActivity.this,"Please choose a date.",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(time)){
        Toast.makeText(RecreateActivity.this,"Please choose a time.",Toast.LENGTH_LONG).show();
        return;
    }

//the rest of the code

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(RecreateActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.confirm_layout, null);
    dialogBuilder.setView(dialogView);

    final Button buttonYes2 = (Button) dialogView.findViewById(R.id.buttonYes2);
    final Button buttonNo2 = (Button) dialogView.findViewById(R.id.buttonNo2);
    //final Team team = teams.get();

    final AlertDialog b = dialogBuilder.create();
    b.show();

    buttonYes2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            databaseMembers.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final ArrayList<String> CheckList = new ArrayList<String>();
                    for (DataSnapshot check : dataSnapshot.child("teams").getChildren()) {
                        CheckList.add(check.getKey());
                    }
                    if (CheckList.contains(team.getTeamName())) {
                        Toast.makeText(RecreateActivity.this, "Team already exist.", Toast.LENGTH_LONG).show();
                        return;
                    }
                    databaseMembers.child("History").child(team.getTeamName()).child("date").setValue(date);
                    databaseMembers.child("History").child(team.getTeamName()).child("time").setValue(time);

                    for (DataSnapshot history : dataSnapshot.child("History").child(encodedEmailAddress).getChildren()) {
                        String key = history.getKey();
                        if (key.equals(team.getTeamName())) {
                            teams.clear();
                            Team team = history.getValue(Team.class);
                            teams.add(team);
                            databaseTeams.child(team.getTeamName()).setValue(team);
                        }
                        if (key.equals("teamMember")) {
                            for (DataSnapshot members : dataSnapshot.child("History").child(encodedEmailAddress).child("teamMember").getChildren()) {
                                String key2 = members.getKey();
                                String value = members.getValue(String.class);
                                Map<String, Object> map = new HashMap<>();
                                map.put(key2, value);

                                databaseMembers.child("members").child(team.getTeamName()).child("teamMember").updateChildren(map);
                                b.dismiss();
                            }
                        }

                    }
                    Toast.makeText(RecreateActivity.this, "Team created.", Toast.LENGTH_LONG).show();

                    Intent myIntent = new Intent(RecreateActivity.this,
                            MainActivity.class);
                    startActivity(myIntent);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });
}

XML:

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Previous Team"
    android:textAppearance="@style/TextAppearance.AppCompat.Title"
    android:textAlignment="center"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Select new Date/Time and tap on the Team."
    android:textAlignment="center"/>


<ListView
    android:id="@+id/listViewHistory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

<TextView
    android:id="@+id/textView5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Members:"
    android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

<TextView
    android:id="@+id/textViewList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

<EditText
    android:id="@+id/textDate1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Select Date..."
    android:layout_alignParentStart="true" />

<EditText
    android:id="@+id/textTime1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Select Time..."
    android:layout_below="@+id/textDate"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/buttonAddHistory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Create Team"
    android:textAlignment="center"
    android:textAllCaps="false"
    tools:textSize="20sp" />

5 个答案:

答案 0 :(得分:0)

您的字段仅初始化一次。我不知道上下文,这可能实际上是你想要的,但是每次验证它都没有任何意义,每次运行监听器代码时这些字段都具有相同的值。尝试添加代码以在侦听器本身中获取日期和时间。

答案 1 :(得分:0)

您的代码正在设置onClickListener的[date]和[time] 。当用户单击该按钮时,您的代码不会重置变量。我将直接从你的onClickListener代码评估EditText:

listViewHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    final Team team = teams.get(i);

    if(TextUtils.isEmpty(textDate1.getText().toString().trim())){
        Toast.makeText(RecreateActivity.this,"Please choose a date.",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(textTime1.getText().toString().trim())){
        Toast.makeText(RecreateActivity.this,"Please choose a time.",Toast.LENGTH_LONG).show();
        return;
    }

答案 2 :(得分:0)

在设置EditText之前,您只需从OnItemClickListener获取一次字符串。你需要在监听器中获取字符串。

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    final Team team = teams.get(i);
    final String date = textDate1.getText().toString().trim();
    final String time = textTime1.getText().toString().trim();

    if(TextUtils.isEmpty(date)){
        Toast.makeText(RecreateActivity.this,"Please choose a date.",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(time)){
        Toast.makeText(RecreateActivity.this,"Please choose a time.",Toast.LENGTH_LONG).show();
        return;
    }

}

答案 3 :(得分:0)

尝试在点击侦听器中获取edittext的日期值。

答案 4 :(得分:0)

listViewHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        final Team team = teams.get(i);
    final String date = textDate1.getText().toString().trim();
        final String time = textTime1.getText().toString().trim();

        if(date.isEmpty()){
            Toast.makeText(RecreateActivity.this,"Please choose a date.",Toast.LENGTH_LONG).show();
            return;
        }

        if(time.isEmpty()){
            Toast.makeText(RecreateActivity.this,"Please choose a time.",Toast.LENGTH_LONG).show();
            return;
        }