如何在单个textview上使用条件if和else来显示结果

时间:2017-03-27 02:45:10

标签: java android json android-arrayadapter android-spinner

抱歉,我很开心在没有找到答案的情况下搜索网站; 我的问题是如何使条件只显示非空结果 在单个textview上:id = t。

data.json 档案:

  {
    "interactions":
    [
         {
           "MDT1": "ACIDE ACETOHYDROXAMIQUE",
           "MDT2": "FER",
           "CI": "",
           "AD": "",
           "PE": "",
           "PC": "A prendre en compte Diminution de l'absorption digestive de ces deux medicaments par chelation du fer..."
         },
         {
           "MDT1": "ACETAZOLAMIDE",
           "MDT2": "ACIDE ACETYLSALICYLIQUE",
           "CI": "",
           "AD": "Association DECONSEILLEE Majoration des effets indesirables.....",
           "PE": "",
           "PC": ""
         },
         {
           "MDT1": "ACETAZOLAMIDE",
           "MDT2": "CARBAMAZEPINE",
           "CI": "",
           "AD": "",
           "PE": "Precaution d'emploi Surveillance clinique........",
           "PC": ""
         },
         {
           "MDT1": "CARBAMAZEPINE",
           "MDT2": "OLANZAPINE",
           "CI": "",
           "AD": "",
           "PE": "Precaution d'emploi Surveillance clinique, et si besoin, adaptation posologique de l'olanzapine. Risque de diminution des concentrations plasmatiques de l'olanzapine et de son efficacite therapeutique, par augmentation de son metabolisme hepatique par la carbamazepine.",
           "PC": ""
         }
    ]
}

MainActivity.java

public SearchableSpinner mySpinner;
private SearchableSpinner mySpinner1;
public ArrayList<Interactions> world = new ArrayList<>();
public final ArrayList<String> mStrings = new ArrayList<>();
public final ArrayList<String> m2Strings = new ArrayList<>();
.....

 public class DownloadJSON extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            // Locate the Interactions Class
            jsonobject = JSONfunctions.getJSONfromURL("https://rabah.000webhostapp.com/data.txt");
            try {
                // Locate the NodeList name
                jsonarray = jsonobject.getJSONArray("interactions");
                for (int j = 0; j < jsonarray.length(); j++ ) {
                    jsonobject = jsonarray.getJSONObject(j);
                    Interactions worldpop = new Interactions();
                    worldpop.setMDT1(jsonobject.optString("MDT1"));
                    worldpop.setMDT2(jsonobject.optString("MDT2"));
                    worldpop.setPE(jsonobject.optString("PE"));
                    worldpop.setCI(jsonobject.optString("CI"));
                    worldpop.setAD(jsonobject.optString("AD"));
                    worldpop.setPC(jsonobject.optString("PC"));
                    world.add(worldpop);
                    // Populate spinner with mdts names
                    mStrings.add(jsonobject.optString("MDT1"));
                    m2Strings.add(jsonobject.optString("MDT2"));
                }
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void args) {
            // Spinner adapter
            mySpinner.setAdapter(new SimpleListAdapter(MainActivity.this,
                    android.R.layout.simple_spinner_dropdown_item, mStrings));

            mySpinner1.setAdapter(new SimpleListAdapter(MainActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,m2Strings));
        }}

 private OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() {

        @Override
            public void onItemSelected(View view, int position, long id) {
                TextView txtMDT1 = (TextView) findViewById(R.id.MDT1);
                txtMDT1.setText("MDT1 : " + world.get(position).getMDT1());

                TextView txtMDT2 = (TextView) findViewById(R.id.MDT2);
                txtMDT2.setText("MDT2 : " + world.get(position).getMDT2());


                String PE = world.get(position).getPE();
                String PC = world.get(position).getPC();
                String CI = world.get(position).getCI();
                String AD = world.get(position).getAD();

                if (PE!="")
                {
                    TextView txtPE = (TextView) findViewById(R.id.t);
                    txtPE.setText("PE : " + world.get(position).getPE());
                }else if (PC!="") {
                    TextView txtPc = (TextView) findViewById(R.id.t);
                    txtPc.setText("Pc : " + world.get(position).getPC());
                }
            }

            @Override
            public void onNothingSelected() {
                Toast.makeText(MainActivity.this, "Nothing Selected", Toast.LENGTH_SHORT).show();
            }
        };

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="gr.escsoft.michaelprimez.searchablespinnerexamples.MainActivity"
    tools:showIn="@layout/activity_main">


    <gr.escsoft.michaelprimez.searchablespinner.SearchableSpinner
        android:id="@+id/mySpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        app:RevealEmptyText="Touch to select"
        android:gravity="center_horizontal"
        app:ShowBorders="true"
        app:BordersSize="1dp"
        app:BoarderColor="@color/colorPrimary"
        app:SpinnerExpandHeight="250dp"/>

    <gr.escsoft.michaelprimez.searchablespinner.SearchableSpinner
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/mySpinner1"
        app:SpinnerExpandHeight="0dp"
        app:BoarderColor="@android:color/holo_blue_light"
        app:BordersSize="1dp" app:ShowBorders="true"
        android:gravity="center_horizontal"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        app:RevealEmptyText="Touch to select"
        app:SearchViewBackgroundColor="@android:color/holo_blue_light"
        app:RevealViewBackgroundColor="@android:color/holo_blue_light"
        app:DoneSearchTintColor="@android:color/white"
        app:StartSearchTintColor="@android:color/white"
        android:layout_below="@+id/mySpinner"/>
    <TextView
        android:id="@+id/MDT1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mySpinner1" />
    <TextView
        android:id="@+id/MDT2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MDT1" />
    <TextView
        android:id="@+id/t"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MDT2" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果您应该更多地解释这个问题,那将会有所帮助。
我设法理解的仍然是你正在使用字符串(在你的情况下PEPCString个对象)。
因此,您必须使用:if(PE!="")而不是检查if(!PE.equals(""))
因为在使用Strings时,必须将object == value替换为object.equals(value)
如果使用此表达式,则更好 if(PE!=null && !PE.equals(""))
这样可以确保字符串PE已正确初始化并且内部也有值。