我如何在listView中显示新结果,而不是每次搜索新数据时都将其显示在另一个下面?

时间:2016-06-20 04:20:43

标签: android sqlite listview

    public void displayItems(View view) {
    String s2 = editText.getText().toString();
    ArrayList<HashMap<String, String>> allItems = dbcontroller.searchdata(s2);
    ArrayList<HashMap<String, String>> a = dbcontroller.getcbrands();
    String firstCompany="",secondCompany="";
    long count = 0;
    long ean_num = Long.parseLong(s2);
    if (allItems.size() == 0) {
        allItems = dbcontroller.getpdata(s2);

        for (HashMap<String, String> map : allItems) {
            //if (map.get("EAN").equals(s2)) {
            //pselected.add(map);
            //if (map.get("EAN")..equals(s2)) {

            long ean_num_pred = Long.parseLong(map.get("EAN"));

            Log.d("coredictor", "the value is " + ean_num);
            Log.d("coredictor", "the predicted value is " + ean_num_pred);
            count = count + 1;
            if (count == 1) {
                firstCompany = map.get("COMPANY");
                selected.add(map);
            }
            if (count == 2) {
                secondCompany = map.get("COMPANY");
            }
        }
        long belowRange = ean_num - 50;
        long aboveRange = ean_num + 50;

        Log.d("coredictor", "the Input is " + ean_num);
        // Log.d("coredictor", "the Ma{ean} is" + ean_num_pred);
        // Log.d("coredictor", "the Below Value is " + belowRange);
        // Log.d("coredictor", "the Abouve Value is " + aboveRange);



        if (firstCompany!=secondCompany) {
            //selected = pselected;
            // String =dbcontroller.insertReport(editText.getText().toString());
          boolean isInserted = dbcontroller.insertReport(editText.getText().toString(),"5");
            //dbcontroller.insertReport("5");
            if (isInserted = true)
                Toast.makeText(Search.this, "Data inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Search.this, "Data not inserted", Toast.LENGTH_LONG).show();
            selected = pselected;
            textView.setText("ITEM UNKNOWN");
            selected.clear();

            textView.setTextColor(Color.RED);
        } else {
            boolean isInserted = dbcontroller.insertReport(editText.getText().toString(),"4");
            if (isInserted = true)
                Toast.makeText(Search.this, "Data inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Search.this, "Data not inserted", Toast.LENGTH_LONG).show();
            selected = pselected;
            textView.setText("ITEM PREDICTED");
            editText.setText("");
            textView.setTextColor(Color.GREEN);
        }


    } else {
        for (HashMap<String, String> map : allItems) {
            if (map.get("EAN").equals(s2)) {
                boolean isInserted = dbcontroller.insertReport(editText.getText().toString(),"1");
                if (isInserted = true)
                    Toast.makeText(Search.this, "Data inserted", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(Search.this, "Data not inserted", Toast.LENGTH_LONG).show();
                selected.add(map);
                textView.setText("ITEM FOUND!!!!!");
                textView.setTextColor(Color.BLUE);
                editText.setText("");

            }

    ListAdapter adapter = new SimpleAdapter(Search.this, selected,R.layout.displays_searchitems, new String[]{"EAN","COMPANY", "NAME","DESCRIPTION", "BRAND", "CBRAND"}, new int[]{
    R.id.ets_ean, R.id.ets_company, R.id.ets_name,   R.id.ets_desc, R.id.ets_brand, R.id.ets_cbrands});
    customList.setAdapter(adapter);

所以这是我的适配器。搜索是我班级的名称。 R.id.ets_ean,R.id.ets_company,....是我正在从数据库显示数据的editText的所有id。 问题是,每次我搜索新结果时,它都会显示一个低于自动刷新的结果。 notifyDataSetChanged()不起作用。

0 个答案:

没有答案