如何从另一个类访问主活动对象



在我的MainActivity类中,我有对象ExpandableListViewAdapter mExpandableListViewAdapter,我需要它在不同的类(非活动(中使用它。这个对象在mainActivity中初始化,之后我想在另一个类中使用他的方法之一。如何将此对象传递给另一个类?我在想我可以使用一个界面

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

将你的非活动类视为 Foo。

class Foo{

private AdapterObj mAdapterObj;
public void setAdapterObj(AdapterObj adapterObj){
this.mAdapterObj=adapterObj
}
......
}

从主活动传递对象实例。

void initViews(){
Foo obj=new Foo();
obj.setAdapterObj(myAdapterObj);
}

最新更新