How to capture a button click in the original activity instead of the ListAdapter
I have an activity called MoviesActivity that uses a ListAdapter called MovieAdapter to display a list of movies. Each list item has name etc., and a button which the user can click to play the movie. My question is - how do I shift the onClick code associated with the button from the MovieAdapter class to the MoviesActivity class?
public class MoviesActivity extends Activity{
private static final String TAG = "MoviesActivity";
public ListView listView;
public MovieAdapter movieAdapter;
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies);
listView = (ListView) findViewById(R.id.MovieListView);
moviesManager = new MoviesManager(client);
List<Movie> allMoviesList = moviesManager.getAllMoviesList();
movieAdapter = new MovieAdapter((Context)this, allMoviesList,moviesManager);
listView.setAdapter(movieAdapter);
}
The code for MovieAdapter
public class MovieAdapter extends BaseAdapter implements OnClickListener{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
}
@Override
public void onClick(View v) {
//Play movie
...
}
}
My MovieAdapter is fully functioning. My question is - I would like to write the code to play the movie in MoviesActivity and not in MovieAdaptor. How can I do that? I have had similar problems many times but never found a good technique (other than using static methods etc.) The problem comes when I开发者_开发技巧 try to issue a getWindow() inside onClick - which it wouldn't since its not in a Activity subclass.
Instead of passing a Context
object into your MovieAdapter
constructor, pass in the MovieActivity
object and run the MovieActivity
's playMovie(Movie)
method from the button's OnClick
event:
Your Activity:
public class MoviesActivity extends Activity {
public ListView listView;
public MovieAdapter movieAdapter;
//...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies);
listView = (ListView) findViewById(R.id.MovieListView);
moviesManager = new MoviesManager(client);
List<Movie> allMoviesList = moviesManager.getAllMoviesList();
movieAdapter = new MovieAdapter(this, allMoviesList, moviesManager);
listView.setAdapter(movieAdapter);
}
public void playMovie(Movie movie) {
// ...
}
}
Your Adapter:
public class MovieAdapter extends BaseAdapter {
private MoviesActivity mActivity;
private List<Movie> mMovies;
private MoviesManager mClient;
// ...
public MovieAdapter(MoviesActivity activity, List<Movie> allMovies,
MoviesManager client) {
mActivity = activity;
mMovies = allMovies;
mClient = client;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Movie movie = mMovies.get(position);
// ...
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mActivity.playMovie(movie);
}
});
}
}
If you can extend ListActivity instead of activity you can simply do:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
精彩评论