Sam
Sam

Reputation: 181

Android onListItemClick fires only sometimes

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

Upvotes: 0

Views: 98

Answers (1)

ThaiPD
ThaiPD

Reputation: 3751

When I remove the adapter.notifyDataSetChanged(), onListItemClick fires always but now, my ImageViews are not refreshed.

when you call notifyDataSetChanged(), items in your listview will be init and draw again. The main cause the onListItemClick fires sometimes because at that time your UI thread was VERY BUSY, it's processing other tasks and onListItemClick command will be put on the task queue to process.

I guess that in the getView() from adapter you do very heavy tasks, Try to improve it or create Thread/AsynTask for heavy processes. Hope it help.

Any way, if you provide more details in your code (getView() is a good point) I think some guys can help a lot.

Upvotes: 1

Related Questions