2017-03-27 5 views
0

申し訳ありませんが、私は答えを見つけることなくサイトを検索する良い時間を持っています。 私の質問は、空でない結果だけを表示する条件を作る方法です。 はシングルテキストビューに表示されます:id = t。結果を表示するには、ifおよびelseをシングルテキストビューで使用する方法

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> 

答えて

0

質問の詳細を説明しておく必要がある場合に役立ちました。
私が理解したことは、文字列(あなたの場合はPEPCStringオブジェクト)で作業していることです。
ので、代わりにif(PE!="")をチェックし、あなたが使用する必要があります。if(!PE.equals(""))
Stringsでの作業中なので、あなたがこの式の代わりに
if(PE!=null && !PE.equals(""))を使用場合object == valueobject.equals(value)
に置き換える必要があります式はそれが
良いだろう
これは、文字列PEが適切に初期化され、内部に値があることを確認します。

+0

ありがとうございます。これはPE.equals( "")と!Objects.equals(PE、 "") - –

+0

と一緒に使えます。@RabahBoulharesを教えてください。 私の答えに印を付けるとうれしいでしょう。 –

関連する問題