Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Continuing my answer from your previous question @ <a href="https://stackoverflow.com/questions/20548108/json-string-is-incorrectly-mapped-to-textviews">JSON String is incorrectly mapped to textviews</a></p> <p>First you need a listview in your layout xml </p> <pre><code>&lt;ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; </code></pre> <p>Declare list as a instance variable</p> <pre><code> ArrayList&lt;CommentsLibrary&gt; list = new ArrayList&lt;CommentsLibrary&gt;(); </code></pre> <p>Then</p> <pre><code> for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.optString("name","defaultValue"); String content = jsonObject.optString("content","defaultValue"); String published = jsonObject.optString("published","defaultValue"); list.add(new CommentsLibrary(name, content, published)); } </code></pre> <p>Then initialize listview</p> <pre><code>ListView lv =(ListView)findViewById(R.id.list); CustomAdapter cus = new CustomAdapter(ActivityName.this,list); lv.setAdapter(cus); </code></pre> <p>Define a custom layout with 3 textviews. (list_item.xml)</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="31dp" android:text="TextView" /&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_centerHorizontal="true" android:text="TextView" /&gt; &lt;TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:layout_marginRight="24dp" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Then define a custom adapter by extending a base adapter. Override <code>getView</code> inflate the custom layout and set text views there. </p> <pre><code>public class CustomAdapter extends BaseAdapter { LayoutInfalter mInflater; ArrayList&lt;CommentsLibrary&gt; list; public CustomAdapter(Context context,ArrayList&lt;CommentsLibrary&gt; list) { mInflater = LayoutInfalter.from(context); this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if(convertView==null) { convertView =minflater.inflate(R.layout.list_item, parent,false); holder = new ViewHolder(); holder.tv1 = (TextView) convertView.findViewById(R.id.textView1); holder.tv2 = (TextView) convertView.findViewById(R.id.textView2); holder.tv3 = (TextView) convertView.findViewById(R.id.textView3); convertView.setTag(holder); } else { holder= (ViewHolder) convertView.getTag(); } holder.tv1.setText(list.get(position).getName()); holder.tv2.setText(list.get(position).getContent()); holder.tv3.setText(list.get(position).getPublished()); return convertView; } static class ViewHolder { TextView tv1,tv2,tv3 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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