android gridview子项目启动活动onclick

时间:2017-03-21 14:52:39

标签: java android

protected function pictureUpload()
{
    if (!($field_ids = $this->product->getCustomizationFieldIds()))
        return false;
    $authorized_file_fields = array();
    foreach ($field_ids AS $field_id)
    {
        if ($field_id['type'] == Product::CUSTOMIZE_FILE)
            $authorized_file_fields[(int)$field_id['id_customization_field']] = 'file' . (int)$field_id['id_customization_field'];
    }

    $indexes = array_flip($authorized_file_fields);
    foreach ($_FILES AS $field_name => $file)
    {
        if (in_array($field_name, $authorized_file_fields) AND isset($file['tmp_name']) AND !empty($file['tmp_name']))
        {
            // If there is an upload error, let the parent handle it
            if ($file['error'] != UPLOAD_ERR_OK)
                continue;

            // If the file is not allowed, let the parent handle it
            if (!$this->isUploadTypeAllowed($file))
                continue;

            // Unset the PDF to prevent the parent to handle this file
            unset($_FILES[$field_name]);

            // Create dir
            mkdir(_PS_UPLOAD_DIR_ . ProductController::CUSTOMIZATION_FILE_DIR.'/'.$this->context->cart->id, 0777, true);

            // Mark the file as a custom upload
            $file_name = ProductController::CUSTOMIZATION_FILE_DIR.'/'.$this->context->cart->id.'/P'. md5(uniqid(rand(), true)).'.pdf';

            $doc = exec("identify -format %n $file_name");

            var_dump($doc);

            $tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
            if (!move_uploaded_file($file['tmp_name'], $tmp_name))
            {
                $this->errors[] = Tools::displayError('An error occurred during the PDF upload.');
                return false;
            }
            // Copy file to the upload dir
            if (!copy($tmp_name, _PS_UPLOAD_DIR_.$file_name))
            {
                $this->errors[] = Tools::displayError('An error occurred during the PDF upload.');
                return false;
            }
            // Chmod the new file
            if (!chmod(_PS_UPLOAD_DIR_.$file_name, 0777))
            {
                $this->errors[] = Tools::displayError('An error occurred during the PDF upload.');
                return false;
            }

            // Create a fake thumb to avoid error on delete, this hack avoids lots of core method override
            file_put_contents(_PS_UPLOAD_DIR_ . $file_name . '_small', '');
            chmod(_PS_UPLOAD_DIR_ . $file_name . '_small', 0777);

            // Register the file
            $this->context->cart->addPictureToProduct($this->product->id, $indexes[$field_name], Product::CUSTOMIZE_FILE, $file_name);

            // Remove tmp file
            unlink($tmp_name);
        }
    }
    return parent::pictureUpload();
}

protected function isUploadTypeAllowed($file)
{
    /* Detect mime content type */
    $mime_type = false;
    $types = array('application/pdf', 'application/zip'); // Extra mime types can be added here

    if (function_exists('finfo_open'))
    {
        $finfo = finfo_open(FILEINFO_MIME);
        $mime_type = finfo_file($finfo, $file['tmp_name']);
        finfo_close($finfo);
    }
    elseif (function_exists('mime_content_type'))
    {
        $mime_type = mime_content_type($file['tmp_name']);
    }
    elseif (function_exists('exec'))
    {
        $mime_type = trim(exec('file -b --mime-type '.escapeshellarg($file['tmp_name'])));
    }
    if (empty($mime_type) || $mime_type == 'regular file')
    {
        $mime_type = $file['type'];
    }

    if (($pos = strpos($mime_type, ';')) !== false)
        $mime_type = substr($mime_type, 0, $pos);
    // is it allowed?
    return $mime_type && in_array($mime_type, $types);
}

我知道这个方法但是比方说我有20个子项,所以我需要20个活动!我如何制作它所以只传递了一个活动,但它内部的数据根据​​点击的子项更改(通过里面的数据我的意思是简单的Textviews)抱歉我的英语非常糟糕

1 个答案:

答案 0 :(得分:1)

您可以为活动意图设置一个额外内容,以识别您的来源:

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent i = new Intent(this, second.class);
    i.putExtra("position", position);
    startActivity(i);
}

然后在你的活动中你可以得到这样的意图:

int position;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity); //if your activities have different layouts depending the given position, you can move this line inside the switch function
    ...
    if(getIntent().getExtras() != null) {
        position = getIntent().getExtras().getInt("position", 0);
    }
    switch(position){
        case 1:
            //do something
            break;
        case 2:
            //do another thing
            break;
        default:
            //default behaviour
            break;
    }
}