从另一个活动编辑数组 [英] edit array from another activity

查看:154
本文介绍了从另一个活动编辑数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从另一个活动编辑自定义 arraylist 时,应用程序部队已关闭。

When trying to edit a custom arraylist from another Activity the application force closed.

Arraylist in MainActivity

Arraylist in MainActivity:

ArrayList<Contacts> contacts = new ArrayList<Contacts>();

另一个活动

MainActivity main = new MainActivity();
main.contacts.get(position).contactname=x;
main.contacts.get(position).contactnumber=x;

如何编辑其他活动的arraylist?

How can I edit an arraylist from another activity?

Logcat:

09-08 22:53:40.415    1760-1760/com.example.amir_p.contacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.amir_p.contacts, PID: 1760
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {com.example.amir_p.contacts/com.example.amir_p.contacts.ContactInfo}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3577)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3620)
        at android.app.ActivityThread.access$1300(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.amir_p.contacts.ContactInfo.onActivityResult(ContactInfo.java:53)
        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3573)


推荐答案

两个问题:

您无法实例化活动:

new MainActivity();

必须从Intent启动活动,如下所示:

Activities must be launched from an Intent like so:

thisActivity.startActivity( new Intent( NextActivity.class ) );



2



如果你想要两项活动要共享列表,那么您必须在更大的范围内实例化该列表,例如通过创建自定义应用程序类或创建单例来实现应用程序范围。

2

If you want two activities to share a list, then you must instantiate that list in a greater scope, like the Application scope by creating a custom application class or creating a singleton.

Singleton:

Singleton:

public final class MySingleton {
    private static final MySingleton SELF = new MySingleton();

    private List<Contacts> contacts = new ArrayList<Contacts>();
    private boolean didContacts

    private MySingleton() {
        // Don't want anyone else constructing the singleton.
    }

    public static MySingleton getInstance() {
        return SELF;
    }

    public List<Contacts> getContacts() {
        return contacts;
    }
}

现在您喜欢的任何活动都可以获得清单

Now in any activity you like you may get the list

MySingleton.getInstance().getContacts()

请注意并发修改。

由于评论请求而修改

评论中指出,您希望将一项活动与另一项活动进行更改。您必须意识到一次只有一个活动处于活动状态,因此您不应该这样做,无论您是否可以破解它。我通常会推荐一个监听器,但是听众不会在这种情况下工作,因为只有一个Activity另一种方法是让一些内存状态存在于活动之外(可能是单例)活动可以响应它们重新获得焦点。

It was stated in the comments that you want to change one activity from another. You must realize only one activity is active at a time and so you SHOULD NOT do that, regardless of whether or not you could hack it. I would normally recommend a listener but a listener wont work in this case since only one Activity Another way to do this would be to have some in-memory state that lives outside the activity (maybe the singleton) that the activities can respond to when they regain focus.

伪代码

ActivityB {
  onClick() {
    MySingleton.getInstance().getContacts().add( theContact );
  }
}

ActivityA {
    onResume() {
        // consider contacts may have changed and redraw
        listViewAdapter.clear();
        listViewAdapter.addAll( MySingleton.getInstance().getContacts() );
    }    
}

这篇关于从另一个活动编辑数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆