在Android中,检测是否单击了ListView项

在Android中,检测是否单击了ListView项,android,listview,android-fragments,android-listview,Android,Listview,Android Fragments,Android Listview,我在片段中有一个列表视图,我需要能够检测何时选择了列表视图中的项目。我需要列表项沿着这些行抛出一个标志或其他东西,以便主活动中包含片段的方法可以检测是否对ListView进行了操作。我需要一个主要活动的方法,基本上如下: public void doSomething(){ if(Fragment ListView onItemClick is detected){ //--- do something } else { //--- don't d

我在
片段中有一个
列表视图
,我需要能够检测何时选择了列表视图中的项目。我需要列表项沿着这些行抛出一个标志或其他东西,以便主活动中包含片段的方法可以检测是否对ListView进行了操作。我需要一个主要活动的方法,基本上如下:

public void doSomething(){
    if(Fragment ListView onItemClick is detected){
        //--- do something
    } else {
        //--- don't do anything
    }
}
list_LV.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Cursor getPathCursor = (Cursor) list_LV.getItemAtPosition(position);               
        String cursorSDFStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("sdfdate"));
        String cursorCalDateStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("caldate"));
        String cursorURLStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("path"));
        String cursorTitleStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("title"));
        String cursorbodyStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("body"));

        Intent slpI = new Intent("com.myapp.LISTVIDEO");
        slpI.putExtra("SDFKey", cursorSDFStr);
        slpI.putExtra("CalDateKey", cursorCalDateStr);
        slpI.putExtra("PathKey", cursorURLStr);
        slpI.putExtra("TitleKey", cursorTitleStr);
        slpI.putExtra("bodyKey", cursorbodyStr);
        startActivity(slpI);
    }
});
片段列表视图中的
OnItemClickListener
需要指示已对其执行操作,如下所示:

public void doSomething(){
    if(Fragment ListView onItemClick is detected){
        //--- do something
    } else {
        //--- don't do anything
    }
}
list_LV.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Cursor getPathCursor = (Cursor) list_LV.getItemAtPosition(position);               
        String cursorSDFStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("sdfdate"));
        String cursorCalDateStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("caldate"));
        String cursorURLStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("path"));
        String cursorTitleStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("title"));
        String cursorbodyStr = getPathCursor.getString(getPathCursor.getColumnIndexOrThrow("body"));

        Intent slpI = new Intent("com.myapp.LISTVIDEO");
        slpI.putExtra("SDFKey", cursorSDFStr);
        slpI.putExtra("CalDateKey", cursorCalDateStr);
        slpI.putExtra("PathKey", cursorURLStr);
        slpI.putExtra("TitleKey", cursorTitleStr);
        slpI.putExtra("bodyKey", cursorbodyStr);
        startActivity(slpI);
    }
});
list_LV.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
游标getPathCursor=(游标)列表\u LV.getItemAtPosition(位置);
字符串游标Dfstr=getPathCursor.getString(getPathCursor.getColumnIndexOrThrow(“sdfdate”);
字符串cursorCalDateStr=getPathCursor.getString(getPathCursor.getColumnIndexOrThrow(“caldate”);
字符串cursorURLStr=getPathCursor.getString(getPathCursor.getColumnIndexOrThrow(“路径”);
字符串cursorTitleStr=getPathCursor.getString(getPathCursor.getColumnIndexOrThrow(“标题”);
字符串cursorbodyStr=getPathCursor.getString(getPathCursor.getColumnIndexOrThrow(“正文”);
Intent slpI=newintent(“com.myapp.LISTVIDEO”);
slpI.putExtra(“SDFKey”,游标DFSTR);
slpI.putExtra(“CalDateKey”,cursorCalDateStr);
slpI.putExtra(“路径键”,cursorURLStr);
slpI.putExtra(“标题键”,cursorTitleStr);
slpI.putExtra(“bodyKey”,光标或光标);
星触觉;
}
});

您应该按照此处概述的步骤操作


您可以交替使用广播或类似Otto的内容

好的,有什么问题吗?在onclick方法中,只需调用
doSomething()
?只需在
onItemClick中调用
getActivity()
,即可获取此活动的引用