I have a custom Listview with an ImageView for drawing. In my Mainactivity I start a thread which redraws the ImageView in my Listview every 20ms. The ImageView is only refreshed when I call adapter.notifyDataSetChanged(); in my Listfragment. This works fine, but my problem is, that onListItemClick only fires sometimes in this case. When I remove the adapter.notifyDataSetChanged(), onListItemClick fires always but now, my ImageViews are not refreshed.
Here the important parts of my code:
public class FragmentOscilloscope extends ListFragment
{
private ListViewAdapter adapter;
private List<ListViewItem> rowItems;
private Handler sampleUpdateHandler = null;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
sampleUpdateHandler = new Handler();
}
public void InitFragment()
{
adapter = new ListViewAdapter(getActivity(), rowItems);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Log.d("FragmentOscilloscope", "onListItemClick");
}
public void UpdateOscilloscope(final PositionMarker pos)
{
for (int i = 0; i < listItems; i++);
{
Canvas canvas = rowItems.get(i).getCanvas();
// do the drawings
}
sampleUpdateHandler.post(new Runnable()
{
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
}
}
This is my getView() in my ListViewAdapter:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ListViewItem row_pos = rowItem.get(position);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.oscilloscope_list_item, parent, false);
}
imageView = (ImageView) convertView.findViewById(R.id.osc_image);
imageView.setImageBitmap(row_pos.getBitmap());
row_pos.setImageView(imageView);
return convertView;
}
Can someone help me with this? I´m really frustrated... Thanks!
You can also find the full code of the described behavior here: Android ListFragment update/refresh and onItemClick