如何在创建自定义微调器时实现getView

时间:2017-07-19 08:52:11

标签: java android adapter android-spinner

我希望自定义微调器具有图像和下拉列表中的textView,所以我创建了不同的布局并使其膨胀,并且它的工作正常但是在按下微调器之前用于微调器的布局我只想要一个像“选择”这样的文本从列表“但当我膨胀

  

android.R.layout.simple_spinner_item

对于getView,我只是得到一个下拉箭头而不是文本

如何在getView中填充布局,是否必须创建新数据集?下拉菜单不同?这是我的代码:我正在为自定义适配器实现SpinnerAdapter

 @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    convertView = inflater.inflate(R.layout.spinner_item, parent, false);
    ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
    TextView tv = (TextView) convertView.findViewById(R.id.tv1);

    iv.setImageResource(R.drawable.flower);
    tv.setText(itemList[position]);

    return convertView;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(android.R.layout.simple_spinner_item, 
                                  parent, false);

   return convertView;
}

2 个答案:

答案 0 :(得分:1)

在微调器适配器中声明ViewHolder类

# Testdata
output_a = ['1.1.2.9', '1.5.65.32']
output_b = ['1.2.57.1', '1.5.65.39']

# This will check if any subIP is in the list of other ip's and if not will append to result
# The rest I leave up to you ;)

# First solution
result = []
subIP = lambda x: ['.'.join(i.split('.')[:3]) for i in x]
for sub, full in zip(subIP(output_a), output_a):
    if not len([ip for ip in subIP(output_b) if sub == ip]):
        result.append(full)
print(result) 
# Will print: ['1.1.2.9']

# Second solution
# Some set operations will also work, but will leave you with a set of ip with 3 octets 
# afterwards you need to convert it back to full length if you want
print (set(subIP(output_a)) - set(subIP(output_b)))
# Will print {'1.1.2'}

并修改您的private static class ViewHolder{ TextView tv_itemname; } 方法,如下所示:

getView()

答案 1 :(得分:0)

text1TextView

android.R.layout.simple_spinner_item的标识符
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(android.R.layout.simple_spinner_item, 
                                  parent, false);

    TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);

    text1.setText(itemList[position]);

    return convertView;
}