当我使用startActivityForResult时,我没有得到结果?

时间:2015-02-04 09:49:41

标签: android onactivityresult

我在第一个活动中尝试了以下代码

Intent in=new Intent(getApplicationContext(),ClaimintimationFilesList.class);
            startActivityForResult(in, 1);

这是我在第二个活动中使用的代码

package com.assuretech.ku.claimintimation;


import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.assuretech.ku.R;


public class ClaimintimationFilesList extends ListActivity {

private String path;


 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.files_list_browse);

    path = "/";
    if (getIntent().hasExtra("path")) {
      path = getIntent().getStringExtra("path");
    }

    setTitle(path);



    // Read all files sorted into the values-array
    List values = new ArrayList();
    File dir = new File(path);
    if (!dir.canRead()) {
      setTitle(getTitle() + " (inaccessible)");
    }
    String[] list = dir.list();
    if (list != null) {
      for (String file : list) {
        if (!file.startsWith(".")) {
          values.add(file);
        }
      }
    }
    Collections.sort(values);

    // Put the data into the list
    ArrayAdapter adapter = new ArrayAdapter(this,
        android.R.layout.simple_list_item_2, android.R.id.text1, values);
    setListAdapter(adapter);
  }

 @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    String filename = (String) getListAdapter().getItem(position);
    if (path.endsWith(File.separator)) {
      filename = path + filename;
    } else {
      filename = path + File.separator + filename;
    }
    if (new File(filename).isDirectory()) {
      Intent intent = new Intent(this, ClaimintimationFilesList.class);
      intent.putExtra("path", filename);
      startActivity(intent);
    } else {



        Intent returnIntent = getIntent();
        returnIntent.putExtra("path",filename);
        System.out.println("Filename in list.."+filename);
        setResult(RESULT_OK,returnIntent);
        finish();


        //Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
         /*Intent intent = new Intent(this, Reportclaim.class);
          intent.putExtra("path", filename);
          startActivity(intent);*/
        /*System.out.println("Filename.."+filename);
        Reportclaim obj=new Reportclaim();
          obj.policy_rep.setText(filename);*/
    }

    this.finish();

  }



}

但是根据活动结果,我没有打造文件名

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
       // if(resultCode == RESULT_OK){
         if(resultCode != RESULT_CANCELED){
             filename=data.getStringExtra("path");
             System.out.println("Filename.."+filename);
             policy_rep.setText(filename);
         }
        //}
       /* if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
            System.out.println("Noresult....");
        }*/
    }

当我尝试使用注释代码时,我在logcat中打印了Noresult。

2 个答案:

答案 0 :(得分:1)

我刚刚复制了你的代码并检查了..它为我工作。 在发送之前确保文件名变量不为空且为空。

公共类MainActivity扩展了ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(getApplicationContext(),SecondActivity.class);
            startActivityForResult(in, 1);
        }
    });


}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
       // if(resultCode == RESULT_OK){
         if(resultCode != RESULT_CANCELED){
            String filename=data.getStringExtra("path");
             System.out.println("Filename.."+filename);
           //  policy_rep.setText(filename);
         }
        //}
       /* if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
            System.out.println("Noresult....");
        }*/
    }


}

公共类SecondActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Intent returnIntent = new Intent();
    returnIntent.putExtra("path","arjun");
    System.out.println("Filename in list.."+"arjun");
    setResult(RESULT_OK,returnIntent);
    finish();
}

}

答案 1 :(得分:0)

尝试使用以下代码

private String path;
private List values;
private  ArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.files_list_browse);

path = "/";
if (getIntent().hasExtra("path")) {
  path = getIntent().getStringExtra("path");
}

setTitle(path);



// Read all files sorted into the values-array
values = new ArrayList();
setData(path);
adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_2, android.R.id.text1,    values);
setListAdapter(adapter); 
}
private void setData(String path){
    values.clear();
     File dir = new File(path);
        if (!dir.canRead()) {
          setTitle(getTitle() + " (inaccessible)");
        }
        String[] list = dir.list();
        if (list != null) {
          for (String file : list) {
            if (!file.startsWith(".")) {
              values.add(file);
            }
          }
        }
        Collections.sort(values);

        // Put the data into the list


}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String filename = (String) getListAdapter().getItem(position);
if (path.endsWith(File.separator)) {
  filename = path + filename;
} else {
  filename = path + File.separator + filename;
}
if (new File(filename).isDirectory()) {
 setData(filename);
 adapter.notifyDataSetChanged();
} else {



    Intent returnIntent = getIntent();
    returnIntent.putExtra("path",filename);
    System.out.println("Filename in list.."+filename);
    setResult(RESULT_OK,returnIntent);
    finish();


    //Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
     /*Intent intent = new Intent(this, Reportclaim.class);
      intent.putExtra("path", filename);
      startActivity(intent);*/
    /*System.out.println("Filename.."+filename);
    Reportclaim obj=new Reportclaim();
      obj.policy_rep.setText(filename);*/
}

this.finish();

}