Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing JSON Populated TextView with JSON Populated ListView
    primarykey
    data
    text
    <p>I have a JSON request which returns a response from youtube containing the comments for a particular video. I currently have 3 textviews: one for the name/uploader, one for the content, and one for the date published - which are then populated with the data from my JSON response. </p> <p>My problem is - only the first comment, date published and uploader appear. </p> <p>I belive I'll need to replace my textviews with a list view and parse the 3 fields to it - I simply do not know how. </p> <h2>JAVA</h2> <p>public class Player extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {</p> <pre><code>public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.player); String title = getIntent().getStringExtra("title"); String uploader = getIntent().getStringExtra("uploader"); String viewCount = getIntent().getStringExtra("viewCount"); TextView titleTv = (TextView) findViewById(R.id.titleTv); TextView uploaderTv = (TextView) findViewById(R.id.uploaderTv); TextView viewCountTv = (TextView) findViewById(R.id.viewCountTv); titleTv.setText(title); uploaderTv.setText("by" + uploader + " |"); viewCountTv.setText(viewCount + " views"); YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview); youTubePlayerView.initialize(API_KEY, this); Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { return false; } }); GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(handler , viewCount); task.execute(); } @Override public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) { Toast.makeText(getApplicationContext(), "onInitializationFailure()", Toast.LENGTH_LONG).show(); } @Override public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { String video_id = getIntent().getStringExtra("id"); player.loadVideo(video_id); } } public final class GetYouTubeUserCommentsTask extends AsyncTask&lt;Void, Void, Void&gt; { public static final String LIBRARY = "CommentsLibrary"; private final Handler replyTo; private final String username; String video_id = getIntent().getStringExtra("id"); public GetYouTubeUserCommentsTask(Handler replyTo, String username) { this.replyTo = replyTo; this.username = username; } @Override protected Void doInBackground(Void... arg0) { try { HttpClient client = new DefaultHttpClient(); HttpUriRequest request = new HttpGet( "http://gdata.youtube.com/feeds/api/videos/" + video_id + "/comments?v=2&amp;alt=json&amp;start-index=1&amp;max-results=50&amp;prettyprint=true"); HttpResponse response = client.execute(request); String jsonString = StreamUtils.convertToString(response .getEntity().getContent()); JSONObject json = new JSONObject(jsonString); JSONArray jsonArray = json.getJSONObject("feed").getJSONArray( "entry"); List&lt;Comments&gt; comments = new ArrayList&lt;Comments&gt;(); for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject entry = jsonArray.getJSONObject(i); JSONArray authorArray = entry.getJSONArray("author"); JSONObject publishedObject = entry.getJSONObject("published"); String published = publishedObject.getString("$t"); JSONObject contentObject = entry.getJSONObject("content"); String content = contentObject.getString("$t"); JSONObject authorObject = authorArray.getJSONObject(0); JSONObject nameObject = authorObject.getJSONObject("name"); String name = nameObject.getString("$t"); comments.add(new Comments(name, content, published)); CommentsLibrary lib = new CommentsLibrary(published, content, name); Bundle data = new Bundle(); data.putSerializable(LIBRARY, lib); Message msg = Message.obtain(); msg.setData(data); replyTo.sendMessage(msg); } } catch (ClientProtocolException e) { Log.e("Feck", e); } catch (IOException e) { Log.e("Feck", e); } catch (JSONException e) { Log.e("Feck", e); } return null; } @Override protected void onPostExecute(Void result) { ListView comments = (ListView) findViewById(R.id.comments); comments.setFilterText(com.idg.omv.domain.CommentsLibrary.getName()); } } </code></pre> <p>}</p> <p>CommentsLibrary</p> <pre><code>public class CommentsLibrary implements Serializable{ // The username of the owner of the comment private static String name; // The comment private static String content; // The date the comment was published private static String published; public CommentsLibrary(String name, String content, String published) { this.name = name; this.content = content; this.published = published; } /** * @return the user name */ public static String getName() { return name; } /** * @return the videos */ public static String getContent() { return content; } /** * @return the videos */ public static String getPublished() { return published; } } </code></pre> <h2>XML</h2> <pre><code> &lt;TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:text="" /&gt; &lt;TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_weight="1" android:gravity="left" android:text="" /&gt; &lt;TextView android:id="@+id/published" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/content" android:layout_weight="1" android:gravity="left" android:text="" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload