0

カスタム ClickListener を登録できないという点で、単純な GridView の問題と思われる問題が発生しています。

標準のクリック イベントを動作させることはできますが、AlbumClickListener を使用することはできないため、クリックされた位置ごとにオブジェクトの詳細を取得できません。

私は通常、厳密に iOS であるため、これについて髪を引き裂きます。助けてください!!

アルバム Java オブジェクト

package com.pixelperfect.mayday;

public class AlbumGridView extends GridView implements
android.widget.AdapterView.OnItemClickListener {

private List<Album> albums;
private AlbumClickListener albumClickListener;

public AlbumGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public AlbumGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AlbumGridView(Context context) {
    super(context);
}

public void setAlbums(List<Album> albums){
    this.albums = albums;
    MusicGridAdapter adapter = new MusicGridAdapter(getContext(), albums);
    setAdapter(adapter);

    setOnItemClickListener(this);

}

 // Calling this method sets a listener to the list
// Whatever class is passed in will be notified when the list is pressed
// (The class that is passed in just has to 'implement VideoClickListener'
// meaning is has the methods available we want to call)
public void setOnAlbumClickListener(AlbumClickListener l) {
    albumClickListener = l;
}

@Override
public void setAdapter(ListAdapter adapter) {
    super.setAdapter(adapter);
}

 // When we receive a notification that a list item was pressed
// we check to see if a video listener has been set
// if it has we can then tell the listener 'hey a video has just been clicked' also passing the video
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    // this is crashing
    if(albumClickListener != null){
        albumClickListener.onAlbumClicked(albums.get(position));
    }
}
}

アルバムクリックリスナー

package com.pixelperfect.mayday;

import com.pixelperfect.mayday.Album;

public interface AlbumClickListener {

public void onAlbumClicked(Album album);
}

ミュージックグリッドアダプター

package com.pixelperfect.mayday;

public class MusicGridAdapter extends BaseAdapter 
{
List<Album> albums;
private LayoutInflater mInflater;

public MusicGridAdapter(Context context, List<Album> albums) {
    this.albums = albums;
    this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return albums.size();
}

@Override
public Object getItem(int position) {
    return albums.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) 
    {
        convertView = mInflater.inflate(R.layout.music_grid, null);
    } 

    Album album = albums.get(position);

    String newString = album.getArtworkURL().replace("100x100", "600x600");

    LoaderImageView thumb = (LoaderImageView) convertView.findViewById(R.id.grid_item_image);
    thumb.setImageDrawable(newString);

    return convertView;
}
}

activity_music.xml

<?xml version="1.0" encoding="utf-8"?>
<com.pixelperfect.mayday.AlbumGridView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:id="@+id/gridView1"/>

music_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp" >

    <com.pixelperfect.mayday.LoaderImageView
    android:id="@+id/grid_item_image"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:focusable="false"
    android:clickable="false"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"
    android:layout_marginBottom="4dp" />

</LinearLayout>

主な活動

public class Music extends Activity implements AlbumClickListener {

private AlbumGridView gridView;
//  Album[] Album;
private ProgressDialog m_ProgressDialog = null;

@SuppressLint("HandlerLeak")
@Override protected void onResume() 
{ 
    super.onResume(); // setText() here 

    responseHandler = new Handler() {
        public void handleMessage(Message msg) {
            populateGridWithAlbums(msg);
        };
    };
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music);

    gridView = (AlbumGridView) findViewById(R.id.gridView1);

    gridView.setOnAlbumClickListener(this);

    new Thread(new GetAlbumsFromiTunesTask(responseHandler, "GrabTheAlbums", "1")).start();
    m_ProgressDialog = ProgressDialog.show(this, "Please wait...", "Loading Albums ...", true);
}


 // This is the handler that receives the response when the YouTube task has finished
@SuppressLint("HandlerLeak")
Handler responseHandler = new Handler() {
    public void handleMessage(Message msg) {
        populateGridWithAlbums(msg);
        m_ProgressDialog.dismiss();
    };
};

/**
 * This method retrieves the Library of videos from the task and passes them to our ListView
 * @param msg
 */
private void populateGridWithAlbums(Message msg) {

    @SuppressWarnings("unchecked")
    List<Album> albums = (List<Album>)   (msg.getData().get(GetAlbumsFromiTunesTask.LIBRARY));
    gridView.setAdapter(new MusicGridAdapter(this, albums));
}

@Override
protected void onStop() {
    responseHandler = null;
    super.onStop();
    m_ProgressDialog.dismiss();
}

@Override
public void onAlbumClicked(Album album) {
    System.out.println("CLICKED 2");
}
}
4

1 に答える 1