在Android中重新绘制一个Activity

发布于 2024-10-12 03:07:16 字数 1610 浏览 5 评论 0 原文

我的问题看起来很简单,但我无法在这里找到答案。

我在一个应用程序中有不同的活动。活动相互启动。 我有一个“主屏幕”,显示所有不同的可用级别,用户单击某个级别并且该活动已启动,当用户完成该级别时,他可以按后退按钮转到主屏幕并开始另一个级别。 当按下后退按钮时,我想再次重新绘制“主屏幕”,因为这次已完成级别的拇指将有所不同,以表明它已完成。

那么如何在按下后退按钮时对活动进行重新绘制? (我想我应该在 Activity.onResume 方法中放置一些代码) (我使用 SharedPreferences 来保存已完成关卡的状态)

主屏幕活动基本上是这样的:

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);

SharedPreferences settings = getSharedPreferences((getResources().getString(R.string.PREFS_HI)),0); /**/ GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this,(settings.getBoolean("level_1_finished", false)))); /**/ gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Open the map which was clicked on, if there is one if(position+1 > 1){ Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show(); Intent myIntent = new Intent(v.getContext(), Tutorial2D.class); startActivity(myIntent); } } }); }

然后,ImageAdapter 使用布尔值 true/false 来选择图像源:未完成的缩略图或已完成的级别缩略图。

my problem seems simple but I can not manage to find an answer here.

I have different activities in one application. Activities start each other.
I have a "main-screen" that shows all the different available levels, user clicks on a level and that activity is lunched, when user finishes that level, he can press the back-button to go to the main-screen and start another level.
When back-button is pressed I would like to re-draw the "main-screen" again, since this time the thumb for the completed level will be different to show that it is completed.

So how do I run a re-draw on a activity upon back-button pressed?
(I suppose I should put some code in the Activity.onResume method)

(I use SharedPreferences to save the state of finished levels)

The main-screen activity is basically this:

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);

SharedPreferences settings = getSharedPreferences((getResources().getString(R.string.PREFS_HI)),0); /**/ GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this,(settings.getBoolean("level_1_finished", false)))); /**/ gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Open the map which was clicked on, if there is one if(position+1 > 1){ Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show(); Intent myIntent = new Intent(v.getContext(), Tutorial2D.class); startActivity(myIntent); } } }); }

ImageAdapter then uses the boolean true/false to choose the source of the image, either the unfinished thumb or the finished level thumb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-10-19 03:07:16

首先将您的 GridView 对象作为类成员。然后重写 onResume() 方法,您可以在该方法中根据用户的操作重新设置网格视图适配器。

private GridView gridview;
@Override
protected void onResume() {
    super.onResume();
    gridview.setAdapter(/* your new ImageAdapter here */);
}

First put your GridView objet as a class member. Then override the onResume() method in which you can re-set the grid view adapter based on what the user did.

private GridView gridview;
@Override
protected void onResume() {
    super.onResume();
    gridview.setAdapter(/* your new ImageAdapter here */);
}
雪花飘飘的天空 2024-10-19 03:07:16

您应该使用 Activity 的 >startActivityForResult() 方法。基本上,您正在开始下一个活动,并在完成时询问请求的状态。在完成之前,它应该调用 setResult(允许您指定用户是否完成该级别)。调用它的 Activity(您的主屏幕)将具有其 onActivityResult() 方法被调用。在此方法中,您可以根据需要更新视图。

You should use the startActivityForResult() method of Activity. Basically, you are starting your next Activity, asking for the status of the request when it finishes. Just before it finishes, it should call setResult (allowing you to specify whether the user completed the level or not). The Activity that called it (your main screen) will have its onActivityResult() method called. In this method, you can update your Views as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文