// -------------------------------------------------------------------------------------------------------------------------- private void showBeans() { // Read all the beans from the channel MobileBean[] beans = MobileBean.readAll("cloud_channel"); if (MobileBean.isBooted("cloud_channel")) { // Populate the List View ListView view = (ListView) findViewById(R.id.list); // Prepare the data for the adapter. Data is read from the ticket bean instances ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); if (beans != null && beans.length > 0) { for (MobileBean local : beans) { HashMap<String, String> map = new HashMap<String, String>(); String name = local.getValue("name"); String value = local.getValue("value"); if (name.length() > 25) { name = name.substring(0, 22) + "..."; } if (value.length() > 25) { value = value.substring(0, 22) + "..."; } map.put("name", name); map.put("value", value); mylist.add(map); } } SimpleAdapter beanAdapter = new SimpleAdapter( this, mylist, R.layout.bean_row, new String[] {"name", "value"}, new int[] {R.id.name, R.id.value}); view.setAdapter(beanAdapter); // List Listener...used to respond to selecting a ticket instance OnItemClickListener clickListener = new ClickListener(beans); view.setOnItemClickListener(clickListener); } else { // Tickets not found...put up a Sync in progress message and wait for data to be downloaded // from the Backend if (!HomeScreen.syncInProgress && !HomeScreen.syncComplete) { HomeScreen.syncInProgress = true; SyncInProgressAsyncTask task = new SyncInProgressAsyncTask(); task.execute(); } } }
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Get the ticket bean selected by the user int selectedIndex = position; final MobileBean selectedBean = activeBeans[selectedIndex]; String name = selectedBean.getValue("name"); String value = selectedBean.getValue("value"); AlertDialog.Builder builder = new AlertDialog.Builder(HomeScreen.this).setCancelable(false); View dialogView = LayoutInflater.from(HomeScreen.this).inflate(R.layout.dialog, null); // Setup the Name Value int nameEditPointer = R.id.nameEdit; final EditText nameEdit = (EditText) dialogView.findViewById(nameEditPointer); nameEdit.setText(name); // Setup the Value Value int valueEditPointer = R.id.valueEdit; final EditText valueEdit = (EditText) dialogView.findViewById(valueEditPointer); valueEdit.setText(value); // Setup the buttons on the dialog box builder.setPositiveButton( "Update", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String name = nameEdit.getText().toString(); String value = valueEdit.getText().toString(); if (name == null || name.trim().length() == 0) { Toast.makeText(HomeScreen.this, "'Name' is required", 1).show(); return; } if (value == null || value.trim().length() == 0) { Toast.makeText(HomeScreen.this, "'Value' is required", 1).show(); return; } selectedBean.setValue("name", name); selectedBean.setValue("value", value); try { selectedBean.save(); } catch (CommitException se) { try { selectedBean.refresh(); selectedBean.setValue("name", name); selectedBean.setValue("value", value); selectedBean.save(); } catch (Exception e) { // we tried, put up an error message Toast.makeText(HomeScreen.this, "Error", 1).show(); } } showBeans(); } }); builder.setNegativeButton( "Delete", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { try { selectedBean.delete(); } catch (CommitException se) { Toast.makeText(HomeScreen.this, "Error : " + se.getMessage(), 1).show(); return; } showBeans(); } }); builder.setNeutralButton( "Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { showBeans(); } }); AlertDialog beanDialog = builder.create(); beanDialog.setView(dialogView); beanDialog.show(); }