0

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

Community
  • 1
  • 1
Sam
  • 139
  • 1
  • 12
  • Try calling `UpdateFragment` again after `adapter.notifyDataSetChanged();` – Shree Krishna Mar 20 '16 at 23:51
  • Same behaviour, only with much flickering in the drawings, because the adapter is created again and again... – Sam Mar 20 '16 at 23:59
  • have you tried putting adapter.notifyDataSetChanged() in the last line of UpdateFragment method and calling that method in That current thread where is currently `adapter.notifyDataSetChanged();` – Shree Krishna Mar 21 '16 at 00:04
  • I get a "ViewRootImpl$CalledFromWrongThreadException" then. – Sam Mar 21 '16 at 00:13
  • then remove thread `sampleUpdateHandler.post(new Runnable()` – Shree Krishna Mar 21 '16 at 00:19
  • I already tried this. My drawings are not updated when I remove the thread. – Sam Mar 21 '16 at 00:22
  • From where did you call `UpdateOscilloscope`? – Shree Krishna Mar 21 '16 at 00:24
  • `UpdateFragment` is also not a method of ListFragment to override. How did you use that ? – Shree Krishna Mar 21 '16 at 00:26
  • You are right. The UpdateFragment is from an Interface of me. So this is only called once and not periodically. – Sam Mar 21 '16 at 00:28
  • I call UpdateOscilloscope from another Thread which is updated every 20ms (like a Timer) – Sam Mar 21 '16 at 00:29
  • OK I will see this after 4 hrs, Till that time what I want to request you is, Edit that question and make clear by updating so that I can directly copy your codes and execute in my device. – Shree Krishna Mar 21 '16 at 00:38
  • Cool, thank you! You can get the full code here: http://stackoverflow.com/questions/28922305/android-listfragment-update-refresh-and-onitemclick It´s the same behaviour I described here and you can directly compile it with Android Studio. – Sam Mar 21 '16 at 00:43
  • Hey are you there ? I've fixed it but Can you say why are you using `while (true)`? – Shree Krishna Mar 21 '16 at 04:21
  • Thank you very much, but I still need an example how to do this. I tried many things, but it still doesn´t run correctly. Can I send you a link to the project?? – Sam Mar 22 '16 at 00:03
  • No that was of that another project link that you posted. The same issue of while loop which I've posted there. Isn't it a same problem ? – Shree Krishna Mar 22 '16 at 00:51
  • I am sorry for your thought, But removing or replacing while loop will work. But you told it's important. – Shree Krishna Mar 22 '16 at 00:52
  • Yes, it's the same problem. It's just a sample code that I created to show the problem in a simple way. Hm, OK. I removed the while loop but then the graphic is only updated once. Perhaps I'm doing something wrong... – Sam Mar 22 '16 at 00:56
  • have you tried creating a boolean and setting that false after updating the graphics and again setting that true , if true then again update drawing and set to false. something like that.. – Shree Krishna Mar 22 '16 at 01:02
  • Unfortunately it's the same. It works good, when I set Thread.sleep to a greater value (i.e. 1000ms). But I need the update faster. The cpu load is about 20% in my device with 20ms sleep. Perhaps there is no easy solution for this and I have to give some other way... – Sam Mar 22 '16 at 01:10
  • Yes thats so. As I explained in your another question, Every item that is being drawn or which are currently inside thread will not be clickable. You have to find another alternatives.. your another question is useful for others.. – Shree Krishna Mar 22 '16 at 01:24
  • Thank you, Shree! I will try to find a solution :-) – Sam Mar 22 '16 at 01:27
  • You are most welcome bro... Hope you will solve it soon.. – Shree Krishna Mar 22 '16 at 11:35

1 Answers1

1

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.

ThaiPD
  • 3,503
  • 3
  • 30
  • 48
  • Thank you, I edited my posting, you can see the getView() method now. I don´t do much there. The main work is done in the UpdateOscilloscope method (updating the canvas objects in the ListView). – Sam Mar 22 '16 at 00:00