Using handler you can update the data frquently
final Handler handler = new Handler()
handler.postDelayed(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
handler.postDelayed(this, 60 * 1000);
}
}, 60 * 1000);
這是我的全部活動及其工作正常
package com.androidhive.dashboard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import androidhive.dashboard.R;
public class EventActivity extends Activity
{
Boolean isInternetPresent = false;
InternetConnection cd;
public EditText uname,pwd;
public Button btnlog1,btncancel1;
public boolean connect=false,logged=false;
public String db_select;
String mUname;
ListView l1,l2;
Spinner s;
public Iterator<String> itr,itr1;
private final String SERVICE_URL = AddIp.Address+"/Events";
private final String TAG = "Event";
ArrayList<String> todoItems,cad;
ArrayAdapter<String> aa,a1;
public List<String> list1=new ArrayList<String>();
public List<String> list12=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.events);
l1 = (ListView)findViewById(R.id.listView1);
l2 = (ListView)findViewById(R.id.listView2);
todoItems = new ArrayList<String>();
cad=new ArrayList<String>();
aa = new ArrayAdapter<String>(this,R.layout.mylist,R.id.list_content,todoItems);
l1.setAdapter(aa);
a1 = new ArrayAdapter<String>(this,R.layout.mylist,R.id.list_content,cad);
l2.setAdapter(a1);
todoItems.clear();
cad.clear();
cd = new InternetConnection(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent)
{
try
{
validat_user();
final Handler handler = new Handler()
handler.postDelayed(new Runnable() {
@Override
public void run() {
validat_user();
handler.postDelayed(this, 60 * 1000);
}
}, 60 * 1000);
}
catch(Exception e)
{
display("Network error.\nPlease check with your network settings.");
}
}
else
{
display("No Internet Connection");
}
}
public void display(String msg)
{
Toast.makeText(EventActivity.this, msg, Toast.LENGTH_LONG).show();
}
private void validat_user()
{
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, this, "Authentication in progress...");
// wst.addNameValuePair("Emp_PWD", stg2);
//db_select="";
wst.execute(new String[] { SERVICE_URL });
}
@SuppressWarnings("deprecation")
public void no_net()
{
display("No Network Connection");
final AlertDialog alertDialog = new AlertDialog.Builder(EventActivity.this).create();
alertDialog.setTitle("No Internet Connection");
alertDialog.setMessage("You don't have internet connection.\nElse please check the Internet Connection Settings.");
//alertDialog.setIcon(R.drawable.error_info);
alertDialog.setCancelable(false);
alertDialog.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.cancel();
EventActivity.this.finish();
System.exit(0);
}
});
alertDialog.setButton2("Use Local DataBase", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
display("Accessing local DataBase.....");
alertDialog.cancel();
}
});
alertDialog.show();
}
private class WebServiceTask extends AsyncTask<String, Integer, String> {
public static final int POST_TASK = 1;
private static final String TAG = "WebServiceTask";
// connection timeout, in milliseconds (waiting to connect)
private static final int CONN_TIMEOUT = 3000;
// socket timeout, in milliseconds (waiting for data)
private static final int SOCKET_TIMEOUT = 5000;
private int taskType = POST_TASK;
private Context mContext = null;
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
public WebServiceTask(int taskType, Context mContext, String processMessage) {
this.taskType = taskType;
this.mContext = mContext;
}
public void addNameValuePair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
protected String doInBackground(String... urls) {
String url = urls[0];
String result = "";
HttpResponse response = doResponse(url);
if (response == null) {
return result;
} else {
try {
result = inputStreamToString(response.getEntity().getContent());
} catch (IllegalStateException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return result;
}
@Override
protected void onPostExecute(String response) {
handleResponse(response);
}
// Establish connection and socket (data retrieval) timeouts
private HttpParams getHttpParams() {
HttpParams htpp = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);
return htpp;
}
private HttpResponse doResponse(String url) {
// Use our connection and data timeouts as parameters for our
// DefaultHttpClient
HttpClient httpclient = new DefaultHttpClient(getHttpParams());
HttpResponse response = null;
try {
switch (taskType) {
case POST_TASK:
HttpPost httppost = new HttpPost(url);
// Add parameters
httppost.setEntity(new UrlEncodedFormEntity(params));
response = httpclient.execute(httppost);
break;
}
} catch (Exception e) {
display("Remote DataBase can not be connected.\nPlease check network connection.");
Log.e(TAG, e.getLocalizedMessage(), e);
return null;
}
return response;
}
private String inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
// Return full string
return total.toString();
}
}
public void handleResponse(String response)
{ //display("JSON responce is : "+response);
try {
JSONObject jso = new JSONObject(response);
String UName = "hi";
if(UName.equalsIgnoreCase("") || UName==null)
{
display("Project report(s) can not be retrieved.");
}
else
{
// int count=Integer.parseInt(UName);
// display("Number of Projects have been handling in AFL right now: "+count);
list1=new ArrayList<String>();
list12=new ArrayList<String>();
JSONArray array=jso.getJSONArray("sun");
JSONArray array1=jso.getJSONArray("sun1");
for(int i=0;i<array.length();i++)
{
list1.add(array.getJSONObject(i).getString("branchName1"));
list12.add(array1.getJSONObject(i).getString("branchName2"));
}
itr=list1.iterator();
itr1=list12.iterator();
while(itr.hasNext()&&itr1.hasNext())
{
//str1=itr.next()+"\n";
todoItems.add(0, itr.next().toString());
cad.add(0, itr1.next().toString());
aa.notifyDataSetChanged();
a1.notifyDataSetChanged();
}
//tv1.setText(str1);
}
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage(), e);
return;
}
}
}
你做了什麼,以獲得其他頁面上的最新值?可能你應該這樣做來更新包含頁面的微調框。只需adapter.notifyDataSetChanged();單獨不會幫助你。您應該將該值添加到列表中。 – itsrajesh4uguys