- Right-click on the TestApp1 project in the Package Explorer window.
-
Select
.
The New Java Class dialog is displayed.
- In the Package field click Browse. Select the package com.alfresco.tutorials.testapp1.
- In the Name text field, enter NodeArrayAdapter as the name of the class.
-
In the Superclass field click
Browse.
This displays the Superclass Selection dialog.
-
In the Choose a type text field, type
ArrayAdapter.
ArrayAdapter - Android.widget will appear in the matching items list.
- Select ArrayAdapter - Android.widget from the matching items list.
- Click OK.
-
Click Finish.
The new class is created. At the moment it will be showing errors you will now correct.
-
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.