在片段中,单击listView
项目以打开新活动并将值传递给其他活动
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sowing sowing=new sowing();
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer,sowing,sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList =(list.getItemAtPosition(position).toString());
}
});
答案 0 :(得分:0)
最好的方法是使用SharedPreferences。
https://developer.android.com/reference/android/content/SharedPreferences
https://developer.android.com/training/data-storage/shared-preferences
答案 1 :(得分:0)
您要找的是Bundles
以下是您在案例中如何使用它的示例:
list.setOnClickListener(new AdapterView.onItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
sowing sowing = new sowing();
FragmentManger fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer, sowing, sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList = (list.getItemAtPosition(position).toString));
// THE BUNDLE
// start a new intent to open the activity
Intent newIntent = new Intent(Context, Activity.class);
newIntent.putStringExtra("NAME_OF_BUNDLE", selectedFromList); // I used putStringExtra because you defined 'selectedFromList' to be a string
startActivity(newIntent);
}
});
现在,在您打开的活动中,只需调用包的名称,如下所示:
Intent intent = getIntent(); // get the intent that caused this activity to be opened
String selectedFromLastActivity = intent.getStringExtra("NAME_OF_BUNDLE"); // from the intent that caused this activity to be opened, get any extras passed through