我的listview显示了它在android中重复项目的前5项

时间:2015-10-05 09:34:24

标签: android arraylist android-viewholder

嗨每一个我面临android问题的我需要的是我在我的Fragment中有一个列表视图,它是用数据库中的值动态设置的,我的列表视图视图动态更新列表我有8个项目列表显示前5个项目并重复从第一个我的代码就像this.please帮助我在此提前感谢。请帮助我在这我不能找到什么是错误。 我已经使用Baseadapter.Thanks提前创建了列表视图

class FashionAdapter extends BaseAdapter
    {
        Context context;
        String redu[];
        JSONArray article;
        LayoutInflater inflater;

        private List<Fashion> articlelist;
        FashionAdapter(JSONArray article, Context context) {
            this.context = context;
            this.article = article;
            //this.content=content;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return article.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return article.getJSONObject(position);
            } catch (JSONException e) {

                e.printStackTrace();
                return null;
            }
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
              ViewHolder holder;
            Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/eenadu-webfont.ttf");

            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.fashoinlist_item, null);
                Log.d("item postion",""+ position);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
                JSONObject FasionObject;
                try {
                    FasionObject=article.getJSONObject(position);
                    holder.fTitle.setText(FasionObject.getString("articletitle"));
                    holder.fsadec.setText(FasionObject.getString("articleshortdec"));
                    holder.fTitle.setTypeface(font);
                    holder.fsadec.setTypeface(font);
                     String articlThumImage=FasionObject.getString("articlethumbimg");
                     Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            else{
                holder = (ViewHolder) convertView.getTag(); // return last set view of ith item
            }

            return convertView;
        }

    }

我写了这样的视图hloder类,在这里我充分利用了所有的观点。         class ViewHolder {

        ImageView fImage;
        TextView fsadec,fTitle;
        public ViewHolder(View itemView) {
            fImage = (ImageView) itemView.findViewById(R.id.fashion_image);
            fsadec=(TextView) itemView.findViewById(R.id.fashion_sdec);
            fTitle=(TextView) itemView.findViewById(R.id.fashion_title);
        }

请帮助我,我无法找到错误。

2 个答案:

答案 0 :(得分:0)

如果阻止

,则在代码外移动
 JSONObject FasionObject;
            try {
                FasionObject=article.getJSONObject(position);
                holder.fTitle.setText(FasionObject.getString("articletitle"));
                holder.fsadec.setText(FasionObject.getString("articleshortdec"));
                holder.fTitle.setTypeface(font);
                holder.fsadec.setTypeface(font);
                 String articlThumImage=FasionObject.getString("articlethumbimg");
                 Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

并写下else else语句

  else{
            holder = (ViewHolder) convertView.getTag();
     } // return last set view of ith item
            JSONObject FasionObject;
            try {
                FasionObject=article.getJSONObject(position);
                                      holder.fTitle.setText(FasionObject.getString("articletitle"));
                holder.fsadec.setText(FasionObject.getString("articleshortdec"));
                holder.fTitle.setTypeface(font);
                holder.fsadec.setTypeface(font);
                 String articlThumImage=FasionObject.getString("articlethumbimg");
                 Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

答案 1 :(得分:0)

尝试使用创建适配器类 https://www.buzzingandroid.com/tools/android-layout-finder/ 并检查从数据库输入的项目数量是否合适。