如何在单个文本视图上使用条件 if 和 else 来显示结果
How to use condition if and else on single textview to display the results
抱歉,我很高兴搜索该站点但没有找到答案;
我的问题是如何使条件只显示非空结果
在单个文本视图上: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>
如果您应该对问题进行更多解释,将会有所帮助。
我仍然设法理解的是您正在使用字符串(在您的情况下 PE
和 PC
是 String
对象)。
因此,您必须使用:if(!PE.equals(""))
而不是检查 if(PE!="")
因为在使用 Strings
时,表达式 object == value
必须替换为 object.equals(value)
如果您改用这个表达式
会更好
if(PE!=null && !PE.equals(""))
这将确保字符串 PE
已正确初始化并且内部也有一个值。
抱歉,我很高兴搜索该站点但没有找到答案; 我的问题是如何使条件只显示非空结果 在单个文本视图上: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>
如果您应该对问题进行更多解释,将会有所帮助。
我仍然设法理解的是您正在使用字符串(在您的情况下 PE
和 PC
是 String
对象)。
因此,您必须使用:if(!PE.equals(""))
而不是检查 if(PE!="")
因为在使用 Strings
时,表达式 object == value
必须替换为 object.equals(value)
如果您改用这个表达式
会更好
if(PE!=null && !PE.equals(""))
这将确保字符串 PE
已正确初始化并且内部也有一个值。