我正在尝试从android中的本地JSON
文件中获取文本和图像数据,但它显示错误...我应该更改或添加到上面的代码中以使图像和文本数据获取?
public class ActivityKurti extends ListActivity {
String Kurti="kurti";
String Img="img";
String Name="name";
String Prize="prize";
//********************
Context context; // context
Resources resources = context.getResources();
//**************************
JSONArray KArray=null;
ArrayList<HashMap<String, String>> KurtiList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.item_kurti);
KurtiList = new ArrayList<HashMap<String,String>>();
try
{
String line, line1 = "";
//File fi = new File("sdcard/1.txt");
//BufferedReader br = new BufferedReader(new FileReader(fi));
BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("kurti.json")));
while ((line = br.readLine()) != null)
{
line1 += line;
}
//Log.e("json", line1);
/*JSONObject jOject = new JSONObject(line1);
JSONArray jArray = jOject.getJSONArray("countrie");
String name= "";
for(int i = 0;i<jArray.length();i++){
JSONObject j = jArray.getJSONObject(i);
name += j.getString("name") + "\n";
//Log.e("name",j.getString("name"));
}
new AlertDialog.Builder(this)
.setMessage(name)
.setPositiveButton("ok", null)
.show();*/
JSONObject jo = new JSONObject(line1);
KArray = jo.getJSONArray(Kurti);
for(int i = 0; i< KArray.length(); i++)
{
JSONObject ijo = KArray.getJSONObject(i);
//8888888888888888888888888888888
String imageName = ijo.getString("img");
final int resourceId = resources.getIdentifier(imageName, "drawable", context.getPackageName());
Drawable drawable = resources.getDrawable(resourceId);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
//888888888888888888888888888888888888888888888
String nm = ijo.getString(Name);
String pz = ijo.getString(Prize);
HashMap<String,String> row = new HashMap<String, String>();
row.put(Name, nm);
row.put(Prize, pz);
//88888888888888888888888888888888
final int resourceId1 = resources.getIdentifier(imageName, "drawable", context.getPackageName());
row.put("flag", Integer.toString(resourceId1) );
//8888888888888888888888888888888888888888888
KurtiList.add(row);
}
}
catch(Exception e)
{
e.printStackTrace();
}
ListAdapter la = new SimpleAdapter(getApplicationContext(), KurtiList,R.layout.item_kurti, new String[] {Name,Prize,Img}, new int[] {R.id.txtname, R.id.txtprize,R.id.imgkurti});
setListAdapter(la);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Ship_Info:
Intent in=new Intent(this,ActivityShipInfo.class);
startActivity(in);
return true;
case R.id.Contact_Us:
Intent in2=new Intent(this,ActivityContactInfo.class);
startActivity(in2);
return true;
case R.id.Policies:
Intent in3=new Intent(this,ActivityPolicies.class);
startActivity(in3);
return true;
case R.id.Main_menu:
Intent in1=new Intent(this,MenuActivity.class);
startActivity(in1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
提前致谢。