Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to click a dynamic button in a ListView
    primarykey
    data
    text
    <p>I hava an Activity, a ListView, and an ArrayAdapter.<br> In the ArrayAdapter, there are a TextView(I name it "title") and serval buttons created dynamically with different texts.<br> I want to do is when I click the button, I want to show the text on the button also the text on the "title".<br> The challenge is that when I do button.setOnClickListener in the Adapter, I dont know how to get the title.<br> On the other hand, If I do list.setOnItemClickListener, it doesn't work when I click the button. Also, because the button is dynamic, I dont know how to show it.<br> Thanks!<br> <BR> here is my getView:<br></p> <pre><code> @Override public View getView(int position, View convertView, ViewGroup parent) { Map&lt;String, String&gt; item = listitem.get(position); View view = inflater.inflate(resId, parent, false); // get views final TextView title = (TextView) view.findViewById(R.id.title); TextView type = (TextView) view.findViewById(R.id.type); final TextView updateDate = (TextView) view.findViewById(R.id.updateDate); TableLayout stLayout = (TableLayout) view .findViewById(R.id.showtimes); // fo title.setFocusable(false); updateDate.setFocusable(false); // set views String[] array = DatabaseHelper.MOVIE_SHOWTIMES; for (int i = 0; i &lt; array.length; i++) { String datum = item.get(array[i]); if (datum != null) { switch (i) { case 0: title.setText(datum); break; case 1: String types = MovieHelper.convertMovieTypes(datum); type.setText(types); break; case 2: try { Date date = new Date(datum); String formattedDate = DateFormatHelper.format( date, DateFormatHelper.DATE); updateDate.setText(formattedDate); } catch (ParseException e) { Log.e(TAG, e.toString()); } break; case 3: try { JSONArray times = new JSONArray(datum); TableRow tableRow = new TableRow(context); tableRow.setGravity(Gravity.CENTER_HORIZONTAL); int record = 0; for (int x = 0; x &lt; times.length(); x++) { String time = times.getString(x); Date convertedTime = new Date(time); try { String formattedTime = DateFormatHelper .format(convertedTime, DateFormatHelper.TIME); Button timeButton = new Button(context); timeButton.setId(buttonCount); timeButton.setText(formattedTime); tableRow.addView(timeButton); buttonCount++; record++; timeButton.setFocusable(false); timeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.getParent(); Toast.makeText(getApplicationContext(), ((Button)v).getText(), Toast.LENGTH_SHORT).show(); } }); } catch (ParseException e) { Log.e(TAG, e.toString()); } if (record % 3 == 0 &amp;&amp; record != 0) { stLayout.addView(tableRow); tableRow = new TableRow(context); tableRow.setGravity(Gravity.CENTER_HORIZONTAL); } } if (record % 3 != 0 &amp;&amp; record != 0) { stLayout.addView(tableRow); } } catch (JSONException e) { Log.e(TAG, e.toString()); } break; } } } return view; } } </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