Android在列表视图选择时调用onActivityResult

时间:2015-02-01 00:45:34

标签: java android listview onactivityresult

当我尝试从列表视图中获取所选行以在onClickListener按钮上执行某些操作时,我遇到了一些问题。

基本上每个listview行都有一个按钮。选择该按钮后,它将导航到QR码扫描意图。当结果在onActivityResult()返回时,我想对该行做一些事情。这就是我设置按钮监听器的方式:

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

这是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) {
        }
    }
}

我已经在onActivityResult中创建了一个if else语句,但我不知道如何先获取所选行。

任何指南?

提前致谢。

更新

这是llist视图行项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>

所以基本上我每行都有一张注册图片。然后在if语句中,让我们说conditons是匹配的,我该如何注册该行?

2 个答案:

答案 0 :(得分:0)

你的名单有多长?如果它不包含太多项,则可以在请求代码上使用switch语句。在Adapter中,您可以将位置作为请求代码传递给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);
            }
        });

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

}

有点黑客,但它应该有用。

答案 1 :(得分:0)

对于列表的每一行,您都可以设置标记。您必须在xml按钮中设置android:onClick,并在定义处理程序时将标记作为对象。 定义一个对象TextView,您将使用视图V中的getTag()保存标记。

TextView tv = v.getTag();

抱歉我的英文。无论如何,这个网址可以帮助你What is the main purpose of setTag() getTag() methods of View?