我是android的初学者..我想显示来自JSON的文本和图像...在这里我可以显示文本但不能显示图像...有人可以帮助我吗?
This is my JSON :
{
" countries":[
{"countryname":"India",
"flag":"http://******.in/p/demo1/india.png" },
{"countryname":"****",
"flag":"http://******.in/p/demo1/*****.png" },
...........................................and so on
]
}
在这里我可以显示国家名称,但无法显示图像(标志) ......国家名称和显示在列表视图中的标志......如果我点击任何项目,它必须显示国家名称及其另一个活动中的标志....我得到国家名称,但没有图像视图中的标志....
这是我的代码:
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://*****@#$$$@#$.in/p/demo1/first.php/countries";
// JSON Node names
private static final String TAG_COUNTRIES = "countries";
private static final String TAG_COUNTRYNAME = "countryname";
private static final String TAG_FLAG= "flag";
JSONArray countries = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
countries = json.getJSONArray(TAG_COUNTRIES);
// looping through All Contacts
for(int i = 0; i < countries.length(); i++){
JSONObject c = countries.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_COUNTRYNAME);
String flag= c.getString(TAG_FLAG);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_COUNTRYNAME, id);
map.put(TAG_LANGUAGE, name);
map.put(TAG_CAPITAL, email);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,
new String[] { TAG_COUNTRYNAME, TAG_FLAG}, new int[] {
R.id.country_name, R.id.flag});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String country_name = ((TextView) view.findViewById(R.id.countyr_name)).getText().toString();
String flag= ((ImageView) view.findViewById(R.id.flag)).getTag().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_COUNTRYNAME, country_name);
in.putExtra(TAG_FLAG, flag);
startActivity(in);
}
});
}
}