Spinner Array是否需要将其项目链接到现有布局?

时间:2019-03-18 09:04:48

标签: android android-spinner

因此,我目前从微阵列中选取一个微调框,该数组根据数组中的内容获取名称和图像。 我需要的是仅根据我拥有的布局从数组中的项目中选择是否存在。 例如。我的主要活动列表中有多个帐户。我只希望能够根据可用帐户在微调器中选择一个帐户(用户只有3个数组中的2个,因此仅在微调器中显示2个项目,而不是全部3个)

这是我当前的微调代码以及数组:

SpinnerActivity:

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的方法,所以我不确定是否需要更改阵列?

1 个答案:

答案 0 :(得分:0)

您只需更改数组。如果您需要异步获取帐户列表,则可以在异步完成回调中调用mAdapter.notifyDataSetChanged()来告诉Adapter其备份数组已更改。