setOnItemClickListener on Android 带有自定义适配器的应用未触发

setOnItemClickListener on Android App with custom Adapter not triggering

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

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

public class Program_Lure_list_Adapter extends BaseAdapter implements View.OnClickListener {


private static ArrayList<Program_Lure_Sound> programLureList;

private LayoutInflater l_inflater;

private int mPosition;

private int[] Image = {R.drawable.blacksound};
ViewHolder viewHolder;
ViewHolder selectedHolder;

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);

}

/*public Program_Lure_list_Adapter(Context context, ArrayList<Program_Lure_Sound> result) {
    programLureList = result;
    l_inflater = LayoutInflater.from(context);

}*/

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

/*@Override
public Object getItem(int i) {
    return null;
}*/

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

/*@Override
public long getItemId(int i) {
    return 0;
}*/

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

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = l_inflater.inflate(R.layout.layout_programlure_listitem, viewGroup, false);
        viewHolder = new ViewHolder();
        viewHolder.iv_Image_souund = (ImageView) view.findViewById(R.id.imageView29);
        viewHolder.tv_soundName = (TextView) view.findViewById(R.id.textView92);
        viewHolder.tv_selected = (TextView) view.findViewById(R.id.textView_selected);
        view.setTag(viewHolder);

    } else
        viewHolder = (ViewHolder) view.getTag();

    viewHolder.iv_Image_souund.setImageResource(Image[programLureList.get(i).getSoundImage()]);
    viewHolder.tv_soundName.setText(programLureList.get(i).getSoundName());
    viewHolder.tv_selected.setVisibility(View.GONE);
    view.setTag(viewHolder);
    view.setOnClickListener(this);
    return view;

}

@Override
public void onClick(final View v) {
    v.post(new Runnable() {
        @Override
        public void run() {
            ViewHolder holder = ((ViewHolder) v.getTag());
            holder.tv_selected.setVisibility(View.VISIBLE);
            if (selectedHolder != null)
                selectedHolder.tv_selected.setVisibility(View.INVISIBLE);
            selectedHolder = holder;
        }
    });

}
public class ViewHolder{
    TextView tv_soundName;
    ImageView iv_Image_souund;
    TextView tv_selected;
}

private class OnItemClickListener  implements OnClickListener{
    private int mPosition;

    OnItemClickListener(int position){
        mPosition = position;
    }

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


    }
}

}

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

        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...!");
    }
}

有什么想法吗?我猜这是我缺少的简单内容。

点击事件是被view消费的,所以没有点击事件触发itemclick。

您可以将 itemclick 代码移到 onclicklistener 中

在您的主 activity

中更改此设置
mAdapter = new Program_Lure_list_Adapter(MainActivity.this, plsList); 

然后在您的自定义适配器中 class

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);

        }

在您的自定义适配器 class 中实施此方法,它将告诉您的主要 activity 或您的其他部分 activity 单击哪个项目。

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;
    }

之后将你的其他段代码放在主 activity 中,如下方法

public void onItemClick(int mPosition) {

int i=mPosition; 字符串名称 = 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...!");
            }



}

希望对您有所帮助。