自定义适配器微调器无法显示所有值

时间:2015-01-26 09:46:39

标签: android arrays custom-adapter

未找到错误,但结果显示在微调器的值上未完成。我从xml中的string-array中获取微调器的值,这个例子:

<string-array name="digit">
  <item>1</item>
  <item>2</item>
  <item>3</item>
  <item>4</item>
  <item>5</item>
  <item>6</item>
  <item>7</item>
  <item>8</item>
  <item>9</item>
  <item>10</item>
  <item>11</item>
  <item>12</item>
  <item>13</item>
  <item>14</item>
  <item>15</item>
  <item>16</item>
  <item>17</item>
  <item>18</item>
  <item>19</item>
</string-array>

我得到的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6

为什么看起来恢复初始值的结果? 请帮助检查错误,这个示例脚本:

public class CustomAdapterSpinner extends BaseAdapter {
    private Context ctx;
    private LayoutInflater layoutInflater;
    private List<String> data;

    public CustomAdapterSpinner(Context context, List<String> data) {
        this.ctx = context;
        this.data = data;
        layoutInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (view == null) {
            view = layoutInflater.inflate(R.layout.spinner_rows, parent, false);
            TextView textView = (TextView) view.findViewById(R.id.text1);
            ImageView imageView = (ImageView) view.findViewById(R.id.image1);
            imageView.getLayoutParams().height = 55;
            imageView.getLayoutParams().width = 55;
            imageView.setPadding(0, 10, 0, 10);

            textView.setText(data.get(position));
            textView.setPadding(10, 10, 0, 10);
            textView.setTypeface(null, Typeface.BOLD);
            textView.setTextSize(18);
            imageView.setImageResource(R.drawable.digit;

        return view;
    }

这是我放入myfragment

List<String> listStrings = Arrays.asList(getResources().getStringArray(R.array.digit));
CustomAdapterSpinner adapter = new CustomAdapterSpinner (getActivity(), listStrings);
spinner.setAdapter(adapter);

请帮助,非常感谢

1 个答案:

答案 0 :(得分:1)

利用视图持有人。

创建一个静态类

static class ViewHolder
{
    TextView textView;
    ImageView imageView;
}

在适配器类中添加此类,因此它必须是内部类。

然后按如下方式更改你的getView()方法。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (converView== null) {
        converView= layoutInflater.inflate(R.layout.spinner_rows, bull);
        viewHolder=new ViewHolder();
        viewHolder.textView = (TextView) view.findViewById(R.id.text1);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.image1);
        viewHolder.imageView.getLayoutParams().height = 55;
        viewHolder.imageView.getLayoutParams().width = 55;
        viewHolder.imageView.setPadding(0, 10, 0, 10);


        viewHolder.textView.setPadding(10, 10, 0, 10);
        viewHolder.textView.setTypeface(null, Typeface.BOLD);
        viewHolder.textView.setTextSize(18);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }
        viewHolder.textView.setText(data.get(position));
        imageView.setImageResource(R.drawable.digit;

      return convertView;
}

这是完全未经测试的代码。

希望这有帮助。