2014-03-30 4 views
0

나는 안드로이드 개발을 처음 사용합니다. 웹 서버에서 데이터를 검색하고 목록보기에서 업데이트하는 응용 프로그램을 만들려고합니다. 문제는 목록보기가 업데이트되지 않는 데이터베이스에 새 데이터가 입력 될 때마다입니다.안드로이드 - 데이터가 추가 될 때 목록보기 업데이트

데이터베이스에 새 항목이있는 경우 자동으로 목록을 업데이트하는 방법은 무엇입니까? 이것은 Sync Adapter을위한 이상적인 사용 사례처럼

public class NotificationTask extends ListActivity { 


UserFunctions userFunctions; 
Button btnLogout; 
DatabaseHandler dbHandler; 
private HashMap<String, String> user; 

SessionManager session; 

// Progress Dialog 
private ProgressDialog pDialog; 

// Creating JSON Parser object 
JSONParser jParser = new JSONParser(); 

ArrayList<HashMap<String, String>> productsList; 

// url to get all products list 
private static String url_all_products = "http://192.168.1.9/android_login_api/get_notifications.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_PRODUCTS = "notifications"; 
private static final String TAG_PID = "id"; 
private static final String TAG_NAME = "title"; 
private static final String TAG_DESCRIPTION = "discription"; 

// products JSONArray 
JSONArray products = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    session = new SessionManager(getApplicationContext());  

    userFunctions = new UserFunctions(); 
    if(userFunctions.isUserLoggedIn(getApplicationContext())){ 
    // user already logged in show databoard 
     setContentView(R.layout.notifications); 
     btnLogout = (Button) findViewById(R.id.btnlogout); 

     dbHandler = new DatabaseHandler(getApplicationContext()); 
     user = dbHandler.getUserDetails(); 
     TextView emailTextView = (TextView) findViewById(R.id.user); 
     emailTextView.setText(user.get("name")); 

    // Hashmap for ListView 
     productsList = new ArrayList<HashMap<String, String>>(); 

     // Loading products in Background Thread 
     new LoadAllProducts().execute(); 

     btnLogout.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       session.logoutUser();  
      } 
     }); 

    } 
     } 

     @Override 
     public void onBackPressed() { 
     Intent intent = new Intent(Intent.ACTION_MAIN); 
     intent.addCategory(Intent.CATEGORY_HOME); 
     startActivity(intent); 
     } 

/** 
* Background Async Task to Load all product by making HTTP Request 
* */ 
class LoadAllProducts extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(NotificationTask.this); 
     pDialog.setMessage("Loading products. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting All products from url 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url_all_products, params); 

     // Check your log cat for JSON reponse 
     Log.d("All Products: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // products found 
       // Getting Array of Products 
       products = json.getJSONArray(TAG_PRODUCTS); 

       // looping through All Products 
       for (int i = 0; i < products.length(); i++) { 
        JSONObject c = products.getJSONObject(i); 

        // Storing each json item in variable 
        String id = c.getString(TAG_PID); 
        String name = c.getString(TAG_NAME); 
        String description = c.getString(TAG_DESCRIPTION); 

        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put(TAG_PID, id); 
        map.put(TAG_NAME, name); 
        map.put(TAG_DESCRIPTION, description); 

        // adding HashList to ArrayList 
        productsList.add(map); 
       } 
      } else { 
       // no products found 
       // Launch Add New product Activity 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         NotificationTask.this, productsList, 
         R.layout.list_item, new String[] { TAG_PID, 
           TAG_NAME, TAG_DESCRIPTION}, 
         new int[] { R.id.pid, R.id.name, R.id.description }); 
       setListAdapter(adapter) 
    } 
} 

}

답변

관련 문제