0

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:

     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);

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:

 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++;
        }

And the onClickListener is this:

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;
        }
    }
}

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.

The card ID is a int in the MyCard object.

If anyone could help me it would be greatly appreciated.

Edit: The MyCard object is a object which needs this libary: http://nadavfima.com/cardsui-view-library/

Marc
  • 1,094
  • 3
  • 17
  • 38
  • print the value of view.getId() inside OnClickListener (worst name ever - imho) and check if really one of your view has id 1 – Blackbelt May 20 '13 at 14:01
  • @blackbelt the output is 05-20 16:03:39.808: I/System.out(8368): view.getId() -->> 2131427341 But that doesn't seem to help me because all of the objects which were made in the while loop have that ID... – Marc May 20 '13 at 14:07
  • is MyCard a view? If it so call a.setId(id); inside your loop – Blackbelt May 20 '13 at 14:09
  • @blackbelt no it's not a view, its a object which is going in a view. (Sort of listview, but then with a twist). – Marc May 20 '13 at 14:14
  • if you do a.setOnClickListener you probably can do a.setId. Post MyCard as well – Blackbelt May 20 '13 at 14:15
  • @blackbelt No I can't. The MyCard object does have a ID which I give through the new MyCard constructor (as shown). But I don't know how to get that ID. – Marc May 20 '13 at 14:17

1 Answers1

0

That is because your view.getId() is never equals 1. Try adding

System.out.println("view.getId()  -->>  "+view.getId());

inside the default case..

You will get your problem then and there...

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • Thanks, this was the output: 05-20 16:03:39.808: I/System.out(8368): view.getId() -->> 2131427341. But with this I can't set specific actions for the objects, because all the ID's are that number, there's no difference. – Marc May 20 '13 at 14:04
  • [check out this link](http://stackoverflow.com/a/8937477/2345913) . what you have to do is set id's for each cards dynamically. Then in OnClickListener utilise these id's – CRUSADER May 20 '13 at 14:10
  • Okay, but when I do that I have to set ID's before the items are created. Sometimes it could be 2 objects, sometimes more than 100... So I don't think I can do that in a xml file. – Marc May 20 '13 at 14:16
  • What I meant is set id's dynamically not in xml file, check out the second code snippet in link provided above – CRUSADER May 20 '13 at 14:17
  • I checked it out again, but I can't do a.setId(); the method isn't there. (Or I would have to create that method myself but that wouldn't help anything (I think)). However the MyCard object has a ID (which I set by making that object), but I don't know how to get that ID. – Marc May 20 '13 at 14:20
  • Well, in that case, try saving those id's generated while creating object in some arrays, or vector or anything else along with position number.. Then use it with comparison in your onClickListener. I know this is not the rigid way to do but, lets give this a try.. – CRUSADER May 20 '13 at 14:29
  • I did save them in a array, but when I click on a card I don't know which I clicked, so I don't know how to do it with a comparison. – Marc May 20 '13 at 14:51
  • I said, save in array with id and position. Like take two dimensional array say int id[i][j]. In here, id[0][0] = { 0 , 12345 }.. where 0 == position and 12345 == your card id,, then in onClick check id and get corresponding position – CRUSADER May 20 '13 at 16:18