When the user clicks an item on the list view, I am changing the background color manually of the selected listView item. I plan on changing more than just the background, (eventually icons and more), therefore I need the listview to redraw itself.
The problem is when I notifyDataChanged nothing happens. Because of the Log's I placed in the functions, when the item is clicked, I can verify that onItemClick is triggered, and also that the notifyDataSetChanged call is made to the adapter.
I would expect that "getView" should be called again for each position, where the data gets linked to the views. But this is not occuring. There is no sign of of anything happening. What am I missing here?
class MyOnItemClickListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(final AdapterView<?> parent, View view,
final int position, long id) {
Log.i(TAG, "listview on click " + position);
MyListAdapter adapter = (MyListAdapter) parent.getAdapter();
adapter.mSelected = position;
adapter.notifyDataSetChanged();
}
}
class MyListAdapter extends BaseAdapter{
public List<String> mTxtContents;
public int mGroupCount;
public int mSelected;
public void setData(List<String> txtContents){
mTxtContents = txtContents;
mGroupCount = txtContents.size();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void notifyDataSetChanged() {
Log.i(TAG, "notify data set changed");
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
Log.i(TAG, "nva panel group count " + mGroupCount);
return mGroupCount;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "get view, " + position);
if(convertView == null){
LayoutInflater li = LayoutInflater.from(getActivity());
convertView = li.inflate(R.layout.lvitem_navpanel, parent, false);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv_txtData);
tv.setText(mTxtContents.get(position));
if(mSelected == position){
convertView.setBackgroundColor(Color.parseColor("#00FF00"));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
return convertView;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
}