gpt4 book ai didi

java - 更新自定义 ListView 项目

转载 作者:行者123 更新时间:2023-12-01 15:58:28 27 4
gpt4 key购买 nike

我有一个 ListView ,它使用基于基本适配器的自定义适配器。 ListView 使用外部数据填充正常,我可以拾取单击事件并知道已选择哪个项目。

我在更新所单击项目的 View (例如 TextView 和 ViewFlipper)时遇到问题。

我是否必须通过 ListView 更新某些内容,还是通过适配器更新某些内容。我尝试过类似以下的事情;

View test = (View)adapter.getView(pos, null, myListView);
ViewFlipper temp = (ViewFlipper)test.findViewById(R.id.flipholder);
temp.showNext();
TextView temp2 = (TextView)test.findViewById(R.id.filmtitle);
temp2.setText("Hello World");

这会导致 View 翻转器翻转第一个和第三个项目或第二个和第四个项目,并且文本根本不更新。

有什么想法吗?

干杯

最佳答案

在 getView 中尝试此模式:

public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;

// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}

// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

return convertView;
}

static class ViewHolder {
TextView text;
ImageView icon;
}
}

关于java - 更新自定义 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527749/

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