有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

listview更改中项目内部的java ImageView

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

这是我的自定义适配器

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

  1. # 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;
    

    }