如何使用json在Listview中添加标头

时间:2016-09-26 05:26:35

标签: android listview android-arrayadapter

我正在使用listview并解析数据并正确显示。但我想在同一网址的filterName中添加listview作为标题。任何人都可以告诉我如何实现这一点。提前谢谢。以下是我的适配器类

适配器

public class MyCustomAdapter extends ArrayAdapter<ModelUser>{
        private List<ModelUser> modelUsers;
        private Context context;
        private  PrefsManager prefsManager;
        private ArrayList<ModelUser> modelUserArrayList;

       public static final int TYPE_ODD = 0;
       public static final int TYPE_EVEN = 1;

        public MyCustomAdapter(Context context, List<ModelUser> products) {
            super(context,R.layout.country_info, products);
            this.context = context;
            this.modelUsers = products;
            prefsManager = new PrefsManager();

            modelUserArrayList = new ArrayList<>();
            modelUserArrayList =  prefsManager.getFavorites(context);
        }


        private class ViewHolder {
            CheckBox userName;
            public TextView tv,tvs;
        }

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


       @Override
       public int getViewTypeCount() {
           return 2;
       }
       @Override
       public int getItemViewType(int position) {
           return modelUsers.get(position).getId();
       }



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

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {

            ViewHolder holder = null;

            final ModelUser modelUser = modelUsers.get(i);
            int listViewItemType = getItemViewType(i);
            if(convertView  == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();

                if (listViewItemType == TYPE_EVEN) {
                    convertView = inflater.inflate(R.layout.list_item_enty, null);
                } else if (listViewItemType == TYPE_ODD) {
                    convertView = inflater.inflate(R.layout.country_info, null);
                }

                holder = new ViewHolder();

                holder.userName = (CheckBox) convertView.findViewById(R.id.chkSelected);
                holder.tv = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvs = (TextView) convertView.findViewById(R.id.list_item_entry_title);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }


            if(listViewItemType==TYPE_EVEN)
            {
                holder.tvs.setText(modelUser.getTmpid());
            }
            else if(listViewItemType == TYPE_ODD)
            {
                holder.tv.setText(modelUser.getUserName());


                if (checkFavoriteItem(modelUser)) {

                    holder.userName.setSelected(modelUser.isSelected());
                    holder.userName.setChecked(true);
                    Log.d("TAG ","Is Selected "+i+" "+modelUser.isSelected());
                }
                else
                {
                    holder.userName.setChecked(false);
                    holder.userName.setSelected(modelUser.isSelected());
                    Log.d("TAG ","Is Not Selected "+i +modelUser.isSelected());
                }



                holder.userName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                        final boolean isChecked = compoundButton.isChecked();
                        // Log.d("TAG", "Position is Selected " + i + isChecked);

                        if(isChecked){
                            prefsManager.addFavorite(context, modelUser);
                            modelUser.setSelected(true);
                            Log.d("TAG", "Position is Selected " + i + isChecked);

                        }
                        else
                        {

                            prefsManager.removeFavorite(context, modelUser);
                            modelUser.setSelected(false);
                            Log.d("TAG", "Position is unchecked " + i + isChecked);

                        }
                    }
                });
            }


            return convertView;
        }

        public List<ModelUser> getStudentist() {


            return modelUsers;
        }

        public boolean checkFavoriteItem(ModelUser checkProduct) {
            boolean check = false;
            List<ModelUser> favorites = prefsManager.getFavorites(context);
            if (favorites != null) {
                for (ModelUser product : favorites) {
                    if (product.equals(checkProduct)) {
                        check = true;
                        break;
                    }
                }
            }
            return check;
        }

        public void add(ModelUser product) {
            super.add(product);
            modelUsers.add(product);
            notifyDataSetChanged();
        }

        public void remove(ModelUser product) {
            super.remove(product);
            modelUsers.remove(product);
            notifyDataSetChanged();
        }



    }

1 个答案:

答案 0 :(得分:2)

这些被称为var FormBox = React.createClass({ getInitialState: function () { return { photo: [] } }, pressButton: function () { var data = new FormData(); data.append("photo", this.state.photo); // is this the correct way to get file data? }, getPhoto: function (e) { this.setState({ photo: e.target.files[0] }) }, render: function () { return ( <form action='.' enctype="multipart/form-data"> <input type='file' onChange={this.getPhoto}/> <button onClick={this.pressButton}> Get it </button> </form> ) } }) ReactDOM.render(<FormBox />, document.getElementById('root')) ,并且已有数千个示例可用。 一个例子http://stacktips.com/tutorials/android/listview-with-section-header-in-android详细解释。