DevOps85
DevOps85

Reputation: 6523

Set data with ListView and Custom Adapter

i have a ListView and a Custom Adapter. The problem is i not see the listview and data, but only a white page.

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

And in the CustomAdapter i have this:

    public class CustomAdapter extends BaseAdapter {


        private final Activity context;
        private LayoutInflater mInflater;
        ArrayList<HashMap<String, Object>> mlist;

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

        @Override
        public Object getItem(int position) {

            if (mlist != null && position >= 0 && position < getCount()) {
                return mlist.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

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

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

            View view = convertView;
            ViewHolder viewHolder;

            if (view == null) {

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

                viewHolder.mImageButton = (ImageButton) view.findViewById(R.id.imageButton);
                viewHolder.mCognomeView = (TextView) view.findViewById(R.id.surname);
                viewHolder.mNomeView = (TextView) view.findViewById(R.id.name);
                viewHolder.mTitleView = (TextView) view.findViewById(R.id.title);
                viewHolder.mInfoView = (TextView) view.findViewById(R.id.info);


                view.setTag(viewHolder);

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

            setData(viewHolder, mlist, position);

            return view;
        }

        private void setData(ViewHolder viewHolder, ArrayList<HashMap<String, Object>> mlist,int position) {


            viewHolder.mCognomeView.setText(mlist.get(position).get("Surname").toString());
    viewHolder.mNomeView.setText(mlist.get(position).get("Name").toString());
            viewHolder.mTitleView.setText(mlist.get(position).get("Title").toString());
            viewHolder.mInfoView.setText(mlist.get(position).get("Info").toString());


        }
    }

where i'm wrong?

UPDATE:

thanks, and if i want to set onClickListener on ImageButton how i can?.. i try with:

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

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


                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);

                        shareIntent.putExtra(Intent.EXTRA_TEXT, (String)  viewHolder.mNomeView.getText().toString()
                                );
                        shareIntent.setType("text/plain");
                        context.startActivity(shareIntent);


                    }
                });

But the problem is when i click for example the first item, viewHolder.mNomeView.getText().toString() it return the String of the second item..why?

Upvotes: 2

Views: 1811

Answers (1)

MHP
MHP

Reputation: 2731

the problem is in your getCount() method

 @Override
    public int getCount() {
        return 0;
    }

change it to:

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

UPDATE

your second question:
try this:

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


                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);

                    shareIntent.putExtra(Intent.EXTRA_TEXT, (String) mlist.get(position).get("Name").toString()
                            );
                    shareIntent.setType("text/plain");
                    context.startActivity(shareIntent);


                }
            });

Upvotes: 3

Related Questions