所以我对Android开发很新,并且一直试图拼凑一些代码位。这是我到目前为止所做的:
package com.teslaprime.prirt;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
import java.util.List;
public class TaskList extends Activity {
List<Task> model = new ArrayList<Task>();
ArrayAdapter<Task> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(onAdd);
ListView list = (ListView) findViewById(R.id.tasks);
adapter = new ArrayAdapter<Task>(this,android.R.layout.simple_list_item_1,model);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(View v, int position, long id) {
adapter.remove(position);
}
});}
private View.OnClickListener onAdd = new View.OnClickListener() {
public void onClick(View v) {
Task task = new Task();
EditText name = (EditText) findViewById(R.id.taskEntry);
task.name = name.getText().toString();
adapter.add(task);
}
};
}
以下是我得到的错误:
compile:
[javac] /opt/android-sdk/tools/ant/main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to /home/chris-kun/code/priRT/bin/classes
[javac] /home/chris-kun/code/priRT/src/com/teslaprime/prirt/TaskList.java:30: <anonymous com.teslaprime.prirt.TaskList$1> is not abstract and does not override abstract method onItemClick(android.widget.AdapterView<?>,android.view.View,int,long) in android.widget.AdapterView.OnItemClickListener
[javac] list.setOnItemClickListener(new OnItemClickListener() {
[javac] ^
[javac] /home/chris-kun/code/priRT/src/com/teslaprime/prirt/TaskList.java:32: remove(com.teslaprime.prirt.Task) in android.widget.ArrayAdapter<com.teslaprime.prirt.Task> cannot be applied to (int)
[javac] adapter.remove(position);
[javac] ^
[javac] 2 errors
BUILD FAILED
/opt/android-sdk/tools/ant/main_rules.xml:384: Compile failed; see the compiler error output for details.
Total time: 2 seconds
任何想法?
答案 0 :(得分:4)
我不是Android程序员,但它指的是这一行:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(View v, int position, long id) {
adapter.remove(position);
}
});}
你有方法签名View,int,long,它想要
AdapterView<?> parent, View view, int position, long id as the signature
关于第二个问题,它是说
适配器的remove方法需要
Task
对象而不是整数。
答案 1 :(得分:1)
abstract
类包含未实现的方法。如果要扩展它,则必须实现未实现的方法。 Eclipse将告诉您何时不执行必要操作,如果单击红色“x”,将为您提供这些问题的建议解决方案。在这种情况下,它将自动包含您尚未实现的这些抽象方法的方法签名。