0

I am writing a code in Android to refresh a Gridview in every 2 mins. I searched a lot in the web but i don't find any specific technique to do it.

Java Code

MainActivity.java

package com.example.mycustomgridrefresh;

import java.util.ArrayList;

import android.os.Bundle;
import android.widget.GridView;
import android.app.Activity;

public class MainActivity extends Activity
{     

GridView gd;

MyGrid mg;

ArrayList<String> abc;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    gd = (GridView)findViewById(R.id.gd);

    abc = new ArrayList<String>();

    for(int i=0;i<100;i++)
    {
        abc.add(String.valueOf(i));

    }

    mg = new MyGrid(this,this,abc);

    gd.setAdapter(mg);

}}

MyGrid.java

package com.example.mycustomgridrefresh;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

 public class MyGrid extends BaseAdapter
 {

ArrayList<String> abc;

Activity activity;

public MyGrid(Activity activity , Context cont,ArrayList<String> abc)
{

    super();

    this.abc = abc;

    this.activity = activity;

}

@Override
public int getCount() 
{
    return abc.size();
}

@Override
public Object getItem(int arg0) 
{

     return abc.get(arg0);
}

@Override
public long getItemId(int arg0) 
{

    return 0;


}

public class ViewHolder
{

    public TextView txt;

}


@Override
public View getView(int arg0, View arg1, ViewGroup arg2) 
{

    ViewHolder view;

    LayoutInflater inflator = activity.getLayoutInflater();

    if(arg1==null)
    {

        view = new ViewHolder();

        arg1 = inflator.inflate(R.layout.mygrid, null);

        view.txt = (TextView) arg1.findViewById(R.id.txt);

        arg1.setTag(view);

    }
    else
    {

        view = (ViewHolder) arg1.getTag();

    }

    view.txt.setText(abc.get(arg0));

    return arg1;

}


}

This above Grid is printing 100 numbers is 10 rows & columns. I want to change the value of the grid cells for each 2mins. The value should to change to +1.

Please suggest me some good solution.

Thanks in Advance !!!

Anshuman Pattnaik
  • 883
  • 3
  • 16
  • 37

1 Answers1

0

To refresh the values you need to call in MyGrid:

notifyDataSetChanged() 

To do any task each 2 mins you need to create other thread, something like this:

public void startRunnable(){
   Runnable r = new Runnable() {        
        @Override
        public void run() {
         while(needToRefresh){
            //add one...
            this.notifyDataSetChanged();
            Thread.sleep(2000);
         }
    }
    };

    this.activity.runOnUiThread(r);
}

Don't forget to stop the needToRefresh condition onPause, onStop.

EDIT: Because notifyDataSetChanged() need to be call in UI thread, you need to call runnable using this.activity.runOnUiThread See there: How to use notifyDataSetChanged() in thread

Community
  • 1
  • 1
RuiOliveiras
  • 89
  • 1
  • 8
  • For instance create a function in MyGrid: `startRunnable()` that will create a new runnable on a new thread. Than use it after `gd.setAdapter(mg);` – RuiOliveiras May 17 '14 at 22:12