Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing onClick() to Set Text Inside of getView() for BaseAdapter-extended Class
    primarykey
    data
    text
    <p>I'm using one of the classes provided by <a href="https://github.com/openaphid" rel="nofollow noreferrer">openaphid</a> to do a flip view. The problem I'm running into (as I'm just beginning to find out) is that as new views get created and old ones are scrapped (inside <code>getView()</code>), the references to my variables are renewed and, thus, have different IDs, which results in me not being able to change them.</p> <p>Essentially, what I have are 2 <code>TextView</code>s. One of the <code>TextView</code>s (<code>txtvwCharacter</code>) has been set as clickable in the <code>XML</code> and what I'm trying to do is: when <code>txtvwCharacter</code> gets clicked/touched, the other <code>TextView</code> (<code>txtvwComposition</code>) gets toggled between <code>"some text"</code> and <code>""</code> (empty string).</p> <p>Below is a simplified version of my code, as I'm only focusing on the getView() method:</p> <pre><code>public class TravelAdapter extends BaseAdapter { private LayoutInflater inflater; ArrayList&lt;String&gt; characterArray = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; compositionArray = new ArrayList&lt;String&gt;(); TextView txtvwCharacter, txtvwComposition; boolean detailsOn = false; public TravelAdapter(Context context) { inflater = LayoutInflater.from(context); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View layout = convertView; if (convertView == null) layout = inflater.inflate(R.layout.complex1, null); txtvwCharacter = UI.&lt;TextView&gt;findViewById(layout, R.id.txtvw_character); txtvwComposition = UI.&lt;TextView&gt;findViewById(layout, R.id.txtvw_composition); txtvwCharacter.setText(characterArray.get(position)); txtvwComposition.setText(""); txtvwCharacter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(detailsOn == false) { txtvwComposition.setText(compositionArray.get(position)); detailsOn = true; } else { txtvwComposition.setText(""); detailsOn = false; } } }); return layout; } } </code></pre> <p>At the moment, with this code, the <code>onClick</code> does get registered and the <code>txtvwComposition</code> does get set, but it's not the same <code>txtvwComposition</code> that's being displayed, so, visually, it looks like nothing's happening.</p> <p>I'd like to have some sort of persistence here, where when I click on <code>txtvwCharacter</code>, it knows that it's that one <code>TextView</code> object, and not a new one that's been instantiated between all the flip back and forth. (Sorry, I don't know how to phrase that better.)</p> <p>I've looked at <a href="https://stackoverflow.com/questions/11953875/getview-the-constant-change-of-position-of-items-and-i-have-to-click-twice-o">this</a> and <a href="https://stackoverflow.com/questions/15354410/how-to-controll-textview-by-button-onclick-in-getview-method">this</a>, but I don't understand what <code>ViewHolder</code> does, how it knows what to "hold," or how to apply it to my specific code here.</p> <p><strong>CODE EDIT:</strong></p> <pre><code>public class TravelAdapter extends BaseAdapter { private LayoutInflater inflater; ArrayList&lt;String&gt; characterArray = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; compositionArray = new ArrayList&lt;String&gt;(); TextView txtvwCharacter, txtvwComposition; boolean detailsOn = false; public TravelAdapter(Context context) { inflater = LayoutInflater.from(context); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View layout = convertView; if (convertView == null) layout = inflater.inflate(R.layout.complex1, null); txtvwCharacter = UI.&lt;TextView&gt;findViewById(layout, R.id.txtvw_character); txtvwComposition = UI.&lt;TextView&gt;findViewById(layout, R.id.txtvw_composition); if (detailsOn == false) { txtvwCharacter.setText(characterArray.get(position)); txtvwComposition.setText(""); } else { txtvwCharacter.setText(characterArray.get(position)); txtvwComposition.setText(compositionArray.get(position)); } txtvwCharacter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RelativeLayout root = (RelativeLayout) v.getParent(); TextView txtvwComposition = (TextView) root.findViewById(R.id.txtvw_composition); if(detailsOn == false) { txtvwComposition.setText(compositionArray.get(position)); detailsOn = true; } else { txtvwComposition.setText(""); detailsOn = false; } } }); return layout; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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