gpt4 book ai didi

android - ListView 中的错误。当我滚动 ListView 时, ImageView 会自行更改。我的错误是什么?

转载 作者:行者123 更新时间:2023-11-30 03:29:46 26 4
gpt4 key购买 nike

在我的getView方法中:

    public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.linha_historico_layout, parent, false);
}

DadosHistorico dadosHistorico = (DadosHistorico) getItem(index);

TextView tvdata = (TextView) view.findViewById(R.id.tvData);
tvdata.setText(dadosHistorico.getData());

TextView ivstatushistorico = (TextView) view.findViewById(R.id.tvStatushistorico);
ivstatushistorico.setText(dadosHistorico.getStatus());

TextView tvInternetTitlo = (TextView) view.findViewById(R.id.tvInternetTitulo);
tvInternetTitlo.setText(dadosHistorico.getInternet());

TextView tvLigacaoTitulo = (TextView) view.findViewById(R.id.tvLigacaoTitulo);
tvLigacaoTitulo.setText(dadosHistorico.getLigacao());

TextView tvInternetDescricao = (TextView) view.findViewById(R.id.tvInternetDescricaoHistorico);
tvInternetDescricao.setText(dadosHistorico.getInternetDescricao());

TextView tvLigacaiDescricao = (TextView) view.findViewById(R.id.tvLigacaoDescricaoHistorico);
tvLigacaiDescricao.setText(dadosHistorico.getLigacaoDescricao());

TextView tvComentarioHistorico = (TextView)view.findViewById(R.id.tvComentarioHistorico);
tvComentarioHistorico.setText(dadosHistorico.getComentario());

final String comentario = tvComentarioHistorico.getText().toString();

ImageView ivStatusLed = (ImageView) view.findViewById(R.id.ivStatusLe);


if(ivstatushistorico.getText().equals("1")){
ivStatusLed.setBackgroundResource(R.drawable.green_led);
ivStatusLed.setVisibility(View.VISIBLE);
}


ImageView ibComentarioHistorico = (ImageView)view.findViewById(R.id.ibComentarioHistorico);

if(comentario.equals("")){
ibComentarioHistorico.setVisibility(View.INVISIBLE);
}else{
ibComentarioHistorico.setVisibility(View.VISIBLE);

}


ibComentarioHistorico.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(c).create();
alertDialog.setTitle("Comentário");
alertDialog.setMessage(comentario);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});

alertDialog.show();


}
});


return view;


}

public void addDados(String data, String status, String internet, String internetDescricao, String ligacao, String ligacaoDescricao, String comentario) {
dados.add(new DadosHistorico(data, status, internet, internetDescricao, ligacao, ligacaoDescricao, comentario));

}
}

在这段代码中,我更改了列表中的 ImageView 。我从数据库接收到一些数据并更改 ImageView 。问题是,当我向下滚动 ListView 并向后滚动时, ImageView 不同,有时是 gream_led,有时 ibComentarioHistorico 消失。

最佳答案

添加一个else语句:

if(ivstatushistorico.getText().equals("1")){
ivStatusLed.setBackgroundResource(R.drawable.green_led);
ivStatusLed.setVisibility(View.VISIBLE);
}
else {
ivStatusLed.setBackgroundResource(R.drawable.your_default);
ivStatusLed.setVisibility(View.INVISIBLE); // or whatever the default is
}

View 会被重用,因此除非您告诉它在它不等于“1”时该怎么做,否则您将看到您所描述的行为。

关于android - ListView 中的错误。当我滚动 ListView 时, ImageView 会自行更改。我的错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17654432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com