java—有没有任何可能的方法从OnClickListener方法中检索arraylist值并将其保存在其父方法中?

bfrts1fy  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(275)
saveBtnYes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveBtnYesFunc(currentYesBtnId);
                    tempActivityList =activityNames;
                    return activityNames;
                }
            });

当我打电话给 saveBtnYesFunc 函数,它返回一个名为 activityNames 到onclick线程。我想从内部传递这个值 onClickListener 方法到其父方法(此代码包含在另一个方法中)。最后一行代码 return activityNames 不起作用。
还有别的办法吗?

kxe2p93d

kxe2p93d1#

你需要确定谁真正有兴趣看到这个新列表。假设感兴趣的部分是某个类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));
                 }
            });
vuv7lop3

vuv7lop32#

你的问题不太清楚,我想你希望 Button 单击,则 Array ( activityNames )它被保存在某处。你可以这样做,如果我明白的话:

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 
    }
}

相关问题