gridview setNotifiedDataSetChanged() not updating gridview smoothly?
I try to update gridview background color smoothly but it does not work somoothly. when i scrool gridview at that 开发者_如何学JAVAtime it get changed; plz guide me how to do it, below is my code, Thanks you.
TableGridViewAdapter gridadapter;
GridView gridview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textViewUname = (TextView) this.findViewById(R.id.userName);
textViewUname.setText("Welcome , " + HomeActivity.name);
gridview = (GridView) findViewById(R.id.gridview);
gridadapter=new TableGridViewAdapter(this);
gridview.setAdapter(gridadapter);
//adapter.notifyDataSetChanged();
UpdateGridview updateGridview=new UpdateGridview();
updateGridview.execute(TableActivity.this);
// code for onClick gridview items , It take to that Activity
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ConText.currentPos = position;
Intent i = new Intent(TableActivity.this,
OrderListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
});
}
}
}
here i tryed to refreshing view in every 20sec
private class UpdateGridview extends AsyncTask<Context, Integer, String>
{
@Override
protected String doInBackground( Context... params )
{
int i = 0;
while( i <10)
{
try{
gridadapter.notifyDataSetChanged();
// gridadapter.refreshItems(gridadapter);
gridview.invalidate();
//TableGridViewAdapter.refreshItems(gridadapter);
Thread.sleep( 20000 );
System.out.println("i called notifyDataSetChanged()=======================");
i++;
} catch( Exception e ){
Log.i("makemachine", e.getMessage() );
}
}
return "COMPLETE!";
}
// -- gets called just before thread begins
@Override
protected void onPreExecute()
{
Log.i( "makemachine", "onPreExecute()" );
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
}
// -- called if the cancel button is pressed
@Override
protected void onCancelled()
{
super.onCancelled();
Log.i( "makemachine", "onCancelled()" );
}
// -- called as soon as doInBackground method completes
// -- notice that the third param gets passed to this method
@Override
protected void onPostExecute( String result )
{
super.onPostExecute(result);
Log.i( "makemachine", "onPostExecute(): " + result );
}
}
}
Try like this:
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.arg1 == 1){
gridadapter.notifyDataSetChanged();
}
}
};
private class UpdateGridview extends AsyncTask<Context, Integer, String>
{
@Override
protected String doInBackground( Context... params )
{
int i = 0;
while( i <10)
{
try{
// gridadapter.notifyDataSetChanged();
// gridadapter.refreshItems(gridadapter);
//gridview.invalidate();
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
//TableGridViewAdapter.refreshItems(gridadapter);
Thread.sleep( 20000 );
System.out.println("i called notifyDataSetChanged()=======================");
i++;
} catch( Exception e ){
Log.i("makemachine", e.getMessage() );
}
}
return "COMPLETE!";
}
// -- gets called just before thread begins
@Override
protected void onPreExecute()
{
Log.i( "makemachine", "onPreExecute()" );
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
}
// -- called if the cancel button is pressed
@Override
protected void onCancelled()
{
super.onCancelled();
Log.i( "makemachine", "onCancelled()" );
}
// -- called as soon as doInBackground method completes
// -- notice that the third param gets passed to this method
@Override
protected void onPostExecute( String result )
{
super.onPostExecute(result);
Log.i( "makemachine", "onPostExecute(): " + result );
}
}
}
精彩评论