如何更改列表视图中第一个元素的按钮文本?
View v = getListView().getChildAt(0 - yourListView.getFirstVisiblePosition());
Button Butt = (Button) v.findViewById(R.id.buttonLine);
Butt.setText("newtext");
此代码使应用程序崩溃。
最佳答案
解决了。
// 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.adaptor_content, null);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.buttonLine = (Button) convertView.findViewById(R.id.buttonLine);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
} holder.buttonLine.setText("Changed text of button");
/////////////
static class ViewHolder {
TextView textLine;
ImageView iconLine;
Button buttonLine;
TextView textTwo;
}
此代码一个接一个地设置按钮文本。您必须只使用if设置一些条件(例如,如果已安装的软件包设置为“卸载”,而未设置为“安装”)
。使用此代码,我无法设置“第一个元素的第二个”。但是我可以添加计数器并检查条件。