2017-09-14 27 views
0

當我使用另一個意圖打開一個活動並返回結果時,我遇到了存儲我之前活動的意圖附加項的問題。收集多個意向附加項,同時在返回主活動期間保存結果

邏輯:

  • 活動A:從複選框接收字符串,使用意圖hasExtra傳遞給b活動
  • 活性B:與按鈕活動ç編輯文本字段的列表
  • 活動Ç :使用intent extra來獲取字符串並將其傳遞給activityText中的另一個editTextField

問題在於我一直在失去intentExtra從a到b。

如果我的描述不徹底,我很抱歉,我是Java新手。

活動A

public class PipesInspection extends AppCompatActivity{ 
private static final String TAG = "PipesInspection"; 
ArrayList<String> pipesInspectionSelection = new ArrayList<String>(); 
Button nextButtonToPost; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.residential_pipes_inspection_lv_selected); 
    Log.d(TAG, "onCreate: starting"); 
    initialiseWidgets(); 
} 
public void initialiseWidgets(){ 
    nextButtonToPost = (Button) findViewById(R.id.nextButton); 
    nextButtonToPost.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String final_category_selection = ""; 
      for (String Selections : pipesInspectionSelection){ 
       final_category_selection = final_category_selection + Selections + ", "; 
      } 
      Log.v(TAG,"gotten text: " + final_category_selection); 
      String selectedChoices = pipesInspectionSelected.getText().toString(); 
      Intent pipesInspectionIntent = new Intent(v.getContext(), PostAJobActivity.class); 
      pipesInspectionIntent.putExtra("selectedChoices", selectedChoices); 
      v.getContext().startActivity(pipesInspectionIntent); 
     } 
    }); 
} 
} 

活動B

public class PostAJobActivity extends AppCompatActivity { 
private static final String TAG = "PostAJobActivity"; 
EditText jobTitle, jobDescription, jobLocation; 
String location, title; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_post_a_job); 
    Log.d(TAG, "onCreate: starting"); 
    jobDescription = (EditText)findViewById(R.id.input_job_description); 
    mapsButton = (ImageButton) findViewById(R.id.mapButton); 
    mapsButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(mContext, LaunchMapsActivity.class); 
      startActivity(intent); 
     } 
    }); 
     getIntentExtras(); 
} 
public void getIntentExtras(){ 
    jobLocation = (EditText) findViewById(R.id.location); 
    Intent intentLocation =getIntent(); 
    location= intentLocation.getStringExtra("location"); 
    jobLocation.setText(location); 


    jobTitle = (EditText) findViewById(R.id.title); 
    Intent pipesInspectionIntent = getIntent(); 
    title = pipesInspectionIntent.getStringExtra("selectedChoices"); 
    jobTitle.setText(title); 
} 
} 

活動Ç

public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.PlaceViewHolder> { 

private Context mContext; 
private PlaceBuffer mPlaces; 


public PlaceListAdapter(Context context, PlaceBuffer places) { 
    this.mContext = context; 
    this.mPlaces = places; 
} 

@Override 
public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    // Get the RecyclerView item layout 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    View view = inflater.inflate(R.layout.maps_item_place_card, parent, false); 

    return new PlaceViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final PlaceViewHolder holder, int position) { 
    String placeName = mPlaces.get(position).getName().toString(); 
    String placeAddress = mPlaces.get(position).getAddress().toString(); 
    holder.nameTextView.setText(placeName); 
    holder.addressTextView.setText(placeAddress); 
    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intentLocation = new Intent(v.getContext(), PostAJobActivity.class); 
      intentLocation.putExtra("location",holder.nameTextView.getText().toString()+ 
        ", " + holder.addressTextView.getText().toString()); 
        v.getContext().startActivity(intentLocation); 
     } 
     }); 

} 
+0

您確定pipesInspectionSelected.getText()。toString()具有您想要傳遞的正確值嗎? – roostertech

+0

@roostertech是的它的確如此 –

回答

0

我認爲這種物品可以是有幫助的 https://developer.android.com/training/basics/intents/result.html

我不知道我理解正確的問題......活動的第一個開始,將數據傳遞活動B的活性調用C和獲取數據從它返回到使用從兩個收集的數據b活動活動做些事情 ....如果這是你的問題,你不應該開始活動C的正常方式,你應該使用startActivityforresult()窗體活動B開始活動C 並等待它完成,然後返回到B與收集的數據C ....從活動C開始活動B將創建B的新實例,而不用從A收集數據,同時使用這種方式B的實例與來自A的數據將等待C完成並從中獲取數據除此之外移動getIntentExtras()函數onClick()

+0

活動b是從活動c和a收集信息的主要活動,但流程從活動A開始,您在活動b中建議我應該嘗試使用startActicityForResult而不是StartActivity(intent)? –

+0

是....在活動B中使用startActivityForResult來啓動活動C @ T-lang –

+0

感謝您的回覆,我能夠添加onActivityResult,並且我使用startActivityForResult方法來處理從活動B到C的意圖,但是我無法從onBindViewHolder的activity c onClick中添加「setResult」。讓我知道如果我需要編輯我的代碼來顯示你? @ Ahmad-Rajab –