0

I was having some problem when I try to get the selected row from list view to do something upon the button onClickListener.

Basically I have a button for each listview row. When the button is selected, it will navigate to QR code scan intent. When the result returned at the onActivityResult(), I wanted to do something with that row. This is how I set up the button listener:

public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.attendee_listview_row,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_dName = (TextView) convertView
                    .findViewById(R.id.txtDisplayName);
                            viewHolder.btn_scan = (ImageView) convertView.findViewById(R.id.btnScan);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dName.setText(attendeeList.get(position)
                .getAccountName().trim());

        viewHolder.btn_scan.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, 0);
            }
        });

And here is the onActivityResult():

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == Activity.RESULT_OK) {

            String contents = intent.getStringExtra("SCAN_RESULT");
            if(attendeeName.equals("Andrew") && contents.equals("QRCode1")){

            }else{
                Toast.makeText(context, "Event QR code does not match.",
                        Toast.LENGTH_LONG).show();
            }

        } else if (resultCode == Activity.RESULT_CANCELED) {
        }
    }
}

I already made an if else statement inside the onActivityResult but I not sure how to get the selected row previously.

Any guides?

Thanks in advance.

UPDATES

Here is the llist view row item xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="1" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/btnScan"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:src="@drawable/qr_icon" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtDisplayName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/lightred"
        android:textSize="16dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ivRegisteredTag"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:paddingTop="15dp"
        anddroid:visibility="gone"
        android:src="@drawable/registered" />
</LinearLayout>

</LinearLayout>

So basically I have a registered image for each row. Then inside the if statement, let's say the conditons are matched, how do I should the registered image for that row?

QWERTY
  • 2,303
  • 9
  • 44
  • 85

2 Answers2

0

How long is your list? If it doesn't contain too many items, you could use a switch statement on the request code. In your Adapter you can pass the position as the request code to startActivityForResult();:

public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.attendee_listview_row,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_dName = (TextView) convertView
                    .findViewById(R.id.txtDisplayName);
                            viewHolder.btn_scan = (ImageView) convertView.findViewById(R.id.btnScan);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dName.setText(attendeeList.get(position)
                .getAccountName().trim());

        viewHolder.btn_scan.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, position);
            }
        });

and in onActivityResult():

public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {

    switch(requestCode){
        case 0:
        // == first list item
            if (resultCode == Activity.RESULT_OK) {

                String contents = intent.getStringExtra("SCAN_RESULT");
                if(attendeeName.equals("Andrew") && contents.equals("QRCode1")){

                }else{
                    Toast.makeText(context, "Event QR code does not match.",
                            Toast.LENGTH_LONG).show();
                }

            } else if (resultCode == Activity.RESULT_CANCELED) {
            }
            break;
        case 1:
        // = second list item
        .
        .
        .
        default:
            break;
    }   

}

Kind of a hack, but it should work.

JensJensen
  • 1,017
  • 1
  • 12
  • 25
  • It shouldn't be too long cause my current data set still not much data. Would you mind to provide me some example? – QWERTY Feb 01 '15 at 00:52
  • Let's say case 0 and I wanted to highlight the background of the row. Am I still able to do that? – QWERTY Feb 01 '15 at 00:59
  • Yes, that should be no problem. You just have to clear your lists adapter and fill you list again, after you've highlighted the item 0 in your list. – JensJensen Feb 01 '15 at 01:03
  • Sorry but would you mind to provide me a solution? Cause from what I've found, they used the convertView inside the getView() to highlight the row. But I not sure how I do this from onActivityResult() – QWERTY Feb 01 '15 at 01:05
  • I'm sorry, but I don't know what your lists data looks like and where it comes from. You could use a custom class for you data with a `boolean isHighlighted`. In `onActivityResult();` you can set the boolean to true and update your list. But this is just one of many ways how you could achieve this. – JensJensen Feb 01 '15 at 01:11
  • That update doesn't really help. Or are you asking how to highlight you layout? – JensJensen Feb 01 '15 at 10:31
0

For each row of the list you sets the tag. You have to set android:onClick in your xml buttons and when you define the handlers you can take the tags as objects. Define one object TextView where you will save the tag using getTag() from the view V.

TextView tv = v.getTag();

Sorry for my english. Anyway this url may helps you What is the main purpose of setTag() getTag() methods of View?

Community
  • 1
  • 1
Asier
  • 461
  • 9
  • 19
  • 1
    Sorry but I do not get it. Would you mind for futher explanation or provide me with some code example? – QWERTY Feb 01 '15 at 01:02
  • This example helped me to learn how to use atributes from each item of the listviews [link]https://looksok.wordpress.com/tag/listview-item-with-button/ – Asier Feb 01 '15 at 01:07