我如何在ArrayAdapter类的getView方法中使用startActivityForResult?

时间:2012-05-04 10:34:10

标签: java android android-intent

我想使用startActivityForResult从ArrayAdapter类调用另一个intent但我不能这样做。这是我的代码。

ArrayAdapter类构造函数

public QsShareManagerAdapter(Context context, String[] values, Long[] size,String[] time,
        boolean isRoot, Boolean[] attrdir, ArrayList<Boolean> selected,boolean type,
        LinearLayout linearLayout,String SHARE,String qsUsername) 
{
    super(context, R.layout.listlayout, values);
    this.context = context;
    this.values = values;
    this.size = size;
    this.time=time;
    this.isRoot = isRoot;
    this.attrdir = attrdir;
    this.selected = selected;
    this.type=type;
    this.linearLayout=linearLayout;
    this.SHARE=SHARE;
    this.qsUsername=qsUsername;
}





    @Override
public View getView(final int position, View convertView, ViewGroup parent) 
{
    String spliter = "/";
    tag=SHARE.split(spliter);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.listlayout, parent, false);

    //
    addFiles=(Button)linearLayout.findViewById(R.id.add_files);
    manageUser=(Button)linearLayout.findViewById(R.id.manage_users);
    deleteSharedFolder=(Button)linearLayout.findViewById(R.id.delete_shared_folder);
    copyFiles=(Button)linearLayout.findViewById(R.id.copy);
    deleteFiles=(Button)linearLayout.findViewById(R.id.delete_files);

    addFiles.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              Intent intent=new Intent(context,QsFileSelector.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  ;
              intent.putExtra(QsFileSelector.SELECTED_FILES,existing_seletedFile);
              intent.putExtra(QsFileSelector.FILE_OR_DIRECTORY, tag[6]);
              startActivityForResult(intent); 
            Toast.makeText(context, "Add Files", Toast.LENGTH_SHORT).show();
        }
    });

    manageUser.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Manage User", Toast.LENGTH_SHORT).show();              
        }
    });

    deleteSharedFolder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              boolean status=serverURL.folderRequestResponseURI("DELETED_CREATOR", tag[6], qsUsername, tag[5], null, "");
              if(status){
                  Toast.makeText(context, "Delete Share Folder", Toast.LENGTH_SHORT).show();
              }
        }
    });

    copyFiles.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Copy", Toast.LENGTH_SHORT).show();
        }
    });

    deleteFiles.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Delete", Toast.LENGTH_SHORT).show();
        }
    });


    TextView textView = (TextView) rowView.findViewById(R.id.txtview);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView sizeView = (TextView) rowView.findViewById(R.id.size);

    final CheckBox checkbox = (CheckBox) rowView
            .findViewById(R.id.checkbox);

    String s = values[position];

    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        int postion=position;
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
            // TODO Auto-generated method stub
            if(isChecked){
                addToList(values[postion],true);
            }else{
                addToList(values[postion],false);                   
            }
        }
    });
    rowView.setTag(s);
    if (!values[position].contains("my_shared_folder") && isRoot && type) 
    {
        s = values[position] + " 's Shared Folder";
        textView.setText(s);
    } 
    else 
    {
        textView.setText(values[position]);
    }

    if (size.length != 0)
    {
        double sizeinKb = size[position] / 1024;

        if (sizeinKb > 1024) {
            double sizeinMb = sizeinKb / 1024;
            sizeView.setText(Double.toString(round(sizeinMb, 2,
                    BigDecimal.ROUND_HALF_UP)) + " MB | "+time[position]);
        }

        else if (sizeinKb > 1048576) {
            double sizeinGB = sizeinKb / (1024 * 1024);
            sizeView.setText(Double.toString(round(sizeinGB, 2,
                    BigDecimal.ROUND_HALF_UP)) + " GB | "+time[position]);
        }

        else {
            sizeView.setText(Double.toString(round(sizeinKb, 2,
                    BigDecimal.ROUND_HALF_UP)) + " KB | "+time[position]);
        }

    }

    if (attrdir[position]) 
    {
        imageView.setImageResource(R.drawable.icon_folder_logo);
        sizeView.setText("");
    }
    else 
    {
        checkbox.setVisibility(View.VISIBLE);
        if (s.contains(".mp3") || s.contains(".wav")) 
        {
            imageView.setImageResource(R.drawable.audio);
        }
        else if (s.contains(".zip") || s.contains(".rar")
                || s.contains(".tar")) 
        {
            imageView.setImageResource(R.drawable.zip);
        }
        else if (s.contains(".db")) 
        {
            imageView.setImageResource(R.drawable.db_icon);
        }
        else if ((s.contains(".png")) || (s.contains(".jpg"))) 
        {
            imageView.setImageResource(R.drawable.imagview);
        }
        else if ((s.contains(".doc")) || (s.contains(".docx"))) 
        {
            imageView.setImageResource(R.drawable.doc);
        }
        else if ((s.contains(".xls")) || (s.contains(".xlsx"))) 
        {
            imageView.setImageResource(R.drawable.excel);
        }
        else if ((s.contains(".pdf"))) 
        {
            imageView.setImageResource(R.drawable.pdf);
        }
        else 
        {
            imageView.setImageResource(R.drawable.unknown);
        }
    }
    /*
     * for(int i=0;i<size.size();i++){
     * sizeView.setText(Integer.toString(size.get(i))); }
     */     

    if(fileList.contains(values[position]) && checkStatus.contains(true)){
        linearLayout.setVisibility(View.VISIBLE);
        checkbox.setChecked(true);
    }

    if(fileList.size() == 0){
        linearLayout.setVisibility(View.GONE);
    }
    checkbox.setTag(values[position]);
    return rowView;
}

5 个答案:

答案 0 :(得分:8)

使用您拥有的context并将该上下文投射到活动中。像:

((Activity)context).startActivityForResult(intent); 

答案 1 :(得分:5)

addFiles.setFocusable(false);

我认为您应该将Context更改为Activity ...在您的适配器构造函数中更改它...     activity.startActivityForResult(意图,位置);

答案 2 :(得分:0)

为什么不能?你有上下文,使用它。

更新:必须将其强制转换为活动或更改类型。

答案 3 :(得分:0)

使用

getApplicationContext().startActivityForResult(intent);

答案 4 :(得分:0)

要实现这一目标,您需要上下文

  

((Activity)context).startActivityForResult(intent,1);

如果您只需要启动Activity,那么就这样做

  

context.startActivityForResult(intent,1);