Android应用程序上的setOnItemClickListener,自定义适配器未触发

时间:2015-12-03 04:24:21

标签: android listview onclick adapter

我正在尝试在用户点击某个项目时触发代码段。它在自定义适配器上触发,但在主段上没有触发。

这是适配器的代码段以及我尝试触发但未触发的内容。

        if(getSoundList()!=null) {
        final ArrayList<Program_Lure_Sound> plsList = getSoundList();
        //mAdapter = new Program_Lure_list_Adapter(this, plsList);
        mAdapter = new Program_Lure_list_Adapter(Activity_program_lure.this, plsList);
        activeSoundList.setAdapter(mAdapter);

        setListViewHeightBasedOnChildren(activeSoundList);


        // activeSoundList.setAdapter();
        //  activeSoundList
        activeSoundList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.v("BLEApp", "HEREHERE2");
                String name = plsList.get(i).getSoundName();

                String[] arr = name.split(" ");

                String ID = arr[0].split("#")[1];
                // soundCode = ID;
                //showToast("Sound code: "+soundCode);
                String sd = plsList.get(i).getSoundName();
                if (sd.length() > 10)
                    sd = sd.substring(0, Math.min(sd.length(), 10));
                tv_ActiveSound.setText(sd);
                if (mBluetoothLeService != null) {
                    // playSound(ID);
                    try {
                        changeToSound(ID);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    showToast("Please, click on power button and then click to" +
                            "play sound...!");
                }
            }
        });

        // endregion

        dbHelper = new DataBaseHelper(this);
        String lureUsed = dbHelper.LureUsed(lureName);
        dbHelper.closeDB();
        tv_LureUsed.setText(lureUsed);

    }

    float  opacity = 0.4f;
    ll.setAlpha(opacity);
    tv_power.setText("ON");
    playSoundButton.setAlpha(opacity);
    playSoundButton.setEnabled(false);


}

public void onItemClick(int mPosition) {
    Log.v("BLEApp", "HEREHERE");
    final ArrayList<Program_Lure_Sound> plsList = getSoundList();

    int i=mPosition; String name = plsList.get(i).getSoundName();

    String[] arr = name.split(" ");

    String ID = arr[0].split("#")[1];
    // soundCode = ID;
    //showToast("Sound code: "+soundCode);
    String sd = plsList.get(i).getSoundName();
    if(sd.length()>10)
        sd = sd.substring(0, Math.min(sd.length(),10));
    tv_ActiveSound.setText(sd);
    if(mBluetoothLeService!=null){
        // playSound(ID);
        try {
            changeToSound(ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else{
        showToast("Please, click on power button and then click to" +
                "play sound...!");
    }
}

}

这是自定义适配器,onClick正在触发,但我还需要触发其他段。

*
 ...
  &:before, &:after
    ...

有什么想法吗?我猜这很简单,我很想念。

2 个答案:

答案 0 :(得分:0)

点击事件是按视图消费,因此没有点击事件来触发项目点击。

您可以在onclicklistener

中移动itemclick代码

答案 1 :(得分:0)

在您的主要活动中更改此内容

mAdapter = new Program_Lure_list_Adapter(MainActivity.this, plsList); 

然后在您的自定义适配器类

private Activity activity;    
    public Program_Lure_list_Adapter(Activity a, ArrayList<Program_Lure_Sound> result) {
    activity = a;
            programLureList = result;
            l_inflater = (LayoutInflater)activity.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

在您的自定义适配器类中实现此方法,该类会告诉您的主要活动或您的其他细分活动,以便点击哪个项目。

private class OnItemClickListener  implements OnClickListener{
        private int mPosition;

        OnItemClickListener(int position){
            mPosition = position;
        }

        @Override
        public void onClick(View arg0) {
            MainActivity sct = (MainActivity)activity;
            sct.onItemClick(mPosition);


        }
    } 

并修改这两种方法,如下所示。

public Object getItem(int position) {
        return position;
    }

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

之后,将您的其他段代码放入主活动中,如下所示

public void onItemClick(int mPosition){

int i = mPosition; String name = plsList.get(i).getSoundName();

            String[] arr = name.split(" ");

            String ID = arr[0].split("#")[1];
           // soundCode = ID;
            //showToast("Sound code: "+soundCode);
            String sd = plsList.get(i).getSoundName();
            if(sd.length()>10)
            sd = sd.substring(0, Math.min(sd.length(),10));
            tv_ActiveSound.setText(sd);
            if(mBluetoothLeService!=null){
               // playSound(ID);
                try {
                    changeToSound(ID);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else{
                showToast("Please, click on power button and then click to" +
                        "play sound...!");
            }



}

希望这会对你有所帮助。