我无法弄清楚为什么我的按钮不可点击。我有一个gridview中的歌曲列表,当用户点击列表中的某个项目时,应用程序将把它们带到我的SongDetailFragment活动中,其中包含有关该歌曲的更多信息。我有一个星形按钮,如果数据库中的某首歌曲我想让它像这样btn_star_big_on,并且如果它不在数据库中,我希望它关闭btn_star_big_off。问题是当我运行我的应用程序时,我无法点击按钮。就好像它只是一个静态图像,因为用户应该能够打开和关闭它。图片按钮不可点击 - Android
任何人都可以请指教如何解决这个问题?
Here是我得到了我的星号按钮:
这里是我的星号按钮的xml:
<ImageView
android:id="@+id/imgFavBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="@string/favourite"
android:paddingTop="@dimen/activity_vertical_margin"
android:src="@android:drawable/btn_star_big_off" />
这里是我的详细信息片段:
public class SongDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
private Song song;
private static final int CURSOR_LOADER_ID = 0;
ImageView imgViewFavButton;
Boolean mIsFavourite;
public SongDetailFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.song_fragment_detail, container, false);
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra("song")) {
song = intent.getParcelableExtra("song");
((TextView) rootView.findViewById(R.id.detail_title_textview))
.setText(song.getTitle());
((TextView)rootView.findViewById(R.id.detail_overview_textview))
.setText(song.getDescription());
((TextView)rootView.findViewById(R.id.song_date_textview))
.setText(song.getdate());
ImageView imageView = (ImageView) rootView.findViewById(R.id.song_detail_poster_imageview);
Picasso.with(getActivity()).load(song.getPoster()).into(imageView);
}
imgViewFavButton = (ImageView) rootView.findViewById(R.id.imgFavBtn);
checkFavourites(song);
imgViewFavButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Cursor c =
getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI,
new String[]{songContract.songEntry._ID},
null,
null,
null);
insertData(song);
getLoaderManager().initLoader(CURSOR_LOADER_ID, null, songDetailFragment.this);
}
});
return rootView;
}
public void insertData(song song){
ContentValues songValues = new ContentValues();
songValues.put(songContract.songEntry.COLUMN_ID, song.getsong_id());
songValues.put(songContract.songEntry.COLUMN_IMAGE, song.getPoster());
songValues.put(songContract.songEntry.COLUMN_TITLE, song.getTitle());
songValues.put(songContract.songEntry.COLUMN_OVERVIEW, song.getDescription());
songValues.put(songContract.songEntry.COLUMN_date, song.getdate());
getActivity().getContentResolver().insert(songContract.songEntry.CONTENT_URI,
songValues);
}
private void checkFavourites(song song) {
Cursor c =
getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI,
null,
songContract.songEntry.COLUMN_ID + " = ?",
new String[]{song.getsong_id()},
null);
if (c != null) {
c.moveToFirst();
int index = c.getColumnIndex(songContract.songEntry.COLUMN_ID);
if (c.getCount() > 0 && c.getString(index).equals(song.getsong_id())) {
mIsFavourite = true;
imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_on);
}
else{
imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_off);
}
c.close();
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args){
return new CursorLoader(getActivity(),
songContract.songEntry.CONTENT_URI,
null,
null,
null,
null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
}
// Set the cursor in our CursorAdapter once the Cursor is loaded
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
}
// reset CursorAdapter on Loader Reset
@Override
public void onLoaderReset(Loader<Cursor> loader){
}
}