尽管添加到适配器的arraylist

时间:2017-02-06 21:58:54

标签: android listview arraylist

我在列表视图中显示项目时遇到问题,在选择从另一个片段添加文章后,Collections片段应检查它是否为null并将其附加到arraylist。但是,当这样做时,listview只显示最新的更新文章。我不知道怎么回事......

public class CollectionsFragment extends Fragment {

    private List<Article> mArticleList = new ArrayList<>();
    private ListView lvArticles;
    private CollectionAdapter mAdapter;

    public CollectionsFragment() {}

    public static CollectionsFragment newInstance() {
        Bundle args = new Bundle();
        CollectionsFragment fragment = new CollectionsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        new MyTask().execute();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collections, container, false);

        lvArticles = (ListView) view.findViewById(R.id.lvArticles);
        mAdapter = new CollectionAdapter(getContext(), R.layout.row, mArticleList);

        lvArticles.setEmptyView(view.findViewById(R.id.empty));

        lvArticles.setAdapter(mAdapter);

        return view;
    }

    private class MyTask extends AsyncTask<Article, Article, Void> {

        @Override
        protected Void doInBackground(Article... articles) {
            Article item = (Article) getArguments().getSerializable("Article");

            if (item != null) {
                mArticleList.add(item);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mAdapter.notifyDataSetChanged();

            lvArticles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Article item = mArticleList.get(position);
                    Intent intent = new Intent(getContext(), WebViewActivity.class);
                    intent.putExtra("URL", item.getUrl());
                    getContext().startActivity(intent);
                }
            });

            lvArticles.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    Article item = mArticleList.get(position);
                    createDialog(view, item);
                    return true;
                }
            });
        }

        private void createDialog(View view, final Article item) {

            final Dialog d = new Dialog(getContext());
            d.setContentView(R.layout.dialog_collections);
            d.setTitle("Remove article?");
            d.setCancelable(true);

            Button b = (Button) d.findViewById(R.id.button1);
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mArticleList.remove(item);
                    mAdapter.notifyDataSetChanged();
                }
            });

            d.show();
        }
    }
}

适配器代码:

public class CollectionAdapter extends ArrayAdapter {

        private List<Article> articleList;
        private int resource;
        private final LayoutInflater inflater;

        public CollectionAdapter(Context context, int resource, List<Article> objects) {
            super(context, resource, objects);
            articleList = objects;
            this.resource = resource;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

            ViewHolder holder = null;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(resource, null);
                holder.author = (TextView) convertView.findViewById(R.id.a_author);
                holder.title = (TextView) convertView.findViewById(R.id.a_title);
                holder.desc = (TextView) convertView.findViewById(R.id.a_desc);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.author.setText(articleList.get(position).getAuthor());
            holder.title.setText(articleList.get(position).getTitle());
            holder.desc.setText(articleList.get(position).getDescription());

            return convertView;
        }

        @Override
        public int getCount() {
            return articleList != null ? articleList.size() : 0;
        }

        public static class ViewHolder {
            private TextView author;
            private TextView title;
            private TextView desc;
        }

    }

0 个答案:

没有答案