更改列表视图中行上按钮的可见性

时间:2013-08-14 09:39:38

标签: android android-listview visibility baseadapter android-adapter

我正在使用带有扩展BaseAdapter的适配器的listview。 列表视图中的每一行都有多个视图,我希望如果变量的值为“是”,则该行的图片可见,如果值为“否”则消失。

但我没有成功。

在改变可见性时我被强制关闭

这是我的代码,非常感谢任何帮助。

谢谢和问候

public class Adapter_c extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        View vi=convertView;

        if(convertView==null)
            vi = inflater.inflate(R.layout.row_concursos, null);
        holder = new ViewHolder();
        holder.title = (TextView)vi.findViewById(R.id.title_c);
        holder.image = (ImageView)vi.findViewById(R.id.image_c);
        holder.button = (Button)vi.findViewById(R.id.button_c);



        HashMap<String, String> items = new HashMap<String, String>();
        items = data.get(position);

        try { 
            String imageV = new String(items.get(FIRST_activity.KEY_STATE).getBytes("ISO-8859-1"));
            if (imageV.equals("active")){

                holder.image.setVisibility(View.VISIBLE);
            } else{

                holder.image.setVisibility(View.GONE);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();}
...

3 个答案:

答案 0 :(得分:0)

尝试在没有ViewHolder的情况下获得结果。

(ImageView)vi.findViewById(R.id.image_c).setVisibility(View.GONE);

答案 1 :(得分:0)

更改这一堆代码:

if(convertView==null)
        vi = inflater.inflate(R.layout.row_concursos, null);
    holder = new ViewHolder();
    holder.title = (TextView)vi.findViewById(R.id.title_c);
    holder.image = (ImageView)vi.findViewById(R.id.image_c);
    holder.button = (Button)vi.findViewById(R.id.button_c);

像这样:

if(convertView==null)
{
    vi = inflater.inflate(R.layout.row_concursos, null);
    holder = new ViewHolder();
    holder.title = (TextView)vi.findViewById(R.id.title_c);
    holder.image = (ImageView)vi.findViewById(R.id.image_c);
    holder.button = (Button)vi.findViewById(R.id.button_c);        

    vi .setTag(holder);
}
else
    holder = (ViewHolder) vi.getTag();

答案 2 :(得分:0)

试试这个

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;


    if(convertView==null)
    {
    convertView= inflater.inflate(R.layout.row_concursos, parent,false);
    holder = new ViewHolder();
    holder.title = (TextView)convertView.findViewById(R.id.title_c);
    holder.image = (ImageView)convertView.findViewById(R.id.image_c);
    holder.button = (Button)convertView.findViewById(R.id.button_c);
    convertView.setTag(holder);
    }
    else{
     holder=(ViewHolder)convertView.getTag();
     }
     ....some code....
       try { 
          String imageV = new String(items.get(FIRST_activity.KEY_STATE).getBytes("ISO-8859-1"));
          if(imageV.equals("active")){

             holder.image.setVisibility(View.VISIBLE);
           } else{

              holder.image.setVisibility(View.GONE);
           }

         } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
          }
      return converView;
    }