2013-06-27 70 views
0

我遇到的问题似乎是一个简单的GridView问题,因为我无法获得自定义ClickListener注册。GridView上的自定义ClickListener未注册

我可以得到一个标准的点击事件工作,但不是通过我的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"); 
} 
} 
+0

检查'专辑! = null',否则你对'albums.get(position)'的调用将会失败。我只能乍一看。 –

回答

1

我认为这将是更容易只是一个onItemClickListener连接到你的GridView是这样的:

gridView.setOnItemLongClickListener(new OnItemLongClickListener() { 

      @Override 
      public boolean onItemLongClick(AdapterView<?> gridView, View view, 
        int pos, long id) { 
         //do something -- you can simply use the pos parameter to indicate which element was clicked 
        } 
}); 
+0

然后我会如何将它链接到我的对象?我在对象中有4个项目,需要访问每个项目。感谢您的回复 –

+0

您可以使用pos参数访问点击位置 – EMarci15

+0

是的,我得到了这一点,现在我已经实现了,但我确信我无法以这种方式访问​​我的对象。我现在会玩一玩,看看。 –