Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks to me as if you the variables tv and myButton are class members. If that is the case they will be overwritten at each step of the for-loop. This means that once the on click listener triggers it will refer to the most recently set values for tv and myButton (ie the last step of the for-loop).</p> <p>Change tv and myButton into variables within the method scope and either combine createButton() and createTextView() into a single method or let createTextView() return the created TextView and pass that to createButton()</p> <p>I've made some changes to your code:</p> <ul> <li>Moved the Button and TextView declarations inside the for-loop</li> <li>Changed the onClick listener to use the local TextView reference.</li> </ul> <p>PS I'm not sure why you called v.isPressed() inside onClick(). It does nothing.</p> <pre><code>protected void onPostExecute(String result) { try { JSONArray jsonArray = new JSONArray(result); Log.i("JSON", "Number of surveys in feed: " + jsonArray.length()); LinearLayout news = (LinearLayout)findViewById(id.hello); //---print out the content of the json feed--- for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); final Button button = new Button(getApplicationContext()); button.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); button.setId(i); button.setText(jsonObject.getString("title")); final TextView tv = new TextView(getApplicationContext()); tv.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); tv.setId(i); tv.setText(jsonObject.getString("text")); news.addView(button); news.addView(tv); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tv.setVisibility(tv.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); } }); Toast.makeText(getBaseContext(), jsonObject.getString("title") + " - " + jsonObject.getString("text"), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } } </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