从扩展ListView的类传递Intent

时间:2012-08-13 06:26:10

标签: android android-intent android-listview

这是我的类,它是列表视图中的填充项,事情很顺利,点击listitem时执行onItemClick方法,但是当我从那里传递一个intent时,它没有传递意图,plz帮帮我

public class VideosListView extends ListView implements android.widget.AdapterView.OnItemClickListener {

private List<Video> videos;
private VideoClickListener videoClickListener;
MainActivity ma;
private Context mcontext;

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

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

public VideosListView(Context context) {
    super(context);
    mcontext=context;
}

public void setVideos(List<Video> videos){
    this.videos = videos;
    VideosAdapter adapter = new VideosAdapter(getContext(), videos);
    setAdapter(adapter);
    // When the videos are set we also set an item click listener to the list
    // this will callback to our custom list whenever an item it pressed
    // it will tell us what position in the list is pressed
    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 setOnVideoClickListener(VideoClickListener l) {
    videoClickListener = 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) 
{
    Intent intent = new Intent(mcontext,AnVideoView.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    mcontext.startActivity(intent); 

    Log.i("VideoListView", "I am Clicked");
    if(videoClickListener != null)
    {
        videoClickListener.onVideoClicked(videos.get(position));
        Log.d("My Position ","position is" + position);
    }

这是我从onItemClick()开始的类,我想传递给一个活动类,如何做到这一点比帮助

2 个答案:

答案 0 :(得分:0)

试试这个...... 它会对你有所帮助。

Activity activity;

    public VideosListView(Activity activity) {
        super(context);
        this.activity=activity;
    }


    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) 
    {
        Intent intent = new Intent(activity,AnVideoView.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        activity.startActivity(intent); 

        Log.i("VideoListView", "I am Clicked");
        if(videoClickListener != null)
        {
            videoClickListener.onVideoClicked(videos.get(position));
            Log.d("My Position ","position is" + position);
        }

答案 1 :(得分:0)

onItemClick中的

((AdapterView适配器,View v,int position,long id) 我已经为扩展BaseAdapter的类创建了一个构造函数,然后从那里我们可以很容易地传递我们的意图......它对我有用,无论如何都会帮助我很多人

我们可以在构造函数中传递位置,并对所选项目执行任何操作。