java - ImageButton 单击监听器不起作用

标签 java android

我有自定义适配器,因为我有一个 ImageButton 并且想设置 onClickListener

问题是,例如,当我单击位置 0 中的元素时,它返回位置 3 中的元素;我不明白为什么..

这是自定义适配器:

   public class CustomAdapter extends BaseAdapter  {


    private final Activity context;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mlist;
    private String mNome;
    private String mCognome;
    private String mTitolo;
    private String mInfo;
    ViewHolder viewHolder;
    View view;
    ArrayList<Integer> map = new ArrayList<>();
    HashMap<String, Object> e;

    public CustomAdapter(Activity context,ArrayList<HashMap<String, Object>> mlist){

        this.context = context;
        this.mlist=mlist;
        mInflater = context.getLayoutInflater();

    }



    public class ViewHolder {

        public ImageButton mImageButton;
        public TextView mTitleView;
        public TextView mNomeView;
        public TextView mCognomeView;
        public TextView mInfoView;



    }


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

    @Override
    public Object getItem(int position) {
        Log.d("POSITION GET",Integer.toString(position));

        if (mlist != null && position >= 0 && position < getCount()) {
            Log.d("POSITION GET",Integer.toString(position));
            return mlist.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {


        if (mlist != null && position >= 0 && position < getCount()) {
            return Integer.parseInt((String)mlist.get(position).get("id"));
        }
        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        view = convertView;
        e = (HashMap<String, Object>) getItem(position);

        if (view == null) {

            view = mInflater.inflate(R.layout.item_list_avvisi, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.mImageButton = (ImageButton) view.findViewById(R.id.imageButton);
            viewHolder.mCognomeView = (TextView) view.findViewById(R.id.cognome);
            viewHolder.mNomeView = (TextView) view.findViewById(R.id.nome);
            viewHolder.mTitleView = (TextView) view.findViewById(R.id.titolo);
            viewHolder.mInfoView = (TextView) view.findViewById(R.id.info);

            viewHolder.mImageButton.setClickable(true);






            view.setTag(viewHolder);

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

        viewHolder.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String nome = e.get("Nome").toString();

                Log.d("TAG", nome);

                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_TEXT, (String) "Avviso Scuola: " + nome + " " +
                                mCognome + "\n" +
                                mTitolo + "\n" +
                                mInfo
                );
                shareIntent.setType("text/plain");
                context.startActivity(shareIntent);


            }
        });


        viewHolder.mCognomeView.setText(e.get("Cognome").toString());
        viewHolder.mNomeView.setText(e.get("Nome").toString());
        viewHolder.mTitleView.setText(e.get("Titolo").toString());
        viewHolder.mInfoView.setText(e.get("Info").toString());



        return view;
    }

在 fragment 中我有:

private ArrayList<HashMap<String, Object>> mlist = new ArrayList<HashMap<String, Object>>();

    ...

for (int i = 1; i < array.length(); i++) {
                    HashMap<String, Object> map = new HashMap<String, Object>();

                    Gson gson = new Gson();


                    Avv mAvv = gson.fromJson(String.valueOf(array.getJSONObject(i)), Avv.class);

                    map.put("id", String.valueOf(i));
                    map.put("Name", mAvv.getNome());
                    map.put("Surname", mAvv.getCognome());
                    map.put("Title", mAvv.getTitolo());
                    map.put("Info",  mAvv.getTesto());


                    mlist.add(map);
                }

...


     CustomAdapter mAdapter = new CustomAdapter(getActivity(),mlist);
     list.setAdapter(mAdapter);

最佳答案

shareIntent.putExtra(Intent.EXTRA_TEXT, (String) "Avviso " + tx.getText().toString());

您正在使用从回收的 View 中使用的数据。永远不要那样做。而是从 ArrayList 获取数据

mlist.get(postion)....

您必须将 final 声明添加到该位置,以便它可以在 onClick 方法中访问

public View getView(final int position, View convertView, ViewGroup parent) {

关于java - ImageButton 单击监听器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30107338/

相关文章:

android - 使用JNI获取arrayList的元素

java - 无论如何,有没有将 mysql 表转换为 Java 类

java - 如果电话响了暂停应用程序

java - getSystemClipboard 因 headless 异常而失败

java - Android Studio 变量未从函数调用中更新

android - 如何随机改变 Sprite 的颜色

android - 在 Kotlin 中进行单元测试的 BuildConfigField 模拟

android - onCreateView() 从不运行(尝试将 ViewPager 用于屏幕幻灯片)

java - 我的逆向加密代码有什么问题吗?

java - HashMap 只打印最后一个条目