listview中的项目内的ImageView随机变化

时间:2014-12-21 22:53:13

标签: java android listview android-adapter baseadapter

我正在尝试使用喜欢的选项进行自定义列表视图,问题是当我向收藏夹添加一些项目然后随机向下滚动收藏夹按钮更改顺序时,我需要的另一件事帮助是我不知道如何在点击其他时更改喜欢的imageview(因为你只能有1个喜欢的选项)

这是我的自定义适配器

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 个答案:

答案 0 :(得分:1)

用下面的方法替换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;

}