收集多个意向附加项,同时在返回主活动期间保存结果
当我使用另一个意图打开一个活动并返回结果时,我遇到了存储我之前活动的意图附加项的问题。收集多个意向附加项,同时在返回主活动期间保存结果
逻辑:
- 活动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);
}
});
}
我认为这种物品可以是有帮助的 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()
活动b是从活动c和a收集信息的主要活动,但流程从活动A开始,您在活动b中建议我应该尝试使用startActicityForResult而不是StartActivity(intent)? –
是....在活动B中使用startActivityForResult来启动活动C @ T-lang –
感谢您的回复,我能够添加onActivityResult,并且我使用startActivityForResult方法来处理从活动B到C的意图,但是我无法从onBindViewHolder的activity c onClick中添加“setResult”。让我知道如果我需要编辑我的代码来显示你? @ Ahmad-Rajab –
您确定pipesInspectionSelected.getText()。toString()具有您想要传递的正确值吗? – roostertech
@roostertech是的它的确如此 –