Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show JSON value in TextView and ImageView
    primarykey
    data
    text
    <p>How to pass JSON value in imageview and textview? </p> <p>My code below contains three ImageView's (<code>img11,img2,img3</code>) and TextView's (<code>txt1,txt2,txt3</code>). I want to show my JSONArray, which contains name and url, in those views. In my code there is no ListView. How do I show my JSON values in my TextView and ImageView? The http connection is OK. The problem is in parsing the JSON array. </p> <p><strong>JSON:</strong></p> <pre><code> /////////////// this is my json file//////////// { "worldpopulation": [ { "rank":1, "name": "BREAKFAST", "url": "http://niel986.files.wordpress.com/2012/07/fast-food.jpg" }, { "rank":2, "name": "LUNCH ", "url": "http://www.bubblews.com/assets/images/news/1107772406_1370520219.gif" }, { "rank":3, "name": "SUPPER", "url": "http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ /s1600/chicken-biryani.jpg" } ] } </code></pre> <p><strong>Code:</strong></p> <pre><code> ImageView img1 = (ImageView) findViewById(R.id.img1); TextView txt1 = (TextView) findViewById(R.id.txt1); ImageView img2 = (ImageView) findViewById(R.id.img2); TextView txt2 = (TextView) findViewById(R.id.txt2); ImageView img3 = (ImageView) findViewById(R.id.img3); TextView txt3 = (TextView) findViewById(R.id.txt3); MenuSelect = Utils.MenuSelect; try { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); HttpConnectionParams.setSoTimeout(client.getParams(), 15000); HttpUriRequest request = new HttpGet(MenuSelect); HttpResponse response = client.execute(request); InputStream atomInputStream = response.getEntity().getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream)); String line; String str = ""; while ((line = in.readLine()) != null) { str += line; } JSONObject json = new JSONObject(str); JSONArray data = json.getJSONArray("worldpopulation"); String Name1 = data.getString("name"); String url1 = person.getString("url"); txt1.setText(Name1); Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL( url1).getContent()); img1.setImageBitmap(bitmap); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block // IOConnect = 1; e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p><strong>XML</strong></p> <pre><code>&lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" &gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/img1" android:layout_width="70dp" android:layout_height="80dp" android:contentDescription="@null" android:background="@drawable/imagebgborder" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/txt1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:textStyle="bold" android:contentDescription="@null" android:text="@string/hello_world" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/img2" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/txt2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:contentDescription="@null" android:text="@string/hello_world" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/img3" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/txt3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:contentDescription="@null" android:text="@string/hello_world" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </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