草庐IT

android - notifyDataSetChanged() 不刷新适配器

coder 2023-12-06 原文

我的适配器列表正在广播接收器上刷新。

Everything is working fine if adapter list size is greater than 1 , means if my recyclerview has already one row shwoing then list refreshing just fine . But if list size goes from 0 to 1 then my adapter notify dataset Changed stop working . No data shows on recyclerview. I don't know why it is not working .

Recyclerview 类:

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.job_recyclerview, container, false);
getActivity());
        initialise(v);
        init();
        showNoTaskMessage();
        new loadListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        mMyBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // Here you can refresh your listview or other UI


                SlidingTab.slidingTab.getTabAt(0).setText("New (" + SingleTon.getInstance().getNewjob() + ")");
                SlidingTab.slidingTab.getTabAt(1).setText("In Progress (" + SingleTon.getInstance().getInprogressjob() + ")");;
                SlidingTab.slidingTab.getTabAt(2).setText("Completed (" + SingleTon.getInstance().getCompletedjob() + ")");

            }

        };

        try {

            IntentFilter filter = new IntentFilter("newJob");
            LocalBroadcastManager.getInstance(context).registerReceiver(mMyBroadcastReceiver,
                    filter);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return v;
    }

适配器类:

 public JobAdapter(ArrayList<Info> myDataset, Context context) {
        this.mDataset = myDataset;
        this.mAct = context;
    }

    public void addApplications(ArrayList<Info> candidates) {
        if (this.filterList == null) {
            filterList = new ArrayList<>();
        }
        this.mDataset.clear();
        this.mDataset.addAll(candidates);
        this.filterList.addAll(mDataset);
        this.notifyItemRangeInserted(0, candidates.size() - 1);

    }

    public void clearApplications() {
        int size = this.mDataset.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                mDataset.remove(0);
                filterList.remove(0);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return VIEW_NORMAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_job_card, parent, false);
        ViewHolder fh = new ViewHolder(v);
        return fh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

//        holder.jobPhone.setText(mDataset.get(position).mobileNo);
        holder.jobNumber.setText(mDataset.get(position).jobNumber);
        holder.jobTime.setText(mDataset.get(position).time);
        holder.jobAddress.setText(mDataset.get(position).address);
//        holder.jobInstructionText.setText(mDataset.get(position).spclInstruction);

        if (mDataset.get(position).jobStatus != null && mDataset.get(position).jobStatus.equalsIgnoreCase("Completed")) {
            holder.endsat.setText("Submitted at");
            holder.jobTime.setText(mDataset.get(position).completedOnString);
            holder.jobTimeLeft.setVisibility(View.INVISIBLE);
            holder.timerImage.setVisibility(View.INVISIBLE);

        } else {
            if (mDataset.get(position).status.equalsIgnoreCase("Active")) {
                holder.jobTimeLeft.setText(mDataset.get(position).appointmentTime);
            } else {
                holder.jobTimeLeft.setText("-" + mDataset.get(position).appointmentTime);
            }
        }

        holder.jobLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SingleTon.getInstance().setWorkDescHolder(mDataset.get(position).descHolder);
                FragmentManager fragmentManager = ((FragmentActivity) mAct).getSupportFragmentManager();
                FragmentTransaction ft = ((FragmentActivity) mAct).getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out);
                ft.replace(R.id.content_frame1, new DetailsFragment(), "persondetails");
                ft.addToBackStack("persondetails");

                // Start the animated transition.
                ft.commit();

            }
        });


    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView jobNumber, jobTimeLeft, jobStatus, jobAddress, jobEmail, jobPhone, timeTimer, jobInstructionText, jobTime, endsat;
        private ImageView timerImage;
        private FrameLayout frameLayout;
        private CardView cardView;
        private LayoutRipple jobLayout1;

        public ViewHolder(View v) {
            super(v);
            this.jobNumber = (TextView) v.findViewById(R.id.job_number);
            this.jobTime = (TextView) v.findViewById(R.id.job_time);
            this.jobTimeLeft = (TextView) v.findViewById(R.id.job_timertext);
            this.timerImage = (ImageView) v.findViewById(R.id.timerimage);
            this.cardView = (CardView) v.findViewById(R.id.card_view);
//            this.jobStatus = (TextView) v.findViewById(R.id.job_status);
            this.jobAddress = (TextView) v.findViewById(R.id.job_addresstext);
//            this.jobInstructionText = (TextView) v.findViewById(R.id.instruction_text);
//            this.jobLayout = (LayoutRipple)v.findViewById(R.id.job_cardLayout);
            this.jobLayout1 = (LayoutRipple) v.findViewById(R.id.cardLayout1);
            this.endsat = (AppCompatTextView) v.findViewById(R.id.endsat);
            this.jobNumber.setTypeface(Utils.RegularTypeface(mAct));
            this.jobAddress.setTypeface(Utils.RegularTypeface(mAct));
            this.jobTimeLeft.setTypeface(Utils.RegularTypeface(mAct));
            this.jobTime.setTypeface(Utils.RegularTypeface(mAct));
        }
    }
}

请帮我找出错误或其他方法。谢谢

最佳答案

BroadcastReceiveronReceive()中调用数据加载任务

    mMyBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Here you can refresh your listview or other UI
           new loadListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

            SlidingTab.slidingTab.getTabAt(0).setText("New (" + SingleTon.getInstance().getNewjob() + ")");
            SlidingTab.slidingTab.getTabAt(1).setText("In Progress (" + SingleTon.getInstance().getInprogressjob() + ")");;
            SlidingTab.slidingTab.getTabAt(2).setText("Completed (" + SingleTon.getInstance().getCompletedjob() + ")");

        }

    };

同时在您的 Adapter 类中进行以下更改。

   public void addApplications(ArrayList<Info> candidates) {
        if (this.filterList == null) {
            filterList = new ArrayList<>();
        }
        this.mDataset.clear();
        this.mDataset.addAll(candidates);
        this.filterList.addAll(mDataset);
        this.notifyItemRangeInserted(0, candidates.size());

    }

    public void clearApplications() {
        int size = this.mDataset.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                mDataset.remove(i);
                filterList.remove(i);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }

希望有用!

关于android - notifyDataSetChanged() 不刷新适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843786/

有关android - notifyDataSetChanged() 不刷新适配器的更多相关文章

  1. ruby - capybara field.has_css?匹配器 - 2

    我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby-on-rails - 我应该使用哪个适用于 Ruby 的 CouchDB 适配器? - 2

    一些我找到的选项是ActiveCouchCouchRESTCouchPotatoRelaxDBcouch_foo我更喜欢GitHub上的项目,因为这让我更容易fork和推送修复。所有这些都符合该要求。我习惯了Rails,所以我喜欢像ActiveRecord模型一样工作的东西。另一方面,我也不希望我和Couch之间太多--毕竟我使用它作为我的数据库是有原因的。最后,它们似乎都得到了相当积极的维护(couch_foo可能是个异常(exception))。所以我想这归结为(不可否认和不幸的)主观:有没有人对他们有过好的或坏的经历? 最佳答案

  4. ruby-on-rails - 具有六边形架构和 DCI 模式的框架和数据库适配器 - 2

    我尝试用Ruby设计一个基于Web的应用程序。我开发了一个简单的核心应用程序,在没有框架和数据库的情况下在六边形架构中实现DCI范例。核心六边形中有小六边形和网络,数据库,日志等适配器。每个六边形都在没有数据库和框架的情况下自行运行。在这种方法中,我如何提供与数据库模型和实体类的关系作为独立于数据库的关系。我想在将来将框架从Rails更改为Sinatra或数据库。事实上,我如何在这个核心Hexagon中实现完全隔离的rails和mongodb的数据库适配器或框架适配器。有什么想法吗? 最佳答案 ROM呢?(Ruby对象映射器)。还有

  5. ruby-on-rails - 具有多个参数的 RSpec 和自定义匹配器 - 2

    我正在尝试使用RSpec为我在RoR中的测试创建自定义匹配器。define:be_accessibledo|attributes|attributes=attributes.is_a?(Array)?attributes:[attributes]attributes.eachdo|attribute|matchdo|response|response.class.accessible_attributes.include?(attribute)enddescription{"#{attribute}shouldbeaccessible"}failure_message_for_shou

  6. ruby - 有没有办法将 html 刷新到 Sinatra 中的线路 - 2

    我有一个Sinatra应用程序,它有一个长时间运行的进程(网络抓取工具)。我希望应用程序在爬虫运行时而不是在结束时刷新爬虫进度的结果。我已经考虑过fork请求并使用ajax做一些有趣的事情,但这是一个非常基本的单页应用程序,实际上只需要在它发生时将日志输出到浏览器。有什么建议吗? 最佳答案 更新(2012-03-21)从Sinatra1.3.0开始,您可以使用新的流式API:get'/'dostreamdo|out|out旧答案不幸的是,您没有可以简单地刷新到的流(这不适用于Rack中间件)。从路由block返回的结果可以简单地响应

  7. ruby - Minitest 规范自定义匹配器 - 2

    我的测试中有一行:page.has_reply?("myreply").must_equaltrue为了使其更具可读性,我想使用自定义匹配器:page.must_have_reply"myreply"基于https://github.com/zenspider/minitest-matchers的文档我希望我需要编写一个看起来像这样的匹配器:defhave_reply(text)subject.has_css?('.comment_body',:text=>text)endMiniTest::Unit::TestCase.register_matcher:have_reply,:hav

  8. ruby-on-rails - 使用 PostgreSQL 适配器限制 ActiveRecord 迁移 5.0 中的文本列 - 2

    我的迁移看起来像这样classCreateQuestionings现在,当我运行$rakedb:migrate:reset时,在我的db/schema.rb中看不到限制:create_table"questionings",force::cascadedo|t|t.text"body",null:falseend我做错了吗还是这是一个错误?顺便说一下,我使用的是rails5.0.0.beta3和ruby​​2.3.0p0。 最佳答案 t.text在PostgreSQL和textdoesn'tallowforsizelimits中生成

  9. ruby - Rspec 3 与 Rspec 2 匹配器 - 2

    学习如何使用Rspec3。我对匹配器有疑问。我正在学习的教程基于Rspec2。describeTeamdoit"hasaname"do#Team.new("Randomname").shouldrespond_to:nameexpect{Team.new("Randomname")}.tobe(:name)endit"hasalistofplayers"do#Team.new("Randomname").players.shouldbe_kind_ofArrayexpect{Team.new("Randomname").players}.tobe_kind_of(Array)enden

  10. ruby - 需要帮助改进 Ruby DSL 以控制 Arduino 控制的饮料分配器(bar monkey) - 2

    我正在用Ruby编写DSL来控制我正在处理的Arduino项目;巴尔迪诺。这是一只酒吧猴子,将由软件控制来提供饮料。Arduino通过串行端口接收命令,告诉Arduino要打开什么泵以及打开多长时间。它目前正在读取一个食谱(见下文)并将其打印出来。串行通信的代码以及我在下面提到的其他一些想法仍然需要改进。这是我的第一个DSL,我正在处理之前的示例,所以它的边缘非常粗糙。任何批评、代码改进(是否有任何关于RubyDSL最佳实践或习语的良好引用?)或任何一般性评论。我目前有DSL的粗略草稿,因此饮料配方如下所示(Githublink):desc"Simpleglassofwater"rec

随机推荐