gpt4 book ai didi

java - 异步任务完成后强制更新选项卡内容

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:56 26 4
gpt4 key购买 nike

我有一个包含 3 个选项卡的 TabActivity。有一个异步任务,当通过单击菜单项进行刷新运行时,它会从服务器检索更新的数据。此数据存储在 Controller 中,并由所有 View 访问,因此模型只需要加载一次。

我的问题是,在异步 Activity 运行并更新模型后,我如何向所有三个选项卡发送信号以更新它们的内容?

我的 Activity

public class DashboardActivity extends TabActivity {
private ProfileModel profile;
private TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

profile = Controller.getProfile();

this.setContentView(R.layout.dashboard);

tabHost = getTabHost();
setupTab(new TextView(this), "Home", new Intent().setClass(DashboardActivity.this, HomeActivity.class));
setupTab(new TextView(this), "History", new Intent().setClass(DashboardActivity.this, PaymentsActivity.class));
setupTab(new TextView(this), "My Wallet", new Intent().setClass(DashboardActivity.this, MyWalletActivity.class));

tabHost.setCurrentTab(0);

ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setTitle(profile.Name);

}

private void setupTab(final View view, final String tag, Intent content) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag)
.setIndicator(tabview)
.setContent(content);
tabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:

return true;
case R.id.menu_refresh:
new RefreshDashboardTask().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}


private class RefreshDashboardTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute()
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);

if(actionBar != null)
actionBar.setProgressBarVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... params)
{
try {
profile = DataHelper.getProfile();
Controller.setProfile(profile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void result)
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);

if(actionBar != null)
actionBar.setProgressBarVisibility(View.GONE);
}
}
}

编辑 为了进一步阐述,这里有更多代码。

我的 Controller

public class Controller extends Application {
private static Controller instance;
private static DefaultHttpClient client;
private static ProfileModel profile;

public Controller() {
instance = this;
client = new DefaultHttpClient();
profile = null;
}

public static Context getContext() {
return instance;
}

public static DefaultHttpClient getHttpContext() {
return client;
}

public static ProfileModel getProfile() {
return profile;
}

public static void setProfile(ProfileModel profile) {
Controller.profile = profile;
}

@Override
public void onCreate()
{
super.onCreate();

}
}

我的一项 Activity 是在选项卡 View 中。这是最简单的一个,因为它只是一个列表。主页 View 是 2 个列表,由标题分隔,钱包 View 是动态生成的带有标题的列表,从集合中的集合创建。

public class PaymentsActivity extends Activity {
ProfileModel profile;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.setContentView(R.layout.payment_history);

profile = Controller.getProfile();

ListView itemList = (ListView)this.findViewById(R.id.payment_history_list);
itemList.setTextFilterEnabled(true);
itemList.clearChoices();
itemList.setAdapter(new ItemSummaryAdapter(PaymentsActivity.this, R.layout.list_item_payment, profile.Items));
//statementList.setOnItemClickListener(clickListener);
}
}

这里的目标是刷新按钮更新 Controller 中的数据。我在选项卡中的所有 View 都已更新。

最佳答案

更新:

如果我是你,我会 Observer pattern .首先,向您的 Controller 类添加一组监听器:

public class Controller extends Application {
private static Controller instance;
private static DefaultHttpClient client;
private static ProfileModel profile;
private static Set<ControllerUpdateListener> updateListeners = new HashSet<ControllerUpdateListener>();

//...

public static void addListener(ControllerUpdateListener listener)
{
updateListeners.add(listener);
}

public static interface ControllerUpdateListener {
void onControllerUpdate(ProfileModel model);
}
}

然后让您的个人选项卡 Activity 实现 ControllerUpdateListener。最后,将触发器添加到 Controller 类:

public static void setProfile(ProfileModel profile) {
Controller.profile = profile;

for(ControllerUpdateListener l : updateListeners) {
l.onControllerUpdate(profile);
}
}

不要忘记将每个 Activity 注册为 Controller 的监听器:

public class PaymentsActivity extends Activity implements Controller.ControllerUpdateListener {
ProfileModel profile;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.setContentView(R.layout.payment_history);

Controller.addListener(this); // <-- Don't forget this...
profile = Controller.getProfile();

ListView itemList = (ListView)this.findViewById(R.id.payment_history_list);
itemList.setTextFilterEnabled(true);
itemList.clearChoices();
itemList.setAdapter(new ItemSummaryAdapter(PaymentsActivity.this, R.layout.list_item_payment, profile.Items));
//statementList.setOnItemClickListener(clickListener);
}

public void onControllerUpdate(ProfileModel model) {
//update these views...
}
}

现在,您的每个选项卡 Activity 都应该在 onControllerUpdate(ProfileModel) 方法中触发它们的 notifyDataSetChanged()(或任何其他触发它们更新的方法)。

关于java - 异步任务完成后强制更新选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com