好的,所以我发现本教程将 mi JSON 数据解析为 ListView (http://www.androidhive.info/2012/01/android-json-parsing-tutorial/),但我无法让它工作,我不知道发生了什么,我不断收到的错误是“例外:您的内容必须有一个列表视图,它是'android.r.id.list'”,这是我的代码:
package com.example.tweev;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Tweev extends ListActivity {
// url to make request
//private static String url = "http://api.androidhive.info/contacts/ ";
private static String url = "http://www.tweevenvivo.com/service/Tweev.getActiveShows";
// JSON Node names
private static final String TAG_DATA = "data";
private static final String TAG_TWEEVERS = "tweevers";
private static final String TAG_ID = "id";
private static final String TAG_CATEGORY = "category";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_NAME = "name";
private static final String TAG_HASHTAG = "hashtag";
private static final String TAG_CHANNELS = "channels"; //contains "operators", "hour and "operator name"
private static final String TAG_OPERATORS = "operators"; //contains "channel" with "code"
private static final String TAG_OPCHANNEL = "channel";
private static final String TAG_OPCODE = "code";
private static final String TAG_CHHOUR = "hour";
private static final String TAG_CHNAME = "name";
private static final String TAG_IMAGE_SMALL = "image_small";
private static final String TAG_IMAGE = "image";
private static final String TAG_SHOW_ID = "show_id";
// tweevs JSONArray
JSONArray tweevs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweev);
TextView view = (TextView) findViewById(R.id.TextView01);
String s="";
for (int i=0; i < 500; i++) {
s += "TEST "+i+" ";
}
view.setText(s);
getData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tweev, menu);
return true;
}
private void getData() {
// Hashmap for ListView
ArrayList<HashMap<String, String>> tweevList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
//Log.d(TAG_DATA, "json " + json);
try {
// Getting Array of Contacts
tweevs = json.getJSONArray(TAG_DATA);
Log.d(TAG_DATA, "tweevs " + tweevs);
// looping through All tweevs
for(int i = 0; i < 10; i++) {//tweevs.length()
JSONObject c = tweevs.getJSONObject(i);
// Storing each json item in variable
String tweevers = c.getString(TAG_TWEEVERS);
String id = c.getString(TAG_ID);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String name = c.getString(TAG_NAME);
String hashtag = c.getString(TAG_HASHTAG);
//String channels = c.getString(TAG_CHANNELS);
String image_small = c.getString(TAG_IMAGE_SMALL);
String image = c.getString(TAG_IMAGE);
String show_id = c.getString(TAG_SHOW_ID);
// operators is again a JSON Object
JSONArray channels = c.getJSONArray(TAG_CHANNELS);
for(int j = 0; j < channels.length(); j++) {
JSONObject channel = channels.getJSONObject(j);
JSONArray operators = channel.getJSONArray(TAG_OPERATORS);
for(int k = 0; k < operators.length(); k++){
JSONObject operator = operators.getJSONObject(k);
String channelName = operator.getString(TAG_OPCHANNEL);
String channelCode = operator.getString(TAG_OPCODE);
}
String chhour = channel.getString(TAG_CHHOUR);
String chname = channel.getString(TAG_CHNAME);
//Log.d(TAG_DATA, "operator channel "+ k + " ::: " +channelName+" __CODE__ "+channelCode);
//Log.d(TAG_DATA, "channel hour "+ j + " ::: " +chhour+" --channel name-- "+chname);
//Log.d(TAG_DATA, "operators "+ j + " ::: " +channel+" --- "+code);
//Log.d(TAG_DATA, "operators "+ j + " ::: " +operators);
}
//Log.d(TAG_DATA, "tweev "+ i + " ::: " +channels);
// 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_HASHTAG, hashtag);
map.put(TAG_IMAGE_SMALL, image_small);
// adding HashList to ArrayList
tweevList.add(map);
}
} catch (JSONException e) {
Log.d(TAG_DATA, "MOTHERFUCKER!");
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, tweevList,
R.layout.list_items,
new String[] { TAG_NAME, TAG_HASHTAG, TAG_IMAGE_SMALL }, new int[] {
R.id.name, R.id.name, R.id.hashtag });
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 name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
*/
}
});
}
}
我的猜测是我的 XML 文件有问题,在他们的代码中,我看到他们使用了 OnCreate 函数的布局,而在创建 ListAdapter 时使用了另一个布局,我不明白为什么会这样,我也不知道这些布局的内容是什么...