3

I have implemented gallery and listview. Gallery has images and when i click on image using setOnItemSelectedListener Listview is updated which has info regarding image selected.

Problem: When I scroll through gallery rapidly it updates my listview rapidly. But according to me, listview should wait for gallery to stop moving on any One image and then update its contents.

Anyone know how to do it or add time between updation of listview ? Please help..

Rohit
  • 2,538
  • 6
  • 28
  • 41

3 Answers3

3

You can try setOnItemClickListener instead. It will be fired only when you actually "click" on image, not just select it.

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // Do what you want
        }
    });

Update:

If you want an action with delay, instead of onItemClickListener implementation, you have (at least) two choices. One is to run separate Thread with timeout (Thread.sleep()) and after it is done, you can use Handler to update your activity state. Another option is to use AsyncTask, that is technically the same approach, it only wraps Runnable task and Handler in one inner class. The simplest implementation might be like this:

    ListUpdater listUpdater; 

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {

    // We have to run this thread to add delay before updating activity views
    if (listUpdater != null) { // Cancel previous task
        listUpdater.cancel(true);
    }
    if (listUpdater == null || position != listUpdater.getPosition()) {
        listUpdater = null;
        listUpdater = new ListUpdater(position);

        listUpdater.execute();
    }
}

class ListUpdater extends AsyncTask<String, String, String> {
    private int position;

    public int getPosition() {
        return position;
    }

    public ListUpdater(int position) {
        this.position = position;
    }

    @Override
    protected String doInBackground(String... params) { 
        try {
            Thread.sleep(600); // set delay time here
        } catch (InterruptedException e) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (!isCancelled()) {
            // If another thread is not running, 
            //  that is if another item was not selected,
            //  update the activity 

            // Add code here to update the activity 
            // This method works in UI Thread, so you don't need to use Handler
            // For example:
            Toast.makeText(YourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
        super.onPostExecute(result);
    }
}

This is an example of implementation the delay. It is not necessary precise with your needs, but you can have some ideas from it how to make it.

morphium
  • 686
  • 1
  • 6
  • 13
  • my application need is that when image is selected(not click) it ll automatically update listview(like wallpaper selection in android). Also it is doing like wallpaper selection now. BUT I just want that it should wait for 1 sec before updating listview.(like it waits in wallpaper selection in android) – Rohit Nov 05 '11 at 08:46
  • I ll try it , and let you know .. Thank you :) – Rohit Nov 07 '11 at 06:07
  • just Edit constructor of ListUpdater class "CourseInformationUpdater" to "ListUpdater" – Rohit Nov 07 '11 at 06:39
0

Instead of implementing setOnItemSelectedListener, implement OnItemClickListener:

  Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • onItemClickListener is not fitting with my application needs.. while scrolling gallery images, it should update listview automatically. not by clicking image. But it should wait(may be 1sec) and then update listview not instantly. – Rohit Nov 05 '11 at 08:41
0

Got mine working after referring to morphium's solution using AsyncTask, However, I found problem with multiple caller (ex: OnHierachyChangeListener and OnItemSelectedListener). Where the second caller would cancel the first task, and would not start the new task if they holds the same position, causing the first update task to fail.

It works well with a

  1. A Gallery with BaseAdapter
  2. Filter inside BaseAdapter (Search for item)
  3. OnHierachyChangeListener (Add/Remove of item by the Filter class)
  4. OnItemSelectedListener (User Action)

And this is my version of code

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {

    // We have to run this thread to add delay before updating activity views
    if (listUpdater != null) { // Cancel previous task
        listUpdater.cancel(true);
    } 
    listUpdater = new ListUpdater(position);
    listUpdater.execute();
}

MY code simply stop the previous task, and begin a new one regardless of the previous and current position. To be safe, you can also make it a synchronized method

Orange
  • 185
  • 1
  • 13