微调使用文本和图标图标、文本和

2023-09-05 23:50:57 作者:各自飘零

在我的应用程序,我有一个微调,也可以填充两个阵列取值字符串,保存在我的价值/ strings.xml中资源。根据两个单选按钮的状态,从正确的数组中的值被选中,我的微调得到填补。

In my app, I have a Spinner, that can be filled with two Arrays of Strings, stored in my values/strings.xml resource. Depending on the state of two RadioButtons, the values from the correct Array is selected and my Spinner gets filled.

有关字符串的每个数组,我有一个具有相同大小的图标的数组。我所有的图标开始以A,并后跟一个数字。我不能改变这一点,它的存储一样,在数据库中。我在strings.xml中数组所有我需要绘制图标的数目。所有实际的图标可以在绘制中找到资源文件夹。

For each Array of Strings, I have an Array of Icons which have the same size. All of my icons start with an "A" and are followed by a number. I can't change this, it's stored like that in the database. I have an Array in Strings.xml with all the numbers I require to draw the icons. All the actual icons can be found in the drawable resource folder.

现在我想告诉从旁边这是由微调选择列表中的字符串的相应项目。当一个项目被选中,我只需要看到的文字。

Now I want to show the corresponding item next to the String from the list which was selected by the Spinner. When an item gets selected, I just need to see the text.

我在网上搜索了一个很好的教程,但我发现我还没有成功地做到了这一点。我刚刚开始在Android的编程,所以我想我需要一些帮助。我希望有人能指导我在正确的方向。

I have searched the Internet for a good tutorial, but with what I have found I have not succeeded in doing this. I'm just beginning in Android programming so I think I need a little help. I hope that somebody can guide me in the right direction.

我把我的字符串数组实际阵列内的,如:

I put my String Array within actual Arrays, such as:

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

在单选按钮中的一个被选中,我用下面的事件处理函数code:

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

这是我的 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);
}

我做的基础上的种本教程,但我纠结在里面......我需要一些帮助:

I've made a custom adapter based on this tutorial, but I'm kind of tangled in it... I need some help:

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

}

这就是我希望它看起来像:

This is what I would want it to look like:

推荐答案

您需要实现一个自定义的适配器和定义自定义布局列表项。 检查本教程。

You need to implement a custom Adapter and define a custom layout for the list items. Check this tutorial.

更新

请尝试以下。我还没有测试code,但我认为它应该这样的。

Try the following. I haven't tested the code but I think it should work like that.

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