Reputation: 11
I have a ListActivity
so as to fill the content of a ListView
with an array of a Class I created.
No problem with that, but I have a Button placed underneath the ListView like this: Adding a button underneath a listview on android
And the problem is: how do I set a onClickListener for that button?
I tried this:
public class showTasks extends ListActivity implements OnClickListener{
Button addTaskButton;
Task[] tasks = {...};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_tasks);
setListAdapter(new TaskAdapter());
addTaskButton=(Button)findViewById(R.id.addTaskButton);
}
public void onListItemClick(ListView parent, View v, int position, long id){
Intent i=new Intent(this,showTask.class);
Bundle b = new Bundle();
b.putSerializable("task", tasks[position]);
i.putExtras(b);
startActivity(i);
}
//adapting a task to be viewable as a list of instances of the layout Task
class TaskAdapter extends ArrayAdapter<Task>{
TaskAdapter(){
super(showTasks.this, R.layout.task, tasks);
}
public View getView(int position, View convertView, ViewGroup parent){
View task=convertView;
if(task==null){
LayoutInflater inflater = getLayoutInflater();
task=inflater.inflate(R.layout.task, parent, false);
}
//now, let's define the Task variables and data
TextView taskName=(TextView)task.findViewById(R.id.taskName);
taskName.setText(tasks[position].name);
TextView deadline=(TextView)task.findViewById(R.id.deadline);
deadline.setText(tasks[position].deadline);
TextView importance=(TextView)task.findViewById(R.id.importance);
importance.setText(tasks[position].importance);
TextView urgency=(TextView)task.findViewById(R.id.urgency);
urgency.setText(tasks[position].urgency);
return(task);
}
}
public void onClick(View view) {
if(view==addTaskButton){
Intent i=new Intent(this,addTask.class);
startActivity(i);
}
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
Just look at the last part: to begin with, it doesn't let me have a void onClick(View view), it has to be onClick(DialogInterface dialog, int which).
But it doesn't even get there, because as soon as I get to addTaskButton.setOnClickListener((android.view.View.OnClickListener) this); it dies!
So, how can I have a ListView
and a Button in the same ListActivity
?
Upvotes: 1
Views: 1431
Reputation: 200170
You are using the wrong dialog... make sure your import statement says:
import android.view.OnClickListener;
instead of
import android.content.DialogInterface.OnClickListener;
Upvotes: 1