【发布时间】:2013-06-27 16:27:47
【问题描述】:
我一直在尝试在 Android 中找到一个“解析 Youtube Gdata JSON”的工作示例来填充列表视图。
我一直在阅读 this question,除了 4 个错误之外,我几乎可以正常工作。
-
item_title
-
item_subtitle
-
setListAdapter
-
获取列表视图
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; public class listview extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview_main); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); //Get the data (see above) JSONObject json = getJSON.getJSONfromURL("https://gdata.youtube.com/feeds/api/videos?q=random&max-results=50&v=2&alt=jsonc"); try{ final JSONArray array = json.getJSONArray("items"); //Loop the Array for(int i=0;i < array.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = array.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("title", "" + e.getString("title")); map.put("viewCount", "Views: " + e.getString("viewCount")); mylist.add(map); } }catch(JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.dubstep, new String[] { "title", "viewCount" }, new int[] { R.id.item_title, R.id.item_subtitle }); setListAdapter(adapter); final ListView lv = getListView(); //final ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); Toast.makeText(listview.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.listview, menu); return true; } }
上面写着"Cannot resolve symbol 'item_title' and 'item_subtitle'
和
"Cannot resolve method 'setListAdapter(android.widget.ListAdapter)'
感谢任何帮助,谢谢!
【问题讨论】:
-
您的代码中的
item_title在哪里?你能发同样的吗?贴一些更相关的代码。 -
你是这个意思吗?
new int[] { R.id.item_title, R.id.item_subtitle }); -
发布整个代码和堆栈跟踪。
-
我对 Android 编程比较陌生,但是如何获取堆栈跟踪?
-
您可以查看更新后的答案。但是最好把位图的url提取出来,做懒加载。查看 logcat goto windows open pepresctive。去找别人。打开ddms。日志猫。
标签: android json android-listview