带有文本和图标的微调器

Han*_*ore 3 icons android text spinner

在我的应用程序中,我有一个Spinner, 可以用两个Arrays填充Strings,存储在我的values/strings.xml资源中。根据两个 RadioButton 的状态,选择正确数组中的值并填充我的 Spinner。

对于每个字符串数组,我有一个大小相同的图标数组。我所有的图标都以“A”开头,后跟一个数字。我无法更改它,它像那样存储在数据库中。我在 Strings.xml 中有一个数组,其中包含绘制图标所需的所有数字。所有实际的图标都可以在drawable资源文件夹中找到。

现在我想在 Spinner 选择的列表中的字符串旁边显示相应的项目。当一个项目被选中时,我只需要看到文本。

我在互联网上搜索了一个很好的教程,但是根据我的发现,我没有成功地做到这一点。我刚刚开始使用 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);
Run Code Online (Sandbox Code Playgroud)

When one of the RadioButtons gets selected, I use the following event handler code:

// 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
Run Code Online (Sandbox Code Playgroud)

这是我的showSpinner方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

我已经根据本教程制作了一个自定义适配器,但我有点纠结……我需要一些帮助:

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

}
Run Code Online (Sandbox Code Playgroud)

这就是我希望它的样子:

结果

Flo*_*Flo 5

您需要实现自定义适配器并为列表项定义自定义布局。检查本教程

更新

请尝试以下操作。我还没有测试代码,但我认为它应该像那样工作。

public class MyCustomAdapter extends ArrayAdapter<String>{

    private String[] mIcons;

    public MyCustomAdapter(Context context, int textViewResourceId,
    String[] objects, String[] icons) {
        super(context, textViewResourceId, objects);
        mIcons = icons;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

       LayoutInflater inflater=getLayoutInflater();
       View row=inflater.inflate(R.layout.spinnerrow, parent, false);
       TextView label=(TextView) findViewById(R.id.functie);
       label.setText(getItem(position);

       ImageView icon=(ImageView)row.findViewById(R.id.icon);

       String uri = "@drawable/a" + mIcons[position];
       int imageResource = getResources().getIdentifier(uri, null, getPackageName());
       icon.setImageResource(imageResource);

       return row;
    }
}
Run Code Online (Sandbox Code Playgroud)