Spinner Array 需要 link 它是现有布局的项目?
Spinner Array needs to link it's items to existing layouts?
因此,我目前使用的微调器是从一个程序化数组中选取的,该数组会根据数组中的内容获取名称和图像。
我需要的是仅根据我的布局从数组中的项目中挑选它们。
例如。我的 activity 主列表中有多个帐户。我只希望能够根据我可用的帐户在微调器中选择一个帐户(用户只有数组中 3 个帐户中的 2 个,因此在微调器中只显示 2 个项目而不是全部 3 个)
这是我当前的微调器代码以及数组:
微调器活动:
public class SpinnerActivity extends AppCompatActivity {
private ArrayList<AccountItem> mAccountList;
private AccountAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transactions);
//the account_spinner is being pulled from the fragment_transactions xml
initList();
Spinner spinnerAccount = findViewById(R.id.account_spinner);
mAdapter = new AccountAdapter(this, mAccountList);
spinnerAccount.setAdapter(mAdapter);
spinnerAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
AccountItem clickedItem = (AccountItem) parent.getItemAtPosition(position);
String clickedAccountName = clickedItem.getAccountName();
Toast.makeText(SpinnerActivity.this, clickedAccountName + " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/**
*This is the array, I need this to link each item to their respective accounts
*that are available
**/
private void initList() {
mAccountList = new ArrayList<>();
mAccountList.add(new AccountItem("Account1", R.drawable.account1_icon));
mAccountList.add(new AccountItem("Account2", R.drawable.account2_icon));
mAccountList.add(new AccountItem("Account3", R.drawable.account3_icon));
}
}
我只需要知道从哪里开始。就目前而言,我看不到为我的数组项提供单独 ID 的方法,所以我不确定是否需要更改我的数组?
你只是改变数组。如果您需要异步获取帐户列表,您可以在异步完成回调中调用 mAdapter.notifyDataSetChanged()
来告诉 Adapter
它的支持数组已更改。
因此,我目前使用的微调器是从一个程序化数组中选取的,该数组会根据数组中的内容获取名称和图像。 我需要的是仅根据我的布局从数组中的项目中挑选它们。 例如。我的 activity 主列表中有多个帐户。我只希望能够根据我可用的帐户在微调器中选择一个帐户(用户只有数组中 3 个帐户中的 2 个,因此在微调器中只显示 2 个项目而不是全部 3 个)
这是我当前的微调器代码以及数组:
微调器活动:
public class SpinnerActivity extends AppCompatActivity {
private ArrayList<AccountItem> mAccountList;
private AccountAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transactions);
//the account_spinner is being pulled from the fragment_transactions xml
initList();
Spinner spinnerAccount = findViewById(R.id.account_spinner);
mAdapter = new AccountAdapter(this, mAccountList);
spinnerAccount.setAdapter(mAdapter);
spinnerAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
AccountItem clickedItem = (AccountItem) parent.getItemAtPosition(position);
String clickedAccountName = clickedItem.getAccountName();
Toast.makeText(SpinnerActivity.this, clickedAccountName + " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/**
*This is the array, I need this to link each item to their respective accounts
*that are available
**/
private void initList() {
mAccountList = new ArrayList<>();
mAccountList.add(new AccountItem("Account1", R.drawable.account1_icon));
mAccountList.add(new AccountItem("Account2", R.drawable.account2_icon));
mAccountList.add(new AccountItem("Account3", R.drawable.account3_icon));
}
}
我只需要知道从哪里开始。就目前而言,我看不到为我的数组项提供单独 ID 的方法,所以我不确定是否需要更改我的数组?
你只是改变数组。如果您需要异步获取帐户列表,您可以在异步完成回调中调用 mAdapter.notifyDataSetChanged()
来告诉 Adapter
它的支持数组已更改。