You are here

Modify the main activity code

This task describes how to modify the main activity code.
In this task you will modify your main activity code to display the list view containing a list of nodes. Once a node is selected the main activity will create an intent, a message object containing the node selected, and start the node detail activity, sending it the constructed intent.
  1. In the Package Explorer expand the src folder and locate MainActivity.java.
  2. Double-click on the file MainActivity.java to load it into the Eclipse editor.
  3. Modify the code as follows:

    Attention: Unfortunately, you will have to edit your connection details again. Just edit the code to ensure that url, username and password are set correctly for your installation.
                  
    package com.alfresco.tutorials.testapp1;
    
    import java.util.List;
    import java.util.ArrayList;
    
    
    import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException;
    import org.alfresco.mobile.android.api.model.Folder;
    import org.alfresco.mobile.android.api.model.Node;
    import org.alfresco.mobile.android.api.model.Site;
    import org.alfresco.mobile.android.api.services.DocumentFolderService;
    import org.alfresco.mobile.android.api.services.SiteService;
    import org.alfresco.mobile.android.api.session.RepositorySession;
    
    import com.alfresco.tutorials.testapp1.NodeDetailActivity;
    import com.alfresco.tutorials.testapp1.R;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Toast;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.AdapterView;
    
    
    public class MainActivity extends Activity {
    
    	public final static String EXTRA_MESSAGE = "com.alfresco.tutorials.testapp1.MESSAGE";
    		
    	private NodeArrayAdapter adapter;
    	private ArrayList<Node> nodes;
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);       
            ListView lv = new ListView(this);
            
            nodes = new ArrayList<Node>();        
            adapter = new NodeArrayAdapter(this, nodes);
            lv.setAdapter(adapter);
            ll.addView(lv);
            adapter.setNotifyOnChange(true);
                    
            lv.setOnItemClickListener(new OnItemClickListener() {
    
      		  @Override
            	  public void onItemClick(AdapterView<?> parent, View view,
            	    int position, long id) {
            		      	    
            		Intent intent = new Intent(getApplicationContext(), NodeDetailActivity.class);	
            		
            		Node node = nodes.get(position);
            		      		
        			MessageObject message = new MessageObject(node);
            		intent.putExtra(EXTRA_MESSAGE, message);
            		startActivity(intent);
    
            	  }
            });
            
            
            // Modify to suit your SkyVault server installation 
            String url = "http://localhost:8080/alfresco";
            String username = "admin";
            String password = "admin";
    
            new ConnectToRepo().execute(url, username, password);
    
        }
        
    
        class ConnectToRepo extends AsyncTask<String, Integer, String> {
    
            private static final String TAG = "ConnectToRepo";
            private List<Node> nodes;
            
            @Override
            protected String doInBackground(String... params) {
    
                Log.d(TAG, "doInBackground");
                Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]);
    
                String url = params[0];
                String username = params[1];
                String password = params[2];
    
                // HelloRepo
                try {
                    // connect to on-premise repo
                   RepositorySession session = RepositorySession.connect(url,
                            username, password);
    
                        // Get site service
                        SiteService siteService = session.getServiceRegistry()
                                .getSiteService();
    
                        // Get sites for current user
                        List<Site> sites = siteService.getSites();
    
                        // Get first site
                        Site site = sites.get(0);
    
                        // Get site document library
                        Folder folder = siteService.getDocumentLibrary(site);
    
                        // Find DocumentFolderService
                        DocumentFolderService documentFolderService = session
                                .getServiceRegistry().getDocumentFolderService();
                        
                        // Get children of document library
                        nodes = documentFolderService
                                .getChildren(folder);
    
                } catch (AlfrescoSessionException e) {
                	Log.e(TAG, "Failed to connect: " + e.toString()); 
                	System.exit(0);
                }
    
                Log.d(TAG, "doInBackground Complete");
                return "doInBackground Complete";
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Log.d(TAG, "onPostExecute");
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            	adapter.addNodes((ArrayList<Node>) nodes);
            	adapter.notifyDataSetChanged();
            }
    
        }
    
    }              
                  
                

    The onCreate() method initially sets the layout, which was set to be a simple linear layout. This linear layout will be used as the container for the list view that is created from subsequent code. Once the linear layout has been located a node adapter and list view are created. The node adapter is added to the list view and the list view added to the linear layout.

    As it is necessary to detect when a node is selected from the list displayed in the list view, a list view click listener needs to be added to listen for click events. A click event handler method is attached to the listener. This onItemClick method creates a new intent, creates a message object, and then starts the node detail activity, sending the created intent, which fundamentally contains the node selected from the list view.

    It is important to point out the code in the onPostExecute method of the ConnectToRepo async task. The main code connects to the repository and obtains a list of nodes in a site. These are the nodes to display in the list view. Once this list of nodes has been obtained, the nodes can be added to the node adapter in the onPostExecute method. A notification is also generated to indicate that the adapter data has changed.
    Important: The addition of the nodes to the adapter and notification must be done in the main UI thread, rather than in the async task thread, hence their location in the onPostExecute method.
You have now completed the coding of the application and you are now ready to test it.