You are here

Add a node adapter class

This task describes how to add a node adapter class.
In this task you will add a simple node adapter class to HelloRepo. This is required as the first change you will make is to display a list of nodes returned from the repository in a ListView UI component. List view controls receive the data they are to display from a piece of software called an adapter. There are standard adapter classes available to deal with data from common data structures such as string arrays. However, for flexibility, you will create a custom adapter class. So in this task you will develop a custom adapter class called the NodeAdapter.
  1. Right-click on the TestApp1 project in the Package Explorer window.
  2. Select New > Class.

    The New Java Class dialog is displayed.

  3. In the Package field click Browse. Select the package com.alfresco.tutorials.testapp1.
  4. In the Name text field, enter NodeArrayAdapter as the name of the class.
  5. In the Superclass field click Browse.

    This displays the Superclass Selection dialog.

  6. In the Choose a type text field, type ArrayAdapter.

    ArrayAdapter - Android.widget will appear in the matching items list.

  7. Select ArrayAdapter - Android.widget from the matching items list.
  8. Click OK.
  9. Click Finish.

    The new class is created. At the moment it will be showing errors you will now correct.

  10. Add the code for the NodeArrayAdapter as follows:

                  
    package com.alfresco.tutorials.testapp1;
    
    import java.util.ArrayList;
    
    import org.alfresco.mobile.android.api.model.Node;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    public class NodeArrayAdapter extends ArrayAdapter<String> {
    	
    	private Context context;
    	private ArrayList<Node> nodes;
    	private int count;
    	
    	public NodeArrayAdapter (Context context, ArrayList<Node> nodes){
    		super (context, android.R.layout.simple_list_item_1, android.R.id.text1, getValues(nodes));
    		this.context = context;
    		this.nodes = nodes;
    		this.count = this.nodes.size();
    	}
    	
    	
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    
    	    LayoutInflater inflater = (LayoutInflater) context
    	            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(android.R.layout.simple_list_item_1, null);
            TextView textView = (TextView) rowView.findViewById(android.R.id.text1);
            textView.setText(this.nodes.get(position).getName());
    		
    		return rowView;
    	}	
    
    	@Override
    	public int getCount() {
    		return this.count;
    	}
    
    
    	// returns list of strings that are node names
    	private static ArrayList<String> getValues(ArrayList<Node> nodes){
    		
    		ArrayList<String> values = new ArrayList<String>(); 
    		
    		for (Node node : nodes){
    			values.add(node.getName());
    		}
    		
    		return values;
    	}
    
    	public void addNodes (ArrayList<Node> nodes){		
    		this.nodes.addAll(nodes);
    		this.count = this.nodes.size();
    	}
    }
                  
                  
                

As can be seen from the code, the NodeArrayAdapter class extends the ArrayAdapter<String> class. The reason this is done is because a normal string array adapter only stores a list of strings. In this example it is more convenient to manage a list of nodes. Although in this example you just display strings (the name of the nodes) in the list view, using a node array adapter allows you to easily extend the list view to display additional node information besides just the name.

The other reason for using a custom adapter is because you will see that when an item is clicked in the list view, you are going to send the node information to a new activity that will display the node details. This is much easier to do where the list view adapter manages a list of nodes, rather than simply a list of strings.