我是Android编码的新手。我使用SimpleAdapter编写了一个多行List,但文本没有显示。如果我切换到硬编码的哈希表,文本将显示在屏幕上。为了确保我的变量实际上包含文本,我使用Log.e。
打印了它的内容我可能做错了什么?
public class AndroidListViewActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ListParse mytask;
super.onCreate(savedInstanceState);
mytask = new ListParse();
mytask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.layout.activity_main, menu);
return true;
}
private class ListParse extends AsyncTask<URL, Integer, Long> {
// url to make request
private static final String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
// create the grid item mapping
String[] from = new String[] { "TAG_NAME", "TAG_EMAIL", "TAG_PHONE_MOBILE" };
int[] to = new int[] { R.id.list_name, R.id.list_email, R.id.list_mobile };
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
@Override
protected void onPostExecute(Long result) {
Iterator<HashMap<String, String>> entry = contactList.iterator();
while (entry.hasNext()){
Log.e("Debug Info", "We have " + entry.next());
}
SimpleAdapter adapter = new SimpleAdapter(AndroidListViewActivity.this, contactList,
R.layout.list_item, from, to);
setListAdapter(adapter);
}
@Override
protected Long doInBackground(URL... arg0) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
// Phone number is again JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}