如果不是在Android应用工作/ else语句(else语句总是执行) [英] if /else statement not working in android app (else statement always executed)

查看:157
本文介绍了如果不是在Android应用工作/ else语句(else语句总是执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,在采用两个用户输入,并将其匹配于存储在数据库中的数据并显示相应的数据(行)从用户输入的 TextView的

if语句完美的作品别说如果条件为真。然而,不执行,即使如果我添加else语句的条件为真。

就总是执行else语句如果该语句是真的还是假的。

 保护无效onPostExecute(虚空V){
    // ambil数据达里语的Json数据库
    尝试{
        JSONArray Jarray =新JSONArray(结果);
        的for(int i = 0; I< Jarray.length();我++){
            JSONObject的Jasonobject = NULL;
            // = _1(的TextView)findViewById(R.id.txt1);
            Jasonobject = Jarray.getJSONObject(ⅰ);            //获取屏幕上的输出
            //字符串ID = Jasonobject.getString(ID);
            字符串名称= Jasonobject.getString(名称);
            串db_detail =;            如果(et.getText()。的toString()。equalsIgnoreCase(名)){
                db_detail = Jasonobject.getString(详细信息);
                text.setText(db_detail);
                打破;
            }其他{
                Toast.makeText(MainActivity.this,不可用,Toast.LENGTH_LONG).show();
                打破;
            }
        }
        this.progressDialog.dismiss();


解决方案

使用标志变量来检查用户是否是可用的,并为循环之后打印信息。

 布尔可用= FALSE;JSONArray Jarray =新JSONArray(结果);
的for(int i = 0; I< Jarray.length();我++)
{
    JSONObject的Jasonobject = NULL;
    // = _1(的TextView)findViewById(R.id.txt1);
    Jasonobject = Jarray.getJSONObject(ⅰ);    //获取屏幕上的输出
    //字符串ID = Jasonobject.getString(ID);
    字符串名称= Jasonobject.getString(名称);
    串db_detail =;
    如果(et.getText()。的toString()。equalsIgnoreCase(名))
    {
        db_detail = Jasonobject.getString(详细信息);
        text.setText(db_detail);
        可用= TRUE;
        打破;
    }
}如果可供使用的话)
      Toast.makeText(MainActivity.this,不可用,Toast.LENGTH_LONG).show();

I am writing an app that takes two user inputs and matches them to data stored in a database and displays the corresponding data(row) from the user inputs in a TextView.

The if statement works perfectly alone if the condition is true. It is however not executed even if the condition is true if I add the else statement.

The else statement is always executed if the statement is true or false

protected void onPostExecute(Void v) {
    // ambil data dari Json database
    try {
        JSONArray Jarray = new JSONArray(result);
        for (int i = 0; i < Jarray.length(); i++) {
            JSONObject Jasonobject = null;
            //text_1 = (TextView)findViewById(R.id.txt1);
            Jasonobject = Jarray.getJSONObject(i);

            //get an output on the screen
            //String id = Jasonobject.getString("id");
            String name = Jasonobject.getString("name");
            String db_detail="";

            if (et.getText().toString().equalsIgnoreCase(name)) {
                db_detail = Jasonobject.getString("detail");
                text.setText(db_detail);
                break;
            } else {
                Toast.makeText(MainActivity.this, "Not available", Toast.LENGTH_LONG).show();
                break;
            }
        }
        this.progressDialog.dismiss();

解决方案

Use flag variable to check whether user is available or not, and print your message after for loop.

boolean available=false;

JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
    JSONObject Jasonobject = null;
    //text_1 = (TextView)findViewById(R.id.txt1);
    Jasonobject = Jarray.getJSONObject(i);

    //get an output on the screen
    //String id = Jasonobject.getString("id");
    String name = Jasonobject.getString("name");
    String db_detail="";
    if(et.getText().toString().equalsIgnoreCase(name)) 
    {
        db_detail = Jasonobject.getString("detail");
        text.setText(db_detail);
        available=true;
        break;
    } 
}

if(!available)
      Toast.makeText(MainActivity.this, "Not available", Toast.LENGTH_LONG).show();

这篇关于如果不是在Android应用工作/ else语句(else语句总是执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆