2014-12-21 33 views
-1

我想用自己喜欢的选项做一个自定义列表视图,事情是,当我添加一些项目收藏夹,然后向下滚动fav按钮更改顺序随机,另一件事我需要帮助的是,我不知道如何改变喜爱的ImageView点击等时(因为你只能有1喜爱的选项)ImageView里面的项目在ListView中的变化的随机

这是我的自定义适配器

public class LineasAdapter extends BaseAdapter { 

Context context; 
protected List<Lineas> lineas; 
LayoutInflater inflater; 

public LineasAdapter(Context context, List<Lineas> lista){ 
    this.context = context; 
    this.inflater = LayoutInflater.from(context); 
    this.lineas = lista; 
} 

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

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

@Override 
public long getItemId(int position) { 
    return 0; 
} 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 

     holder = new ViewHolder(); 
     convertView = this.inflater.inflate(R.layout.item_lista, 
       parent, false); 

     holder.numero = (TextView) convertView 
       .findViewById(R.id.number); 
     holder.titulo = (TextView) convertView 
       .findViewById(R.id.titulo); 
     holder.subtitulo = (TextView) convertView 
       .findViewById(R.id.subtitulo); 
     holder.star = (ImageView) convertView 
       .findViewById(R.id.star); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 



    Lineas linea = lineas.get(position); 
    holder.numero.setText(linea.numero_s()); 
    holder.titulo.setText(linea.titulo()); 
    holder.subtitulo.setText(linea.subtitulo()); 

    final SharedPreferences.Editor editor = convertView.getContext().getSharedPreferences("Favorito",0).edit(); 
    final int numero = linea.numero(); 
    final Context c = convertView.getContext(); 
    final ImageView estrella = holder.star; 
/** this is where I change my fav button and add the number to some sharedpreferences **/ 
    holder.star.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      editor.putInt("linea", numero); 
      editor.commit(); 
      Toast.makeText(c,"Has agregado a favoritos a el cole numero "+String.valueOf(numero), Toast.LENGTH_SHORT).show(); 
      estrella.setImageResource(R.drawable.star); 
     } 
    }); 

    return convertView; 
} 

private class ViewHolder { 
    TextView numero; 
    TextView titulo; 
    TextView subtitulo; 
    ImageView star; 
} 
} 

回答

1

更换用follwing的getview方法...

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

    holder = new ViewHolder(); 
    convertView = this.inflater.inflate(R.layout.item_lista,parent, false); 

    holder.numero = (TextView) convertView.findViewById(R.id.number); 
    holder.titulo = (TextView) convertView.findViewById(R.id.titulo); 
    holder.subtitulo = (TextView) convertView.findViewById(R.id.subtitulo); 
    holder.star = (ImageView) convertView.findViewById(R.id.star); 
    convertView.setTag(holder); 

Lineas linea = lineas.get(position); 
holder.numero.setText(linea.numero_s()); 
holder.titulo.setText(linea.titulo()); 
holder.subtitulo.setText(linea.subtitulo()); 

final SharedPreferences.Editor editor =   convertView.getContext().getSharedPreferences("Favorito",0).edit(); 
final int numero = linea.numero(); 
final Context c = convertView.getContext(); 
final ImageView estrella = holder.star; 
/** this is where I change my fav button and add the number to some sharedpreferences **/ 
holder.star.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     editor.putInt("linea", numero); 
     editor.commit(); 
     Toast.makeText(c,"Has agregado a favoritos a el cole numero "+String.valueOf(numero),     Toast.LENGTH_SHORT).show(); 
     estrella.setImageResource(R.drawable.star); 
    } 
}); 

return convertView; 

}