我正在创建一个列表视图。我使用自定义适配器填充列表视图。在我的列表视图中有一个文本视图窗口小部件。当我重置该文本视图的值时,它显示正常。然后,向下滚动以获取新值到listView中。但是,当我向上滚动文本视图窗口小部件重置回旧的填充值。 我怎样才能永久保留我设定的新价值? 这里是我的列表视图适配器类的代码,我将值填充到我的列表视图。我点击列表视图中的imageview,触发事件将新的文本值设置为列表视图中的文本视图控件。滚动时Textview重置为旧值
private class MyListAdapter extends ArrayAdapter<CommentInfo> {
public MyListAdapter()
{
super(Comment.this, R.layout.listview_xml, myComments);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent)
{
itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.listview_xml, parent, false);
}
CommentInfo currentComment = myComments.get(position);
TextView tvLikes = (TextView) itemView.findViewById(R.id.tvLikes);
tvLikes.setText(currentComment.likes);
ImageView ivLikes = (ImageView) itemView.findViewById(R.id.likeBtn);
ivLikes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
likePosition = position;
TextView tvlikes1 = (TextView) convertView.findViewById(R.id.tvLikes);
// Here, i will get the correct value from DB and set it.
// I am setting 999 for sample purpose
tvlikes1.setText("999");
}
});
return itemView;
}
}
我在getView越来越convertView值()的说法在我上面的适配器类,并通过它访问我的ListView的TextView的部件重置ListView的文本视图的价值。然后,我将它设置为999(例如目的)。现在,我向下滚动以获取更多新的listView项目。但是,滚动新的值(999),我设置为旧项目消失。
请帮帮我吧。
为什么你必须使用findViewById()来找到你的TextView再次在OnClick()方法中。你可以重复使用电视机(尽管如此),而不是电视机1。 –
托尼,我试着用电视本身。是的,我不需要使用findViewById()。但它并不能解决我在文本视图窗口小部件中将新值重置为oldvalue的问题。 – Mat