2015-02-04 1 views
0

나는이 내가 두 번째 활동에 사용되는 코드는startActivityForResult를 사용할 때 결과가 다시 표시되지 않습니까?

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...."); 
     }*/ 
    } 
을 geeting하지 오전

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

첫 번째 활동에 코드 아래 시도

내가 논평 한 코드를 시도했을 때 Noresult가 logcat에 인쇄되어있다.

+0

두 번째 활동 코드에'finish();'를 추가 했습니까? –

+0

예. finish()를 추가했습니다. 끝 부분에서 – zyonneo

+0

setResult를 설정 한 전체 방법을 게시하십시오. – Ramesh

답변

1

방금 ​​코드를 복사하고 확인했습니다. 나를 위해 일합니다.

공용 클래스 MainActivity가 ActionBarActivity를 확장 ... 확인 파일 이름 변수가 null 및 전송하기 전에 비어 있지 않은지 확인 {

@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는 활동 {

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

}

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

} 
관련 문제