Android OnActivityResult将数据传递给另一个表单并从中获取数组结果

时间:2014-07-29 10:07:42

标签: android onactivityresult

我想将一些数据发送到另一个活动,说SecondActivity,然后使用OnActivityResult从The SecondActivity获取Array数据到主要活动

这是一个应用程序,就像当我按上传按钮我已经有路径我想将该路径发送到另一个活动,然后将数组中的结果从另一个活动到相同的主动,然后想要在textview中查看该数组

CSVUpload

public class CSVUploader extends Activity {
    Button btnUpload;
    EditText txtName;
    EditText txtMessageName;
    Bundle extras = getIntent().getExtras();
    String FullPath = extras.getString("FullPath");
    @Override
    protected void onCreate(Bundle savedInstanceState) {

    try{
    File csvfile = new File(FullPath);
    FileInputStream csvStream = new FileInputStream(csvfile);
    BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
    String line;
    String[] name = null;
    String[] number = null;
    int iCount=0;
            while ((line = in.readLine()) != null){
                String[] RowData = line.split(",");
                name[iCount] = RowData[0];
                number[iCount] = RowData[1];
                iCount++;
/*                  ContentValues values = new ContentValues();
                values.put(key, value);
                values.put(CsvProvider.NUMBER, number);
                values.put("status",status);
                getContentResolver().insert(CsvProvider.CONTENT_URI, values);
*/              }
            in.close();
            Bundle b =new Bundle();
            Intent intent = new Intent();
            b.putStringArray("name", name);
            b.putStringArray("number", number);
            intent.putExtras(b);
            setResult(RESULT_OK, intent);
            finish();


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }

}

FirstActivity

  public void uploadfile(View view){
        edittext = (EditText)findViewById(R.id.txtFile);
        Toast.makeText(NewMessage.this, FullPath, 2000).show();
        if(FullPath != null)
        {
        Intent intent1 = new Intent(this, CSVUploader.class);
        intent1.putExtra("FullPath", FullPath);
        startActivityForResult(intent1, 2);     
        }
        else
        {
            Toast.makeText(NewMessage.this, "No File Selected", 2000).show();
        }
    }

 // Listen for results.
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        // See which child activity is calling us back.
        super.onActivityResult(requestCode, resultCode, data);


        if (requestCode == REQUEST_PATH){
                if (resultCode == RESULT_OK) { 
                    curPathName = data.getStringExtra("GetPath"); 
                    curFileName = data.getStringExtra("GetFileName");
                    FullPath = curPathName+"/"+curFileName;
                    edittext.setText(curFileName);
    /*              Toast.makeText(NewMessage.this, resId, duration);*/
                }
             }

        if (requestCode==2){
            if (resultCode == RESULT_OK) {
                Bundle b=this.getIntent().getExtras();
                String[] name=b.getStringArray("name");
                String[] number=b.getStringArray("number");
                String[] status;
            }
        }

2 个答案:

答案 0 :(得分:0)

尝试使用此代码。我不确定这是否会正常工作

<强> MainActivity.java

public void uploadfile(查看视图){

Intent intent1 = new Intent(this, CSVUploader.class);       
intent1.putExtra("FullPath", FullPath);         
startActivity(intent1);

}

同样替换你的代码

<强> CSVUploader.java

   Bundle b=new Bundle();
        Intent i = new Intent(this,MainActivity.class);
        b.putStringArray("name",name); 
        b.putStringArray("number",number) 
         i.putExtras(b);
         startActivity(i);

您可以在MainActivity.java中将其作为

进行检索
 Bundle b=this.getIntent().getExtras();
 String[] name=b.getStringArray("name");
  String[] number=b.getStringArray("number");

答案 1 :(得分:0)

您需要将检索代码放入onCreate而不是现在的位置。

 @Override
    protected void onCreate(Bundle savedInstanceState) {     
      String FullPath = getIntent().getStringExtra("FullPath");

顺便说一下,为什么要使用其他活动来完成任务。为什么不在同一活动中这样做。