所以我有包含此代码的json文件:
{
"gejala": "Apakah hawar berwarna kuning terang menuju ujung daun",
"gejala_kode": "GJL0025",
"penyakit_kode": "PNY0012",
"gambar_gejala":"hawar_daun",
"child": null
}
hawar_daun是我放在可绘制文件夹中的图像的名称。
我的xml部分:
<RelativeLayout
android:id="@+id/big_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:background="@drawable/bubble1"
android:layout_marginTop="20dip">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="null"
android:textSize="17dp"
android:id="@+id/ask"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/gambargejala"
/>
</RelativeLayout>
这是我想要放置图像的java代码的一部分。
public class KonsultasiBerdasarkanGejalaActivity extends AppCompatActivity {
public static final String ASK_GEJALA = "Apakah terdapat tanda-tanda ini?" ;
int qIndex = 0;
int level = 0;
TextView header;
TextView ask;
ImageView askgambar;
Button ya;
Button tidak;
ScrollView lyContainer;
boolean calculate = false;
boolean fromTidak = false;
int calcCount = 0;
int yaCount = 0;
int tidakCount = 0;
boolean lastQuestion = false;
PohonKeputusan pohonKeputusan;
Padi padi;
String[] gejalaPadi;
ArrayList<PenyakitPadi> allPenyakit;
ArrayList<PenyakitPadi> relPenyakitPadiArr;
ArrayList<PohonKeputusan> pohonKeputusanObjArr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konsultasi_start);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
setTitle(" Menu Konsultasi");
toolbar.setSubtitle(" Berdasarkan Gejala");
lyContainer = (ScrollView) findViewById(R.id.lyContainer);
Intent padiIntent = getIntent();
padi = padiIntent.getExtras().getParcelable("padiObj");
gejalaPadi = padi.gejala;
relPenyakitPadiArr = setRelatedPenyakit();
try {
pohonKeputusanObjArr = setRelatedGejala(initiatePohonKeputusan());
Log.e("pohonKeputusanObjArr", String.valueOf(pohonKeputusanObjArr.size()));
Log.e("pohonKeputusanObjArr", String.valueOf(pohonKeputusanObjArr.toString()));
} catch (IOException e) {
e.printStackTrace();
}
pohonKeputusan = pohonKeputusanObjArr.get(qIndex);
//Log.e("pohonKeputusan", String.valueOf(pohonKeputusan.gejala_kode));
String namaPadi = padi.nama;
if (namaPadi != null) {
namaPadi = namaPadi.substring(0,1).toUpperCase() + namaPadi.substring(1).toLowerCase();
header = (TextView) findViewById(R.id.textView4);
String ni = header.getText().toString();
ni = ni + "\"" + namaPadi + "\"";
header.setText(ni);
}
ask = (TextView) findViewById(R.id.ask);
askgambar=(ImageView) findViewById(R.id.gambargejala);
}
public ArrayList<PohonKeputusan> setRelatedGejala(ArrayList<PohonKeputusan> phk){
ArrayList<PohonKeputusan> phkObjArr = new ArrayList<PohonKeputusan>();
for (int i = 0; i < phk.size(); i++){
boolean result = false;
PohonKeputusan phkObj = phk.get(i);
String gejalaKode = phkObj.gejala_kode;
for (int i2 = 0; i2 < gejalaPadi.length; i2++){
if(gejalaKode.equals(gejalaPadi[i2])){
result = true;
break;
}
}
if(result == true) phkObjArr.add(phkObj);
}
return phkObjArr;
}
public void rollbackQuestion(ArrayList<PohonKeputusan> phk){
qIndex = 0;
pohonKeputusan = phk.get(qIndex);
}
public void setQuestion(PohonKeputusan phk){
ask.setText(phk.gejala);
------------------------------------??????????----------------------------
}
我必须在最后一行的最后一行输入什么代码才能从json的名称文件中设置图像?
谢谢你。
答案 0 :(得分:0)
如果我理解正确,您会尝试将字符串转换为资源ID。 如果是这种情况,您可以使用getResources().getIdentifier(name, type, package)
这是一个例子。
public void setQuestion(PohonKeputusan phk){
ask.setText(phk.gejala);
askgambar.setImageResource(getResources().getIdentifier(phk. gambar_gejala, "drawable", getPackageName()));
}