【问题标题】:Problem with ArrayListArrayList 的问题
【发布时间】:2011-08-11 16:13:00
【问题描述】:

我对 Java 编程相当陌生,并且正在学习位于此处的教程:http://coenraets.org/blog/android-samples/androidtutorial/ 我在哪里复制了我在这里遇到问题的代码:http://code.google.com/p/androidtutorial/source/browse/trunk/%20androidtutorial/EmployeeDirectory6/src/samples/employeedirectory/EmployeeDetails.java

编辑:谢谢大家。感谢@adamcodes 指出我完全错过了他发布源代码的链接。看来他忘记在分步教程中包含该链接了。

在大约 25 行之后,我在

处遇到错误
protected ArrayList<EmployeeAction> actions;

上面写着“EmployeeAction 不能被解析为一个类型”我的问题是 EmployeeAction 类是否必须被创建

actions = new ArrayList<EmployeeAction>();

即使我输入“actions = new ArrayList();”在我的代码中?如果是这样,该类应该包含什么?

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;
    }

}

}

【问题讨论】:

  • EmployeeAction 包含在 EmployeeDirectory6 的 src 中。该文件需要包含在您的项目中。
  • 谢谢。我完全错过了他发布源代码的那个链接。看起来他忘记在分步教程中包含该链接。

标签: java android eclipse


【解决方案1】:

Java 是静态类型的,您在编译时要引用的所有内容都必须存在。所以是的 - 如果需要,您必须创建 EmployeeAction。 (并且您需要在类的顶部使用import yourpackage.EmployeeAction; 导入它)

如果列表&lt;EmployeeAction&gt; 用于强制列表元素的编译时安全,则类型定义。这意味着“您只能将EmployeeAction 的实例放入此集合中”。这在您稍后访问集合时很有用 - 编译器可以保证列表仅包含 EmployeeAction 实例

【讨论】:

    【解决方案2】:

    是的,它肯定必须存在(EmployeeAction 类)。对于它应该包含的内容,我会说可能是一个构造函数,它接受如下参数:(String location, String phone, EmployeeAction.ACTION*),然后是这些值的 setter 和 getter。

    【讨论】:

      【解决方案3】:

      EmplyeeAction 是一个未知的 Java 类。然后,可以导入(如果存在)或创建它。

      【讨论】:

        【解决方案4】:

        这里的列表无关紧要。几行下来,你的代码说:

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

        是的,您确实需要一个类EmployeeAction 来编译该代码。它可能是在您忽略或跳过的教程的一部分中介绍的。

        【讨论】:

          【解决方案5】:

          你必须有一个EmployeeAction 类。 既然你说你是java新手,那么如果通过这个tutorial会很好

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-10-09
            • 1970-01-01
            • 2013-11-20
            • 2018-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多