package com.bruno.cloudclient;import java.io.IOException;import com.bruno.cloudclient.R;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.content.SharedPreferences;import android.graphics.LightingColorFilter;import android.os.Bundle;import android.view.Gravity;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class CloudClientActivity extends Activity { /** Called when the activity is first created. */ ArrayAdapter<String> adapter = null; // String listName; SharedPreferences settings; /*************************************************************** * Purpose - Startup routine. Configures view and accesses * xml file that is your view. Set the file system * sharing mode. ****************************************************************/ @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Set file sharing mode to private settings = getPreferences(Context.MODE_PRIVATE); listName = settings.getString("listName", "Bruno"); // Get some data. Create an adapter and attach to our listview // control. final Data data = new Data(); // This is a key call. It retrieves the dat from the web service. // The data is in JSON originally, then converted to simple strings. adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listItem, data.getItems(listName)); // Attach the data to the ListView final ListView myList = (ListView) findViewById(R.id.list); myList.setAdapter(adapter); // Create an "Add" button. This button will allow users to // add data stored in web service. Button btnAdd = (Button) findViewById(R.id.btnAdd); // Manage button color for a black screen btnAdd.getBackground().setColorFilter( new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000)); // Add a textbox the user can use to enter new data to be added // to the web service. final EditText txtItem = (EditText) findViewById(R.id.txtItem); // Setup the button listener btnAdd.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { settings = getPreferences(Context.MODE_PRIVATE); listName = settings.getString("listName", "Bruno"); // Passing true to modifyData, which will add a new record // in the cloud data.modifyItem(listName, txtItem.getText().toString(), true); // Clear the textbox txtItem.setText(""); try { refresh(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // If you click on a ListItem in the ListBox you will // pass "false" to modifyItem() which will delete // a record in the cloud myList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> a, View v, int position, long id) { String s = myList.getItemAtPosition(position).toString(); settings = getPreferences(Context.MODE_PRIVATE); listName = settings.getString("listName", "Bruno"); data.modifyItem(listName, s, false); try { refresh(); // Refresh the listbox to reflect the deleted entry } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Notify user that record was just deleted makeToast(s); } }); } catch (Exception e) { e.printStackTrace(); } } /*************************************************************** * Purpose - Notify user that we just deleted a record * Pops a little message ****************************************************************/ public void makeToast(String item) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.icon); TextView text = (TextView) layout.findViewById(R.id.text); text.setText(item + " deleted."); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } /*************************************************************** * Purpose - Get latest data. Fill the view, attach view and * adapter, tell adapter to refresh view. ****************************************************************/ public void refresh() throws IOException { Data d = new Data(); settings = getPreferences(Context.MODE_PRIVATE); listName = settings.getString("listName", "Bruno"); adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listItem, d.getItems(listName)); // Attach adapter and view, then refresh with the // notifyDataSetChanged event ListView myList = (ListView) findViewById(R.id.list); myList.setAdapter(adapter); adapter.notifyDataSetChanged(); }}
|