1

I have my Custom Adapter because I have a ImageButton and want to set the onClickListener.

The problem is when I click for example the element in the position 0 it returns the element in position 3; I do not understand why..

This is the Custom Adapter:

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

And in the fragment i have:

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);
DevOps85
  • 6,473
  • 6
  • 23
  • 42

1 Answers1

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

You are using data used from the Views that are recycled. Don't do that, ever. Instead get that data from the ArrayList

mlist.get(postion)....

You have to add the final declaration to the position in order for it to be accessible inside the onClick method

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

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • You can, but you have to make the position final. getView(**final** int position – Bojan Kseneman May 07 '15 at 17:26
  • Hi, the problem is the position (i have try with Log.d("POSITION", Integer.toString(position)); is only 0-1-2 or 3.. and after that it return to 0 but the element in the list is 4...so the other element is cut out..Can you help? ) – DevOps85 May 08 '15 at 13:47
  • ListView is recycling views and you will see the position of the view that is currently being drawn. Read this http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – Bojan Kseneman May 08 '15 at 17:32
  • Hi, i read but for now i'm not found solution.. i update my question – DevOps85 May 11 '15 at 07:31