使用ListView和自定义适配器设置数据

时间:2015-05-07 14:36:21

标签: android listview android-listview

我有一个ListView和一个自定义适配器。问题是我没有看到列表视图和数据,只有白页。

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

在CustomAdapter中我有这个:

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


        }
    }

我哪里错了?

更新:

谢谢,如果我想在ImageButton上设置onClickListener我怎么做?..我试着用:

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


                    }
                });

但问题是当我点击第一个项目viewHolder.mNomeView.getText()。toString()时它返回第二个项目的字符串..为什么?

1 个答案:

答案 0 :(得分:3)

问题出在您的getCount()方法

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

将其更改为:

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

更新

你的第二个问题:
试试这个:

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


                }
            });