Note that there are some explanatory texts on larger screens.

plurals
  1. POOnClickListener in a while loop on custom objects
    primarykey
    data
    text
    <p>I'm setting up a CardView layout (with the libary) but I've got a problem. I can setup a onClickListener (which works) in this way:</p> <pre><code> mCardView = (CardUI) getView().findViewById(R.id.cardsview); mCardView.setSwipeable(true); MyCard b = new MyCard("Hi", "Hi", 15); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println(v.toString()); Toast.makeText(getActivity(),"werkt",Toast.LENGTH_LONG).show(); } }); mCardView.addCard(b); </code></pre> <p>But when I try to do it in a loop I can't seem to get the ID of the cards. My cards do have a unique ID (just a int as ID). I'm adding them like this:</p> <pre><code> int id = 0; while(!c.isAfterLast()){ String description = ""; System.out.println("Opmerking: " + c.getString(3)); if(!c.getString(3).equals("")){ description = "Opmerkingen: " + c.getString(3); } if(recreate){ MyCard a = new MyCard(c.getString(2)+" "+c.getString(1), description, id); cards.add(a); mCardView.addCard(a); a.setOnClickListener(new OnClickListener()); } c.moveToNext(); id++; } </code></pre> <p>And the onClickListener is this:</p> <pre><code>private class OnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()){ case 1: Toast.makeText(getActivity(), "werkt", Toast.LENGTH_LONG).show(); break; default: Toast.makeText(getActivity(), "werkt niet", Toast.LENGTH_LONG).show(); break; } } } </code></pre> <p>But everytime when I click one which has a onClickListener of the while loop I get 'werkt niet' which is the default item of the switch.</p> <p>The card ID is a int in the MyCard object.</p> <p>If anyone could help me it would be greatly appreciated.</p> <p>Edit: The MyCard object is a object which needs this libary: <a href="http://nadavfima.com/cardsui-view-library/" rel="nofollow">http://nadavfima.com/cardsui-view-library/</a></p>
    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.
 

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