0

I am fairly new to Java programming and was following a tutorial located here: http://coenraets.org/blog/android-samples/androidtutorial/ Where I copied the code I am having a problem with here: http://code.google.com/p/androidtutorial/source/browse/trunk/%20androidtutorial/EmployeeDirectory6/src/samples/employeedirectory/EmployeeDetails.java

EDIT: Thank You all. Thanks to @adamcodes for pointing out I totally missed that link where he put out the source code. It looks like he forgot to include that link in the step by step tutorial.

At about 25 lines down I'm getting an error at

protected ArrayList<EmployeeAction> actions;

which says "EmployeeAction cannot be resolved to a type" My question is does the class EmployeeAction have to be created

actions = new ArrayList<EmployeeAction>();

Even if I put "actions = new ArrayList();" in my code? If so what should the class contain?

package samples.employeedirectory;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EmployeeDetails extends ListActivity {

protected TextView employeeNameText;
protected TextView titleText;
protected ArrayList<EmployeeAction> actions;
protected EmployeeActionAdapter adapter;
protected int employeeId;
protected int managerId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.employee_details);

    employeeId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
    SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
                            new String[]{""+employeeId});

    if (cursor.getCount() == 1)
    {
            cursor.moveToFirst();

            employeeNameText = (TextView) findViewById(R.id.employeeName);
            employeeNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));

            titleText = (TextView) findViewById(R.id.title);
            titleText.setText(cursor.getString(cursor.getColumnIndex("title")));

            actions = new ArrayList<EmployeeAction>();

            String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
            if (officePhone != null) {
                    actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));
            }

            String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
            if (cellPhone != null) {
                    actions.add(new EmployeeAction("Call mobile", cellPhone, EmployeeAction.ACTION_CALL));
                    actions.add(new EmployeeAction("SMS", cellPhone, EmployeeAction.ACTION_SMS));
            }

            String email = cursor.getString(cursor.getColumnIndex("email"));
            if (email != null) {
                    actions.add(new EmployeeAction("Email", email, EmployeeAction.ACTION_EMAIL));
            }

            managerId = cursor.getInt(cursor.getColumnIndex("managerId"));
            if (managerId>0) {
                    actions.add(new EmployeeAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), EmployeeAction.ACTION_VIEW));
            }

            cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?", 
                                    new String[]{""+employeeId});
            cursor.moveToFirst();
            int count = cursor.getInt(0);
            if (count>0) {
                    actions.add(new EmployeeAction("View direct reports", "(" + count + ")", EmployeeAction.ACTION_REPORTS));
            }

            adapter = new EmployeeActionAdapter();
            setListAdapter(adapter);
    }

}

public void onListItemClick(ListView parent, View view, int position, long id) {

        EmployeeAction action = actions.get(position);

        Intent intent;
        switch (action.getType()) {

            case EmployeeAction.ACTION_CALL:  
                    Uri callUri = Uri.parse("tel:" + action.getData());  
                    intent = new Intent(Intent.ACTION_CALL, callUri); 
                startActivity(intent);
                    break;

            case EmployeeAction.ACTION_EMAIL:  
            intent = new Intent(Intent.ACTION_SEND);
            intent.setType("plain/text");
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
            startActivity(intent);
            break;

            case EmployeeAction.ACTION_SMS:  
                    Uri smsUri = Uri.parse("sms:" + action.getData());  
                    intent = new Intent(Intent.ACTION_VIEW, smsUri); 
                startActivity(intent);
                    break;

            case EmployeeAction.ACTION_REPORTS:  
                    intent = new Intent(this, DirectReports.class);
                    intent.putExtra("EMPLOYEE_ID", employeeId);
                    startActivity(intent);
                    break;

            case EmployeeAction.ACTION_VIEW:  
                    intent = new Intent(this, EmployeeDetails.class);
                    intent.putExtra("EMPLOYEE_ID", managerId);
                    startActivity(intent);
                    break;
        }
}    

class EmployeeActionAdapter extends ArrayAdapter<EmployeeAction> {

    EmployeeActionAdapter() {
                    super(EmployeeDetails.this, R.layout.action_list_item, actions);
            }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            EmployeeAction action = actions.get(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.action_list_item, parent, false);
            TextView label = (TextView) view.findViewById(R.id.label);
            label.setText(action.getLabel());
            TextView data = (TextView) view.findViewById(R.id.data);
            data.setText(action.getData());
            return view;
    }

}

}

2
  • EmployeeAction is included in the src of EmployeeDirectory6. That file needs to be include in your project. Commented Aug 11, 2011 at 16:18
  • Thank You. I totally missed that link where he put out the source code. It looks like he forgot to include that link in the step by step tutorial. Commented Aug 11, 2011 at 16:36

5 Answers 5

2

Java is statically typed, and everything you want to refer to at compile time needs to exist. So yes - you have to create the EmployeeAction, if you need it. (And you need to import it with import yourpackage.EmployeeAction; at the top of the class)

The type definition if the list <EmployeeAction> is there to enforce compile time safety for the elements of the list. It means "you can only put instances of EmployeeAction in this collection". This is useful when later you access the collection - the compiler can guarantee that the list contains only EmployeeAction instances

Sign up to request clarification or add additional context in comments.

Comments

0

Yes it definitly have to exists (EmployeeAction class). For what it should contains, I'd say probably a constructor that take parameters like these : (String location, String phone, EmployeeAction.ACTION*) and then setters and getters for these values.

Comments

0

EmplyeeAction is a not Java known class. Then, it could be imported (if it exists) or created.

Comments

0

The list is irrelevant here. A few lines down, your code says:

actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));

Yes, you do need a class EmployeeAction to compile that code. It's probably introduced in a part of the tutorial you overlooked or skipped.

Comments

0

you have to have a EmployeeAction class. since you say that you are new to java it will be good if go through this tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.