我注意到我的适配器中的上下文是MyMovieApp,而不是SelectActivity:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Movie movie = getItem(position);
final ViewHolder viewHolder;
/* convertView will be null if convertView isn't from recycler
* the last earlier row isn't totally out of view when scrolling
*/
if(convertView == null) {
Log.d("ADAPTER", "convertView is null; Position=" + position + " and " +
" movie=" + movie.getTitle());
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.selection_item, parent, false);
// inflate individual views and cache them into viewholder
viewHolder = new ViewHolder();
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
viewHolder.cb.setChecked(movie.isSelected());
viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//allMovies.get(getPosition).setSelected(buttonView.isChecked());
Movie selectedMovie = allMovies.get(getPosition);
selectedMovie.setSelected(buttonView.isChecked());
//movie.setSelected(buttonView.isChecked());
Log.d("SELECT", "onCheckChanged, " + selectedMovie.getTitle() + " is " + buttonView.isChecked());
}
});
viewHolder.posterIV = (ImageView) convertView.findViewById((R.id.ivMoviePoster));
viewHolder.playBtnIV = (ImageView) convertView.findViewById((R.id.ivPlayBtn));
viewHolder.movieTitleTV = (TextView) convertView.findViewById(R.id.movie_title);
viewHolder.castTV = (TextView) convertView.findViewById(R.id.topCast);
viewHolder.ratingTV = (TextView) convertView.findViewById((R.id.rating));
viewHolder.runtimeTV = (TextView) convertView.findViewById(R.id.movlength);
viewHolder.showtimesNSGV = (NonScrollableGridView) convertView.findViewById(R.id.showtimes);
// TAG - associate view memory with these views
convertView.setTag(viewHolder);
// TAG - associate view memory with checkbox, unnecessary
convertView.setTag(R.id.cb, viewHolder.cb);
}
else {
Log.d("SELECT_ADAPTER", "Reusing convertView, position=" + position + "and " +
" movie=" + movie.getTitle());
viewHolder = (ViewHolder) convertView.getTag();
}
// AND TAG HERE
viewHolder.cb.setTag(position);
viewHolder.cb.setChecked(allMovies.get(position).isSelected());
// associate play button with movie so listener callback knows which trailer to launch
viewHolder.playBtnIV.setTag(movie);
// set values in views
ImageLoader.getInstance().displayImage(movie.getImageUrl(), viewHolder.posterIV);
viewHolder.movieTitleTV.setText(movie.getTitle());
viewHolder.castTV.setText(movie.getTopCast());
viewHolder.ratingTV.setText(movie.getRating());
viewHolder.runtimeTV.setText(movie.getDuration());
ArrayList<String> showtimes = movie.getShowtimes();
showtimes = getTwelveHrShowtimes(showtimes);
ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(getContext(), R.layout.gridview_item, showtimes);
viewHolder.showtimesNSGV.setAdapter(gridAdapter);
return convertView;
}
logcat说:在活动类com.example.apps.MyMovieApp中找不到方法playTrailer(View),用于视图类android.widget.ImageView上的onClick处理程序,id为'ivPlayBtn'
选择项的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="1.9"
android:scaleY="1.9"
android:layout_marginRight="8dp" />
<FrameLayout
android:id="@+id/posterLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cb"
android:layout_alignTop="@+id/cb">
<ImageView
android:id="@+id/ivMoviePoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/poster_border" />
<ImageView
android:id="@+id/ivPlayBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/play"
android:onClick="playTrailer" />
</FrameLayout>
<TextView
android:id="@+id/movie_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/posterLayout"
android:layout_marginLeft="6dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/topCast"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/posterLayout"
android:layout_below="@id/movie_title"
android:layout_margin="6dp"
android:textSize="13sp" />
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/posterLayout"
android:layout_below="@id/topCast"
android:layout_marginLeft="6dp"
android:textSize="13sp" />
<TextView
android:id="@+id/movlength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/posterLayout"
android:layout_below="@id/rating"
android:layout_marginLeft="6dp"
android:textSize="13sp" />
<!-- match_parent width means match the parent(RelativeLayout) width;
fill out rest of screen. Versus wrap_content on height means the grid should only
be as high as the results it contains -->
<com.sb.android.widget.NonScrollableGridView
android:id="@+id/showtimes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/posterLayout"
android:layout_toRightOf="@id/cb"
android:numColumns="4"
android:padding="2dp"
android:gravity="center" />
</RelativeLayout>
我的清单:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".MyMovieApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
android:label="@string/app_name">
<activity android:name=".SelectActivity"
android:label="Select Desired Movies"
android:screenOrientation="portrait" >
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultsActivity"
android:label="@string/poss_sched">
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
答案 0 :(得分:0)
将LayoutInflater.from(getContext());
替换为getLayoutInflater()
上调用的Activity
。