UPDATE: I've decided to use the ExpandableListAdapter to accomplish this ...as this code is almost effectively an attempt to engineer what expandable listadapter already does.
I'm using the standard pattern of ViewHolder in order to make ListView simply work...
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if( convertView == null ){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.expandButton=(ImageView)vi.findViewById(R.id.expand_button);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
However....I believe that inside of my onClickListener...something is going wrong.
final String textData = texts[position];
final ViewHolder holderFinal = holder;
holder.expandButton.setVisibility(View.VISIBLE);
holder.expandButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v) {
holderFinal.expandButton.setImageResource(R.drawable.expander_ic_maximized);
holderFinal.text.setText(textData);
So, everything displays fine when scrolling back and forth....however, whenever one of the listitems is expanded, the final line above开发者_如何转开发 contains a textData that belongs to another row. How do I get the correct textData to work INSIDE the onCLick listener?
i was experimented the same issue, i resolve this problem doing each item object of the listview a static class, like this:
all the data necesary to display a row in the ListView i defined in a static class like this:
static class DataHolder{
Drawable extIcon;
String text;
String quantity_text;
Drawable icon;
int progressBarVisible;
int quantityTextVisible;
}
then to create all rows i make this:
ArrayList<DataHolder> data = new ArrayList<DataHolder>();
for ( int i=0 ; i < activitys.length ; i++ ) {
DataHolder item = new DataHolder();
item.icon = icons[i]; //an icons array
item.quantity_text = ""; //some text
item.progressBarVisible = View.GONE; //a status for a progress bar
//other data setting
}
then my ArrayAdapter subclass:
class ActivityList extends ArrayAdapter<DataHolder>{
public ActivityList(Context context, int textViewResourceId, ArrayList<DataHolder> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(HomeActivity.this);
convertView = inflater.inflate(R.layout.home_activity_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView)convertView.findViewById(R.id.homeActivityText);
holder.icon = (ImageView)convertView.findViewById(R.id.homeActivityIcon);
holder.progressBar = (ProgressBar)convertView.findViewById(R.id.homeActivityQuantityPBar);
holder.quantity_text = (TextView)convertView.findViewById(R.id.homeActivityQuantity);
holder.extIcon = (ImageView)convertView.findViewById(R.id.homeActivityIconExtended);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getItem(position).text);
holder.icon.setImageDrawable( getItem(position).icon );
holder.quantity_text.setVisibility( getItem(position).quantityTextVisible );
holder.quantity_text.setText( getItem(position).quantity_text );
holder.progressBar.setVisibility( getItem(position).progressBarVisible );
holder.extIcon.setImageDrawable( getItem(position).extIcon );
return convertView;
}
}
finally i dont have a button on my list row view but i can do any operation on various context in this way, obtaining always the exact text. (the listView field is the instance of ActivityList) (SAVED_SEARCH is the list item position)
((ActivityList)listView.getAdapter()).getItem(SAVED_SEARCHED).progressBarVisible = View.GONE;
((ActivityList)listView.getAdapter()).getItem(SAVED_SEARCHED).quantity_text = "any text";
((ActivityList)listView.getAdapter()).notifyDataSetChanged();
sincerally hope this help you.
Try postInvalidate()ing the ViewHolder after click.
Hi look at my solution on a similar problem:
- Change ListView background - strange behaviour
I'm using there the holder pattern too
initialize int GlobalPostion as a global variable
GlobalPosition = position
holder.expandButton.setOnClickListener(new View.OnClickListener() int localPosition = GlobalPosition @Override public void onClick(View v) {
holderFinal.expandButton.setImageResource(R.drawable.expander_ic_maximized); holderFinal.text.setText(""+texts[localPosition]); }
The problem is you are not getting the exact list position , if you are storing it in a global variable an then move to a local variable inside OnClick you can retain the position. I hope this will help you
精彩评论