简体   繁体   中英

Android call onActivityResult upon listview selection

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?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM