如何防止我的过滤列表清除?

时间:2018-02-17 04:11:07

标签: android

我有一个Android应用程序,我有一个自定义适配器。我想过滤我的数据但是当我清除编辑文本时,我的列表变成空白。

我希望在清除编辑文本时显示原始列表视图。我认为我的原始列表也在某些代码中被清除,但我不明白在哪里。

 public class Adapter_SelectedDetail extends ArrayAdapter<DataModel> implements Filterable {  
        private final Context context;  
        ArrayList<DataModel> MyList=null,newList=null;  
        private MyFilter filter;  
        DataModel dataModel;  
        EditText Mysearch;  
        ListView listView;    
        String searchdata;  
        private static LayoutInflater inflater=null;
        public Adapter_SelectedDetail(Context context, EditText search, ArrayList MyList1, ListView listView) {
            super(context, -1, MyList1);
            this.newList = new ArrayList<DataModel>();
            this.newList.addAll(newList);
            this.Mysearch=search;
            this.listView=listView;
            this.MyList = new ArrayList<DataModel>();
            this.MyList.addAll(newList);
            this.MyList=MyList1;
            this.context=context;
        }
        @Override
        public Filter getFilter() {
            if (filter == null){
                filter  = new MyFilter();
            }
            return filter;
        }

    @Override
    public View getView(final int position, View ConverView, ViewGroup parent) {
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        DataModel dataModel = (DataModel) getItem(position);
        View rowView = ConverView;
        // TODO Auto-generated method stub
        TextView  CompanyName,CompanyMobile,Bill_no,Bill_date,PartyName,Transport,City,
                State,CustomerMobileNumber,AgentName,LRno,LRdate,Item,Shades,
                AgentMobileNumber,InvoicePDF,PartyEmail,agentEmail;
        if (rowView==null) {
            rowView = inflater.inflate(R.layout.selected_detail_list_view, parent,false);
        }
        dataModel=MyList.get(position);
        if (dataModel!=null) {
            CompanyName = (TextView) rowView.findViewById(R.id.txt_heading_of_detail);
            CompanyMobile = (TextView) rowView.findViewById(R.id.txt_subheading_of_detail);
            Bill_no = (TextView) rowView.findViewById(R.id.txt_bill_no);
            Bill_date = (TextView) rowView.findViewById(R.id.txt_bill_date);
            PartyName = (TextView) rowView.findViewById(R.id.txt_party_name);
            Transport = (TextView) rowView.findViewById(R.id.txt_transport);
            City = (TextView) rowView.findViewById(R.id.txt_city);
            State = (TextView) rowView.findViewById(R.id.txt_state);
            Shades = (TextView) rowView.findViewById(R.id.txt_shades);
            CustomerMobileNumber = (TextView) rowView.findViewById(R.id.txt_customer_mob_no);
            AgentName = (TextView) rowView.findViewById(R.id.txt_agent);
            AgentMobileNumber = (TextView) rowView.findViewById(R.id.txt_agent_mob_number);
            LRdate = (TextView) rowView.findViewById(R.id.txt_lr_date);
            LRno = (TextView) rowView.findViewById(R.id.txt_lr_number);
            Item = (TextView) rowView.findViewById(R.id.txt_item);
            agentEmail = (TextView) rowView.findViewById(R.id.txt_agent_Email);
            PartyEmail = (TextView) rowView.findViewById(R.id.txt_party_email);

            // Toast.makeText(context, MyList1.get(2).toString(), Toast.LENGTH_SHORT).show();
            CompanyMobile.setText(dataModel.getCompanyMobile());
            City.setText(dataModel.getCity());
            State.setText(dataModel.getState());
            CompanyName.setText(dataModel.getCompanyName());
            Bill_no.setText(dataModel.getBill_no());
            PartyName.setText(dataModel.getPartyName());
            Transport.setText(dataModel.getTransport());
            Shades.setText(dataModel.getShades());
            CustomerMobileNumber.setText(dataModel.getCustomerMobileNumber());
            AgentName.setText(dataModel.getAgentName());
            AgentMobileNumber.setText(dataModel.getAgentMobileNumber());
            LRdate.setText(dataModel.getLRdate());
            LRno.setText(dataModel.getLRno());
            Item.setText(dataModel.getItem());
            agentEmail.setText(dataModel.getAgentEmail());
            PartyEmail.setText(dataModel.getPartyEmail());
        }
                return rowView;
    }

public class MyFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if(constraint != null && constraint.toString().length() > 0)
        {
            ArrayList<DataModel> filteredItems = new ArrayList<DataModel>();
            for(int i = 0, l = MyList.size(); i < l; i++)
            {
                DataModel dataModel = MyList.get(i);
                if(dataModel.getShades().toString().toLowerCase().contains(constraint))
                    filteredItems.add(dataModel);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        }
        else
        {
            synchronized(this)
            {
                result.values = MyList;
                result.count = MyList.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {

        newList = (ArrayList<DataModel>)results.values;
        notifyDataSetChanged();
        clear();
            for (int i = 0, l = newList.size(); i < l; i++)
                add(newList.get(i));
            notifyDataSetInvalidated();

    }
}


}


public class Selected_Detail_Activity extends Activity {
    @BindView(R.id.select_listview)
    ListView selected_list;
    @BindView(R.id.search)
    EditText search;
    String item, mobile, pass, partyName;
    ArrayList<DataModel> MyList1;
        Context context;
    DataModel dataModel=new DataModel();
    @BindView(R.id.txt_party_name)
    TextView txtparty_Name;
    ConnectionClass connectionClass;
     Adapter_SelectedDetail adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selected_detail);
        ButterKnife.bind(this);
        item = getIntent().getStringExtra("item");
        mobile = getIntent().getStringExtra("mobile");
        pass = getIntent().getStringExtra("pass");
        partyName = getIntent().getStringExtra("partyName");
        txtparty_Name.setText(partyName);
        connectionClass = new ConnectionClass();
        context=this;
        selected_list.setAdapter(adapter);

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
            } else {
                PreparedStatement statement = con.prepareStatement("EXEC " + pass + "  " + item + " , " + mobile + "");
                MyList1 = new ArrayList<DataModel>();
                ResultSet rs = statement.executeQuery();
                while (rs.next()) {
                    dataModel.setBill_date(rs.getString("bill_date"));
                    dataModel.setCompanyMobile(rs.getString("CompanyMobile"));
                    dataModel.setCity(rs.getString("city"));
                    dataModel.setState(rs.getString("state"));
                    dataModel.setShades(rs.getString("Shades"));
                    dataModel.setAgentMobileNumber(rs.getString("agentMobNumber"));
                    MyList1.add(dataModel);
                    dataModel=new DataModel();
                    adapter=new Adapter_SelectedDetail(this,search,MyList1,selected_list);
                    selected_list.setAdapter(adapter);
                    search.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }
                        @Override
                        public void afterTextChanged(Editable s) {
                            Selected_Detail_Activity.this.adapter.getFilter().filter(s.toString());


                        }
                    });
                }


            }
        }
        catch (Exception e) {
            Log.e("123", "error", e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您滥用数据集。即,您已删除所有原始数据。

首先,您尚未设置备份数据,请将适配器构造函数替换为此。

public Adapter_SelectedDetail(Context context, EditText search, ArrayList MyList1, ListView listView) {
    super(context,-1);
    this.newList = new ArrayList<>();
    this.newList.addAll(MyList1);
    this.Mysearch = search;
    this.listView = listView;
    this.MyList = new ArrayList<>();
    this.MyList.addAll(MyList1);
    this.context = context;
}

并向您的适配器添加覆盖函数getCount。

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

然后,

......
@Override
protected FilterResults performFiltering(CharSequence constraint) {
    constraint = constraint.toString().toLowerCase();
    FilterResults result = new FilterResults();
    if(constraint != null && constraint.toString().length() > 0)
    {
      ArrayList<DataModel> filteredItems = new ArrayList<DataModel>();
      for(int i = 0, l = newList.size(); i < l; i++)
      {
        DataModel dataModel = newList.get(i);
     if(dataModel.getShades().toString().toLowerCase().contains(constraint))
            filteredItems.add(dataModel);
    }
    result.count = filteredItems.size();
    result.values = filteredItems;
}else
  {
    synchronized(this)
     {
        result.values = newList;
        result.count = newList.size();
     }
   }
  return result;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
                              FilterResults results) {

    MyList= (ArrayList<DataModel>)results.values;
    notifyDataSetChanged();
}
.....

然后它工作正常, 祝你好运。