在Android中搜索无法正常运行的editext

时间:2014-05-05 13:11:02

标签: android search android-listview baseadapter

我做了一个有EditText的活动。当我在EditText中输入任何字母或者文本被更改时,我已经调用了一个用于从webservice中搜索产品的API,我尝试了如下,但问题是它不能正常工作,它不会过滤结果基于EditText内的文本,并且当我更改EditText中的文本时不会更新List。 的 Main.java

public class SearchActivity extends Activity {
    public com.epe.yehki.uc.Menu searchMenu;
    public Header searchHeader;
    public EditText et_serach;
    JSONObject jsonObj;
    private ProgressDialog pDialog;
    Intent in = null;
    String searchUrl;
    int flag;
    public Header header;
    public Menu menu;
    public TextView title;
    Bitmap bitmap;;
    private ProductAdapter productContent;
    private CategoryAdapter categoryContent;
    // PRODUCTS....
    // arrayLists......
    public static ArrayList<String> productArray;
    public static ArrayList<String> categoryArray;

    //

    // contacts JSONArray
    JSONArray subcategories = null;
    JSONArray products = null;
    public String catid;
    public String id;
    public String pid;
    String name;
    ListView lv;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> subcategoryList;
    ArrayList<HashMap<String, String>> productList;

    // new
    public String proname;
    public String prodesc;
    public String proimg;

    public String proMinOrderQty;
    public String proMinPrice;
    public String proMaxPrice;
    public String proTerms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_search);
        productList = new ArrayList<HashMap<String, String>>();
        searchMenu = (com.epe.yehki.uc.Menu) findViewById(R.id.menusearch);
        searchMenu.setSelectedTab(2);
        searchHeader = (Header) findViewById(R.id.headersearch);
        searchHeader.title.setText("Search");
        et_serach = (EditText) findViewById(R.id.et_serach);
        lv = (ListView) findViewById(R.id.serch_list);

        et_serach.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() != 0) {
                    // CALL SEARCH API........!!!
                    new GetSearchList().execute();
                }

            }
        });
    }

    // SEARCH API()..!!!
    private class GetSearchList extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(SearchActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();
            searchUrl = Const.API_PRODUCT + "?product_name=" + et_serach.getText().toString().trim();
            System.out.println(":::::::::::::::::::SUB URL:::::::::::::::::" + searchUrl);
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(searchUrl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);

                    if (jsonObj.has(Const.TAG_PRODUCT_DETAIL)) {
                        System.out.println("::::::::::::::::true::::::::::::::::" + jsonObj.has(Const.TAG_PRODUCT_DETAIL));
                        products = jsonObj.getJSONArray(Const.TAG_PRODUCT_DETAIL);

                        if (products != null && products.length() != 0) {
                            // looping through All Contacts

                            for (int i = 0; i < products.length(); i++) {
                                JSONObject c = products.getJSONObject(i);

                                pid = c.getString(Const.TAG_PRODUCT_ID);
                                System.out.println("::::::::::::::::::PARSING PRODUCT ID:::::::::::::" + pid);
                                String proname = c.getString(Const.TAG_PRODUCT_NAME);
                                String prodesc = c.getString(Const.TAG_LISTING_DESCRIPTION);
                                String proimg = Const.API_HOST + "/" + c.getString(Const.TAG_PRODUCT_IMG);
                                System.out.println("::::::::::::::;products Length:::::::::::" + products.length());
                                System.out.println(":::::::::::::::My Image Url:::::::::::::" + proimg);
                                String proMinOrderQty = c.getString(Const.TAG_PRODUCT_MIN_ORDER_QTY);
                                c.getString(Const.TAG_PRODUCT_MIN_PRICE);
                                c.getString(Const.TAG_PRODUCT_MAX_PRICE);

                                c.getString(Const.TAG_PRODUCT_PAYMENT_TERMS);

                                // for company details..!!!

                                // new Working

                                HashMap<String, String> product = new HashMap<String, String>();

                                product.put(Const.TAG_PRODUCT_ID, pid);
                                product.put(Const.TAG_PRODUCT_NAME, proname);
                                product.put(Const.TAG_PRODUCT_IMG, proimg);
                                product.put(Const.TAG_PRODUCT_MIN_ORDER_QTY, proMinOrderQty);
                                product.put(Const.TAG_PRODUCT_DESCRIPTION, prodesc);
                                productList.add(product);

                            }
                        }
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

            productContent = new ProductAdapter(SearchActivity.this, productList);
            productContent.notifyDataSetChanged();
            lv.setAdapter(productContent);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

  1. 每次向搜索API发出新请求时,请清除productList。否则产品将继续添加到您的列表中进行每次搜索。

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        productList.clear();
        pDialog = new ProgressDialog(SearchActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    
  2. 每次获取新结果时都不需要创建新适配器。在onCreate中声明一个适配器,将该适配器设置为listView。现在,每次发出新请求时,都会将新产品添加到productList,然后调用adapter.notifyDataSetChanged();

    notifyDataSetChanged()将自动刷新初始化时传递给它的列表中的数据。