另一个Adapter.notifyDataSetChanged()无效 - Android

时间:2018-04-24 17:18:30

标签: android list listview adapter

我一直在使用一种方法来填充适配器,如下所示:

  1. onCreate(...)
  2. 中的自定义方法中设置UI组件

    //..
    mRecentSales = new ArrayList<>();
            recentSaleAdapter = new RecentSaleAdapter(getApplicationContext(), R.layout.row_recent_sale, mRecentSales);
            lvRecentSales.setAdapter(recentSaleAdapter);
    //.
    
    1. onResume()
    2. 之后,在另一个线程中加载适配器的数据

      @Override
      public void onLoadingPrepare() {
          super.onLoadingPrepare();
          if (qDaySales == null)
              qDaySales = new ArrayList<>();
          else
              qDaySales.clear();
          if (mRecentSales == null)
              mRecentSales = new ArrayList<>();
          else
              mRecentSales.clear();
      }
      
      @Override
      public void onLoading() {
          super.onLoading();
          todayIncome = Conversor.asCurrency(presenter.getDaySales());
          performance = presenter.getImprovement(getApplicationContext());
          for (int i = 1; i<= DateTime.now().getDayOfMonth(); i++){
              qDaySales.add(new Entry(i, presenter.getSalesFromDay(i)));
          }
          mRecentSales.addAll(presenter.getRecentSales());
      }
      
      @Override
      public void onLoadingComplete() {
          super.onLoadingComplete();
          tvTodayIncome.setText(todayIncome);
          tvPerformance.setText(performance);
          setPerformanceChart(qDaySales);
          recentSaleAdapter.notifyDataSetChanged();
      }
      

      这是适配器代码:

      public class RecentSaleAdapter extends ArrayAdapter<MTicket> {
      
      public RecentSaleAdapter(@NonNull Context context, int resource, @NonNull List objects) {
          super(context, resource, objects);
      }
      
      @NonNull
      @Override
      public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
          if (convertView == null){
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_recent_sale, null);
          }
          MTicket ticket = getItem(position);
          TextView tvTicketId = (TextView)convertView.findViewById(R.id.tv_ticket_id);
          TextView tvTicketReference = (TextView)convertView.findViewById(R.id.tv_ticket_reference);
          TextView tvTicketTotal = (TextView)convertView.findViewById(R.id.tv_ticket_total);
          if (ticket != null){
              tvTicketId.setText(String.format("# %d", ticket.id));
              tvTicketReference.setText(ticket.ticketReference);
              tvTicketTotal.setText(ticket.maskSaleTotal);
          }
          return convertView;
      }
      
      @Override
      public long getItemId(int position) {
          MTicket ticket = getItem(position);
          if (ticket != null)
              return ticket.id;
          return -1;
      }
      

      }

      结果不是我预期的,因为List没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

我结束了生活&#39;使用异步任务。

它似乎有一个错误,Android只是不能正确处理回调。

这是由此产生的解决方法:

  1. 我只是在主线程中调用所有需要的方法,而不是在onLoading()方法中填充对象列表。

    @Override
    protected void onResume() {
        super.onResume();
        setTitle(getPreferenceString(SettingsActivity.KEY_BUSINESS_NAME,
            getString(R.string.set_default_business_name)));
        start(); // <- This will start the Async Task
        mRecentSales.clear();
        mRecentSales.addAll(presenter.getRecentSales());
        recentSaleAdapter.notifyDataSetChanged();
    }