Android onActivityResult设置选定的行

时间:2015-01-31 13:24:25

标签: java android android-activity

我在尝试从弹出窗口中的列表视图中检测所选行时遇到了一些问题。这是我点击行的部分,然后打开另一个意图:

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

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

        return convertView;
    }
}

然后当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(contents.equals("QRCode1")){

            }else{
                Toast.makeText(context, "QR code does not match. Please make sure you attended the correct event.",
                        Toast.LENGTH_LONG).show();
            }

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

如果内容是QRCode1,我想对第一行做一些事情。如果它是QRCode2,那么我想做第二行的事情。

但是,我不知道应该如何获得行号。我应该从onClick传递它吗?

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

为什么不在活动/片段中的ListView上使用setOnItemClickListener

你会有一个方法onItemClick并获取所点击的项目的位置