Android的新手,所以我真的不知道这里发生了什么;
现在,在我的主要活动中,我有一个下拉式微调器;点击一个按钮进入第二个活动,其中有一个列表视图,其中包含来自下拉微调器内容的信息。
当我点击另一个按钮并转到第三个活动,然后按回来时,下拉微调器内容中的信息不再存在;而是有空值。
有人知道可能是什么问题吗?后退按钮是否发送意图?
第二项活动:
public class classList extends AppCompatActivity {
ListView listView;
SimpleCursorAdapter mAdapter;
static private String[] classes = {"class1","class2"};
String course;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
course = intent.getStringExtra("com.naomi.classAlert.course");
number = intent.getStringExtra("com.naomi.classAlert.number");
Toast.makeText(getApplicationContext(), course+number,
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_class_list);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] {
course+number+" 001", course+number+" 002", course+number+" 003"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
}
});
}
}
第三项活动:
public class pickSection extends AppCompatActivity {
String course;
String number;
int section;
//SharedPreferences faves = PreferenceManager
// .getDefaultSharedPreferences(this);
SharedPreferences faves;
SharedPreferences.Editor editor;
Button button;
TextView courseInfo;
TextView capacity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
faves = getSharedPreferences("preferencename",0);
editor = faves.edit();
setContentView(R.layout.activity_pick_section);
button = (Button) findViewById(R.id.favouriteButton);
courseInfo = (TextView) findViewById(R.id.courseinfo);
capacity = (TextView) findViewById(R.id.capacity);
Intent intent2 = getIntent();
course = intent2.getStringExtra("com.naomi.classAlert.classList.course");
number = intent2.getStringExtra("com.naomi.classAlert.classList.number");
section = intent2.getIntExtra("com.naomi.classAlert.classList.section",0);
courseInfo.setText(course+number+" "+section);
capacity.setText("dummy text");
String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")){
button.setText("Remove from favourites");
}
}
public void addToFavourites(View v) {
String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")) { //it exists in favourites
editor.remove(course + number + section);
button.setText("Add to favourites");
} else {
editor.putString(course+number+section, "favourite");
button.setText("Remove from favourites");
}
editor.commit();
}
}
答案 0 :(得分:2)
可以通过手机上的后退按钮关闭活动。在这种情况下,执行finish()方法。如果活动是使用startActivity(Intent)方法调用启动的,则调用者不需要现在已关闭的活动的结果或反馈。
如果使用startActivityForResult()方法调用启动活动,则需要来自子活动的反馈。子活动结束后,将调用子活动上的onActivityResult()方法,您可以根据结果执行操作。
public void onItemClick (....){
// start pickSection
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
startActivityForResult(intent1, 0);
}
按下后退按钮, pickSection 完成后,它可以通过Intent将数据发送回其来电。这是在finish()方法中完成的。 @override
public void finish() {
Intent intent1 = new Intent();
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
// Activity finished ok, return the data
setResult(RESULT_OK, intent1);
super.finish();
}
一旦pickSection完成,就会调用 classList 中的onActivityResult()方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
if (data.hasExtra("com.naomi.classAlert.classList.course")) {
Toast.makeText(this, data.getExtras().getString("com.naomi.classAlert.classList.course"),
Toast.LENGTH_SHORT).show();
}
}
}