Android 如何将一个活动的ListView中的数据与其他活动进行更改
我很少看到关于SOF的问题,但它们都没有帮助 在我的应用程序中,我有一个用户列表,可以通过单击用户的朋友来访问该列表。流程是:Android 如何将一个活动的ListView中的数据与其他活动进行更改,android,listview,Android,Listview,我很少看到关于SOF的问题,但它们都没有帮助 在我的应用程序中,我有一个用户列表,可以通过单击用户的朋友来访问该列表。流程是: 转到我的个人资料 单击“我的朋友”转到包含用户(我的朋友)列表的活动 单击该用户配置文件中的任何listView项 从这个配置文件中,我可以看到该用户的好友列表与我的相同 问题是所有这些listView项目都有一个按钮来添加为好友,这使得我和该用户成为该列表中的好友(就像在twitter中跟踪对follow的更改),现在我通过backback返回,在前面的一个列表视图中
如何在所有列表视图中更改该用户的按钮(我的适配器数据中的标志?使用
界面
将事件发送回活动,并在收到事件时更新列表或数据库
接口是向“外部世界”传递消息的方式。只需在ClickListener上查看一个简单的按钮
。基本上,您可以在按钮上调用setOnClickListener(this)
,并实现onClickListener
,这是一个界面。无论何时单击按钮,您都会在onClick
中看到一个事件。这是在活动之间传递消息的最安全的方式,而不需要意图(我认为这是一种巨大的痛苦…),下面是一个例子:
例如:
class A extends Activity implements EventInterface{
public A(){
//set a listener. (Do not forget it!!!)
//You can call it wherever you want;
//just make sure that it is called before you need something out of it.
//safest place is onCreate.
setEventInterfaceListener( A.this );
}
//this method will be automatically added once you implement EventInterface.
void eventFromClassB(int event){
//you receive events here.
//Check the "event" variable to see which event it is.
}
}
class B{
//Interface logic
public interface EventInterface{
public static int BUTTON_CLICKED = 1;
void eventFromClassB(int event);
}
static EventInterface events;
public static void setEventInterfaceListener(EventInterface listener) {
events = listener;
}
private void dispatchEvent(int trigger) {
if (events != null) {
events.eventFromClassB(trigger);
}
}
//Interface ends
void yourMethod(){
//Call this whenever you want to send an event.
dispatchEvent( BUTTON_CLICKED );
}
}
尝试定义静态全局项和listview。或者使用Bundle和putexrasi无法将数据发送回backbackback中5步之外的活动。实际上,我甚至不知道要更改哪个ListView。静态数据也不起作用,因为有多个活动实例具有列表。如果不可能使用全局变量,则不适合在这些活动中使用,但在恢复事件时,只需从DB或任何您想要的位置重新创建列表。如何为每个用户存储好友列表?你在使用数据库吗?另外,我假设你提到的每一个步骤都会启动一个新的活动,你能提供一些代码或你推荐的链接吗。