Mukhlis Saputro
Mukhlis Saputro

Reputation: 189

Set OnClickListener For Each Table Row

I have source code that use json to get data from server and show it dynamically in table. I want to make every data in row table clickable. And When data clicked, It will show new activity that contain detail of data. This is my code :

for (Iterator i = penyakit.iterator(); i.hasNext();) {

        Penyakit p = (Penyakit) i.next();

        /** Create a TableRow dynamically **/
        tr = new TableRow(this);

        /** Creating a TextView to add to the row **/
        String nomor = String.valueOf(no);
        isikolom1 = new TextView(this);
        isikolom1.setText(nomor);
        isikolom1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        isikolom1.setPadding(10, 10, 10, 10);
        isikolom1.setGravity(Gravity.CENTER);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 2, 2, 0);
        Ll.addView(isikolom1, params);
        tr.addView((View) Ll); // Adding textView to tablerow.

        TextView isikolom2 = new TextView(this);
        isikolom2.setText(p.getNama());
        isikolom2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        isikolom2.setPadding(10, 10, 10, 10);
        isikolom2.setGravity(Gravity.CENTER);

        idp = p.getId();
        isikolom2.setClickable(true);
        isikolom2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent in = new Intent(getApplicationContext(),
                        detail_penyakit.class);
                // sending pid to next activity
                in.putExtra(TAG_id, idp);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });

        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 2, 2, 2);

        Ll.addView(isikolom2, params);
        tr.addView((View) Ll); // Adding textview to tablerow.

        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        no = no + 1;
    }

When I click TableRow it show new activity with detail of LAST Data. Not detail from data in Table Row That I Clicked. How can I show detail of data in Table Row That I Clicked? Thank You For suggest.

Upvotes: 0

Views: 420

Answers (1)

Deepak John
Deepak John

Reputation: 967

the variable idp you use is defined outside the iterator so, after executing the iterator, the value of idp will be id of last vlaue of penyakit. So create local variable idp inside the iterator

Upvotes: 1

Related Questions