![](/img/trans.png)
[英]How can i pass the class object into the BroadCastReviever from the mainActivity class
[英]How can i access a MainActivity object from another class
在我的 MainActivity class 我有 object ExpandableListViewAdapter mExpandableListViewAdapter 我需要它在不同的類(非活動)中使用它。 這個 object 在 mainActivity 中初始化,之后我想在另一個 class 中使用他的一種方法。 如何將這個 object 傳遞給另一個 class? 我在想我可以使用一個界面
public class MainActivity extends AppCompatActivity {
private static final Logger LOG = LoggerFactory.getLogger("MainActivity");
private ExpandableListView mExpandableListView;
private ExpandableListViewAdapter mExpandableListViewAdapter;
private List<String> mListDataGroup;
private HashMap<String, List<String>> mListDataChild;
PreselectionAplicationUseCases preselectionAplicationUseCases;
private void unselectPrevious (int childId)
{
setContentView(R.layout.activity_main);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
initObjects();
initListData();
}
private void initViews() {
mExpandableListView = findViewById(R.id.expandableListView);
}
private void initListeners() {
int i= 0;
mExpandableListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
String title = mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
if (Constants.PROPIETARY_SELECTION_SUPPORTED == false)
{
if (groupPosition > 1)
groupPosition -=1;
}
if (groupPosition == 0) {
switch (childPosition) {
case 0:
if (!isServiceRunning()) {
startService();
}
break;
case 1:
if (isServiceRunning()) {
stopService();
}
break;
}
}else if (groupPosition == 1) {
int selection = 0;
selection = mExpandableListViewAdapter.getSelectedChild();
if (selection == -1)
{
mExpandableListViewAdapter.setSelectedChild(childPosition);
TransactionState.setUserPreferredAid(aidList.get(childPosition));
}
else{
if (childPosition == selection)
{
mExpandableListViewAdapter.setSelectedChild(-1);
TransactionState.setUserPreferredAid(null);
}else {
// Deselect previous and select new
mExpandableListViewAdapter.setSelectedChild(childPosition);
TransactionState.setUserPreferredAid(aidList.get(childPosition));
}
}
mExpandableListViewAdapter.notifyDataSetChanged();
}else if (groupPosition == 2) {
if (childPosition == 0) {
startActivity(new Intent(Settings.ACTION_LOCALE_SETTINGS));
}
} else if (groupPosition == 3) {
showContact(title);
}
} else if (groupPosition == 4) {
if (childPosition == 0) {
HTMLPrinter htmlPrinter= HTMLPrinter.getInstance();
htmlPrinter.ticketTest();
}
}
return false;
});
}
private void initObjects() {
// initializing the list of groups
mListDataGroup = new ArrayList<>();
// initializing the list of child
mListDataChild = new HashMap<>();
// initializing the adapter object
mExpandableListViewAdapter = new ExpandableListViewAdapter(this, mListDataGroup, mListDataChild);
// setting list adapter
mExpandableListView.setAdapter(mExpandableListViewAdapter);
}
}
將您的非活動 class 視為 Foo。
class Foo{
private AdapterObj mAdapterObj;
public void setAdapterObj(AdapterObj adapterObj){
this.mAdapterObj=adapterObj
}
......
}
從 MainActivity 傳遞您的 object 實例。
void initViews(){
Foo obj=new Foo();
obj.setAdapterObj(myAdapterObj);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.