在我的应用程序中,我有一个Spinner
,可以填充两个Array
s of Strings
,存储在我的values/strings.xml
资源。根据两个单选按钮的状态,选择正确数组中的值并填充我的微调器。
对于每个字符串数组,我都有一个大小相同的图标数组。我的所有图标都以“A”开头,后面跟着一个数字。我无法更改它,它就像数据库中一样存储。我在 Strings.xml 中有一个数组,其中包含绘制图标所需的所有数字。所有实际的图标都可以在drawable
资源文件夹。
现在我想显示微调器选择的列表中字符串旁边的相应项目。当选择一个项目时,我只需要查看文本。
我在互联网上搜索了一个很好的教程,但根据我发现的内容,我还没有成功地做到这一点。我刚刚开始 Android 编程,所以我想我需要一些帮助。我希望有人能引导我走向正确的方向。
我将字符串数组放入实际数组中,例如:
// Arrays with each function in text
arbeiderjobs = getResources().getStringArray(R.array.arbeiders);
bediendejobs = getResources().getStringArray(R.array.bediende);
// Arrays with each function ID
arbeiderjobsid = getResources().getIntArray(R.array.arbeidersid);
bediendejobsid = getResources().getIntArray(R.array.bediendeid);
当选择其中一个单选按钮时,我使用以下事件处理程序代码:
// RadioButton 'Arbeider': If a value gets chosen in this list,
// the corresponding ID is put in the below variable
RadioButton rbArbeider = (RadioButton) findViewById(R.id.rbArbeider);
rbArbeider.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
showSpinner(arbeiderjobs, R.id.cmbFuncties);
s.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id)
{
functie = arbeiderjobsid[position];
statuut = 1;
visible = false;
}
});
visible = true;
}
} // s.setOnItemSelectedListener
}); // rbArbeider.setOnCheckedChangeListener
这是我的showSpinner
method:
private void showSpinner(String [] jobs, int id)
{
MyCustomAdapter adapter = new MyCustomAdapter
(
FindJob.this,
R.layout.spinnerrow,
jobs
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < jobs.length; i++)
{
adapter.add(jobs[i]);
}
Spinner s = (Spinner) findViewById(id);
s.setClickable(true);
s.setAdapter(adapter);
}
我制作了一个基于的自定义适配器本教程,但我有点纠结......我需要一些帮助:
public class MyCustomAdapter extends ArrayAdapter<String>
{
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent,
arbeiderjobs, arbeiderjobsid);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent,
arbeiderjobs, arbeiderjobsid);
}
public View getCustomView(int position, View convertView, ViewGroup parent,
String jobs[], int jobsid[])
{
// return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinnerrow, parent, false);
TextView label = (TextView) findViewById(R.id.functie);
label.setText(jobs[position]);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
String uri = "@drawable/a" + jobsid[position];
int imageResource = getResources().getIdentifier
(
uri,
null,
getPackageName()
);
icon.setImageResource(imageResource);
if (visible)
{
icon.setVisibility(View.GONE);
}
return row;
}
}
这就是我希望它看起来像的样子: