是否有可能从OnCllickListener方法检索ArrayList值并将其保存在其父方法中?

问题描述 投票:0回答:2
 saveBtnYes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveBtnYesFunc(currentYesBtnId);
                    tempActivityList =activityNames;
                    return activityNames;
                }
            });

当我调用saveBtnYesFunc函数时,它会向onClick线程返回一个名为activityNames的ArrayList值。我想将该值从onClickListener方法内部传递给其父方法(此代码包含在另一个方法中)。代码return activityNames的最后一行无效。

还有其他方法吗?

java android function methods
2个回答
0
投票

您的问题还不清楚,我想您希望单击Button时将ArrayactivityNames)保存在某处。如果我理解得很好,您可以通过这种方式进行:

class YourClass {  //almost surely it extends Activity (or AppCompatActivity)

    private List<String> thePlaceWhereTheArrayWillBeLocated = new ArrayList<String>();  //You should define a class varibale where the result of saveBtnYesFunc will ends up (I think that the content is of String type)

    ... onCreate(...) {
    super.onCreate(savedInstanceState);
    ...
    ...
    saveBtnYes.setOnClickListener(new View.OnClickListener() {   //I don't know if it's inside the onCreate, but it could be here
            @Override
            public void onClick(View v) {
                thePlaceWhereTheArrayWillBeLocated = saveBtnYesFunc(currentYesBtnId);  //Maybe some type casts are needed
                //tempActivityList =activityNames;
                //return activityNames;    //This line is totally incorrect
            }
        });
    }

    void wheneverFunctionYouWant() {
        //do whatever you want with thePlaceWhereTheArrayWillBeLocated 
    }
}

0
投票

您需要确定谁真正有兴趣查看此新列表。假设感兴趣的部分是某个类ListUsingClass的实例,则应该可以使用。

final ListUsingClass listUser = new ListUsingClass(); // or assign to existing reference
saveBtnYes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listUser.setActivityNames(saveBtnYesFunc(currentYesBtnId));
                 }
            });
© www.soinside.com 2019 - 2024. All rights reserved.