2014-04-19 90 views
0

我有一個微調在從數據庫中取值的頁面。如果我進入下一頁,然後按下後退按鈕,我將登錄到包含微調器的頁面。當我在另一頁上,我的數據庫得到更新時,只要我回到包含微調器的頁面,我就想在微調器上反映它。 我正在使用php mysql連接數據庫。 我用的是:微調值沒有得到更新onRestart()

public void onRestart(){ 
    super.onRestart(); 
    adapter.notifyDataSetChanged(); 
} 

如果適配器是陣列適配器設置微調值。 我在包含微調器的頁面上編寫了這段代碼。

上面的代碼不會更新微調....請幫助....

+1

你做了什麼,以獲得其他頁面上的最新值?可能你應該這樣做來更新包含頁面的微調框。只需adapter.notifyDataSetChanged();單獨不會幫助你。您應該將該值添加到列表中。 – itsrajesh4uguys

回答

0

嘗試恢復這樣

public void onResume(){ 
    super.onResume(); 
    adapter.notifyDataSetChanged(); 
} 

希望這將有助於

編輯

如果它不幫助嘗試記錄onresume()

我想問題是關於適配器,你想刷新適配器。

i谷歌文檔約notifyDataSetChanged():通知附加的觀察員,基礎數據已被更改,任何視圖反映數據集應刷新本身

Link到同樣的問題

+0

也嘗試過......它不工作 – ganapathy

0
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; 
     } 

    } 

}