使用下一个和上一个按钮在同一活动之间跳转
我已经搜索过,但没有找到我遇到的问题的任何明确答案。
在我的应用程序中,我有一项活动,您可以回答一个检查点,发送它并转到下一个检查点。用户还应该能够跳过并跳回到上一个检查点。
让我用一些代码来支持我的故事。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkpointform);
go = (GlobalObject)getApplication();
_checkPoints = new ArrayList<CheckPoint>();
_checkPoints.addAll(go.getCheckPoints());
_currentActiveItemId = getIntent().getIntExtra("checkPointId", 0);
int count = 0;
while(count < _checkPoints.size()) {
if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
_checkPointTitle = _checkPoints.get(count).getCheckPointTitle();
}
count++;
}
final TextView tvCheckPointTitle = (TextView)findViewById(R.id.txtCheckpoint);
tvCheckPointTitle.setText(_checkPointTitle);
this.registerButtonEvents();
_simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
Log.d(TAG, "On create");
}
这段代码没有优化。在我这样做之前,我希望它能先工作,所以不要为此对我太苛刻,但建议总是非常受欢迎的。
那么这里发生了什么。 全局(应用程序范围)对象被实例化。 我得到一个数组列表并将其存储在本地数组列表中。
我从屏幕上得到了额外的信息,让我来到了这里。 我循环遍历列表,将当前选定的项目 ID 与主键进行比较,以获得要显示的正确数据。
然后,我像这样注册按钮事件:
private void registerButtonEvents() {
ImageButton btnTakePicture = (ImageButton)findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePictureButton();
}
});
Button btnPreviousCheckPoint = (Button)findViewById(R.id.btnPrev);
btnPreviousCheckPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
previousCheckPoint();
}
});
Button btnNextCheckPoint = (Button)findViewById(R.id.btnNext);
btnNextCheckPoint .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextCheckPoint();
}
});
}
onclick 事件中调用的方法尚未执行任何操作。 这就是我需要帮助的地方。
我需要的是,要销毁的当前活动,但在此之前,调用此活动..可能类似于: Intent Intent = new Intent(this, checkPointFormActivity);
并将当前选定的 id + 1 赋予下一个/上一个活动(如果下一个项目为空,我将获取列表中的第一项。这是稍后我要担心的逻辑)
但是我在调用此活动时一直不成功。
简而言之:
我需要使用新数据调用与当前活动相同的活动。这样做后,我想销毁/完成/删除/取消之前的活动。 (假设检查点 1 是当前活动。我按下一步,该活动应该使用新数据刷新/完成,并使用以 ID 形式在意图中传递的新数据来调用。
我希望我能以这种方式提供足够的信息。 如果有什么不清楚的话。这不会让我感到惊讶。请告诉我,以便我进行编辑。
我尝试的是
private void nextCheckPoint() {
int count = 0;
int listLength = _checkPoints.size();
while(count < listLength) {
if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
final Intent intent = new Intent(this, CheckPointFormActivity.class);
intent.putExtra("checkPointId", _currentActiveItemId + 1);
startActivity(intent);
}
}
}
在 startactivity 之后和之前调用 finish() 。这导致我的应用程序根本没有响应。
非常感谢您花时间阅读这个问题。希望尽快看到答案或任何有关如何改善流程的建议。谢谢。
I have searched and not found any clear answers to a problem im having.
In my application, I have an activity where you answer to a checkpoint, send it and go to the next one. A user should also be able to skip, and jump back to the previous checkpoint.
Let me support my story with a bit of code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkpointform);
go = (GlobalObject)getApplication();
_checkPoints = new ArrayList<CheckPoint>();
_checkPoints.addAll(go.getCheckPoints());
_currentActiveItemId = getIntent().getIntExtra("checkPointId", 0);
int count = 0;
while(count < _checkPoints.size()) {
if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
_checkPointTitle = _checkPoints.get(count).getCheckPointTitle();
}
count++;
}
final TextView tvCheckPointTitle = (TextView)findViewById(R.id.txtCheckpoint);
tvCheckPointTitle.setText(_checkPointTitle);
this.registerButtonEvents();
_simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
Log.d(TAG, "On create");
}
This code is not optimized. I want it to work first before I do that so don't be hard on me for that, but tips are always very welcome.
So what happens here.
A global(applicationwide) object is instantiated.
I get an arraylist and store it in a local arraylist.
I take the extra from the screen that got me here.
I loop trough the list to compare the currentselected item id with primary key, to get the right data to show.
I then register button events like so:
private void registerButtonEvents() {
ImageButton btnTakePicture = (ImageButton)findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePictureButton();
}
});
Button btnPreviousCheckPoint = (Button)findViewById(R.id.btnPrev);
btnPreviousCheckPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
previousCheckPoint();
}
});
Button btnNextCheckPoint = (Button)findViewById(R.id.btnNext);
btnNextCheckPoint .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextCheckPoint();
}
});
}
The methods called in the onclick events don't do anything yet.
This is where I need help.
What I need is, the current activity to be destroyed, but before that happens, call this activity.. could be something like: Intent intent = new Intent(this, checkPointFormActivity);
And give the next/previous activity the currentselected id + 1 (if the next item is null, ill get the first item in the list.. this is logic ill worry about later)
However I have been unsuccesfull in calling this activity.
So in short:
I need to call the same activity as the current one, with new data. Upon doing that, I want to destroy/finish/delete/un-do the previous activity. (lets say checkpoint 1 is the current activity. I press next, this activity should be refreshed with new data/finished and be recalled with new data passed in the intent in the form of an ID.
I hope I am providing enough information this way.
If anything is unclear. And it would not surprise me. Please let me know so I can edit.
What I tried is
private void nextCheckPoint() {
int count = 0;
int listLength = _checkPoints.size();
while(count < listLength) {
if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
final Intent intent = new Intent(this, CheckPointFormActivity.class);
intent.putExtra("checkPointId", _currentActiveItemId + 1);
startActivity(intent);
}
}
}
and also ttried calling finish() after and before startactivity. It results in my app not responding at all.
Thanks alot for even taking the time to read the question. Hope to see an answer or any advice of how to imrpove the flow soon. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能没有正确理解你的意思,但是重新实例化活动进行导航的原因是什么?为什么不:
ViewFlipper
在同一活动中的视图之间导航?编辑
您说您不知道如何使用新信息刷新小部件。这应该相当容易。
您有导航按钮的事件处理程序,因此您可以确定新的检查点以及要显示的数据。视图中显示检查点特定数据的每个控件都必须针对新检查点进行更改。
例如,检查点的名称可以显示在
TextView
中。然后你必须更新TextView
,如下所示:你可以创建一个获取检查点 ID 的方法(索引可能是检查点的对象列表或数据库 ID 或其他)并更新控件。然后,在确定要显示的检查点的 ID 后,从
previousCheckPoint
和nextCheckPoint
方法调用此方法。当然,当 Activity 第一次显示第一个检查点时,您也会调用此方法。I may not have understood you correctly, but what's the reason for re-instantiating the activity to navigate? Why don't you:
ViewFlipper
to navigate between the views within the same activity?ViewPager
to use fragments the user can navigate using gestures?EDIT
You say you do not know how to refresh the widget with new information. This should be fairly easy.
You have event handlers for the navigation buttons, so you can determine the new checkpoint, the data of which you want to display. Each control in the view that displays checkpoint-specific data must be changed for the new checkpoint.
For example, the name of the checkpoint could be displayed within a
TextView
. Then you'd have to update theTextView
like:You could create a method that takes the ID of the checkpoint (may be the index is a list of objects for the checkpoints or a database ID or whatever) and updates the controls. Then you call this method from your
previousCheckPoint
andnextCheckPoint
methods after determining the ID of the checkpoint to be displayed. Of course, you call this method also when the Activity is displayed for the first time for the first checkpoint.