我正在使用Parse.com在我的应用的第二个活动中填充ListView。单击ListView中的项目将打开第三个活动。问题是当使用向上按钮返回第二个活动时,ListView消失了。由于某些原因,使用“后退”按钮时不会发生此行为。此外,仅在第二个活动中使用普通查询时不会发生这种情况。关于query.whereEqualsTo(" department",department)行的一些事情正在抛弃整个事情。
第1次活动:
public class MainActivity extends ActionBarActivity {
private Button mMEButton;
private Button mCEButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMEButton = (Button)findViewById(R.id.meButton);
mCEButton = (Button)findViewById(R.id.ceButton);
mMEButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ProfessorsActivity.class);
intent.putExtra("department", "ME");
startActivity(intent);
}
});
mCEButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ProfessorsActivity.class);
intent.putExtra("department", "CE");
startActivity(intent);
}
});
}
第二项活动:
public class ProfessorsActivity extends ActionBarActivity {
private ParseQueryAdapter<ParseObject> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_professors);
Intent intent = getIntent();
final String department = intent.getStringExtra("department");
ParseQueryAdapter.QueryFactory<ParseObject> factory =
new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Professor");
query.whereEqualTo("department", department);
return query;
}
};
adapter = new ParseQueryAdapter<ParseObject>(this, factory) {
@Override
public View getItemView(ParseObject professor, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.professor_item, null);
}
TextView contentView = (TextView) v.findViewById(R.id.professorNameTextView);
contentView.setText(professor.getString("lastName") + ", " + professor.getString("firstName"));
return v;
}
};
//adapter.setTextKey("lastName");
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final ParseObject tappedProfessor = adapter.getItem(position);
String professorId = tappedProfessor.getObjectId();
Intent intent = new Intent(ProfessorsActivity.this, OfficeHoursActivity.class);
intent.putExtra("professorId", professorId);
startActivity(intent);
}
});
}
第3次活动:
public class OfficeHoursActivity extends ActionBarActivity {
private TextView mNameTextView;
private TextView mTimeTextView1;
private TextView mDaysTextView1;
private TextView mTimeTextView2;
private TextView mDaysTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_office_hours);
Intent intent = getIntent();
String professorId = intent.getStringExtra("professorId");
mNameTextView = (TextView) findViewById(R.id.professorName);
mTimeTextView1 = (TextView) findViewById(R.id.officeHourTime1);
mDaysTextView1 = (TextView) findViewById(R.id.officeHourDays1);
mTimeTextView2 = (TextView) findViewById(R.id.officeHourTime2);
mDaysTextView2 = (TextView) findViewById(R.id.officeHourDays2);
mNameTextView.setVisibility(INVISIBLE);
mTimeTextView1.setVisibility(INVISIBLE);
mDaysTextView1.setVisibility(INVISIBLE);
mTimeTextView2.setVisibility(INVISIBLE);
mDaysTextView2.setVisibility(INVISIBLE);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Professor");
// Retrieve the object by id
query.getInBackground(professorId, new GetCallback<ParseObject>() {
public void done(ParseObject currentProfessor, ParseException e) {
if (e == null) {
mNameTextView.setText(currentProfessor.getString("lastName") + ", " + currentProfessor.getString("firstName"));
mTimeTextView1.setText(currentProfessor.getString("start1") + " - " + currentProfessor.getString("end1"));
mDaysTextView1.setText(currentProfessor.getString("days1"));
mNameTextView.setVisibility(VISIBLE);
mTimeTextView1.setVisibility(VISIBLE);
mDaysTextView1.setVisibility(VISIBLE);
if (currentProfessor.getString("start2") != null) {
mTimeTextView2.setText(currentProfessor.getString("start2") + " - " + currentProfessor.getString("end2"));
mDaysTextView2.setText(currentProfessor.getString("days2"));
mTimeTextView2.setVisibility(VISIBLE);
mDaysTextView2.setVisibility(VISIBLE);
}
} else {
//something went wrong
}
}
});
答案 0 :(得分:0)
默认情况下,向上按钮会创建父活动的新实例。 如果你想让它表现为后退按钮只是覆盖&#34; onOptionsItemSelected&#34; in&#34; OfficeHoursActivity&#34;并执行以下操作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
private void ListView(){
ParseQueryAdapter.QueryFactory<ParseObject> factory =
new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Professor");
query.whereEqualTo("department", department);
return query;
}
};
adapter = new ParseQueryAdapter<ParseObject>(this, factory) {
@Override
public View getItemView(ParseObject professor, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.professor_item, null);
}
TextView contentView = (TextView) v.findViewById(R.id.professorNameTextView);
contentView.setText(professor.getString("lastName") + ", " + professor.getString("firstName"));
return v;
}
};
//adapter.setTextKey("lastName");
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
现在从onResume()调用此方法。不要从onCreate()调用它。
@Override
public void onResume(){
super.onResume();
ListView();
}
现在检查..可能是你的问题会解决.. !!!
答案 2 :(得分:0)
我通过将以下标记添加到AndroidManifest.xml文件中的第二个活动来解决了这个问题。
android:launchMode="singleTop"
这可以防止“向上”按钮在单击时重新创建活动。