change first element in list
How I can change buttontext of the first element in listview?
View v = getListView().getChildAt(0 - yourListView.getFirstVisiblePosition());开发者_Go百科
Button Butt = (Button) v.findViewById(R.id.buttonLine);
Butt.setText("newtext");
This code crashes application.
Solved.
// 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;
}
This code sets buttons texts one by one. You must just set some condition with if (for example if package installed set "Uninstall", and if not set "Install") . With this code I can't set "second of first element". But I can add counter and check condition.
The code that you are passing to getChildAt(...) is always going to be negative, except when the position is 0. As a result, it will generally return null according to the documentation. That null value will then crash when trying to use it with null.findViewById(...).
Try removing the
"0 -"
I think I found the answer, but you'll have to test it.
RelativeLayout listItem = (RelativeLayout)getListView().getChildAt(getListView().getFirstVisiblePosition());
Button btn = (Button)listItem.findViewById(R.id.buttonLine);
btn.setText("Hi! I updated you manually!");
Basically, what you're getting from getListView().getChildAt() is the RelativeLayout object, defined in your XML as list item template. Then you get item number 2 from it - the third item defined, which is your Button.
精彩评论