简体   繁体   中英

Remove or Clear content in SimpleAdapter in Android?

Here is the code:

ibtSearchStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            try{
                searchQuery = etSearchThis.getText().toString();
                searchQuery = searchQuery.toUpperCase();
                cursor = searchActivity.getData(product, "product", tableColumns);

                //Clean ArrayList
                resultRow.clear();
                resultTable.clear();

                //Get Search Result
                resultTable  = searchActivity.searchByProductName(cursor, searchQuery);

                //Display Search Result
                for(int ctr = 0; ctr < resultTable.size(); ctr++){
                    HashMap<String, String> map = new HashMap<String, String>();
                    resultRow = resultTable.get(ctr);
                    String result = resultRow.get(2);
                    map.put("ProductName",result);
                    list.add(map);

                }
                Log.e("resultProduct", "" + list);
                adapter = new SimpleAdapter(
                        SearchMain.this,
                        list,
                        R.layout.search_result,
                        new String[]{"ProductName"},
                        new int[]{R.id.tvProductName}
                    );
                lvSearchResult.setAdapter(adapter);     

            }
            finally{
                product.close();
            }
        }
    });

The function of this is that it will search for a match in the database then it will insert the result of the search in a HashMap then on a adapter.

But every click i am not able to remove the previous result. What is the proper implementation to this?

当您使用适配器并且信息更改时,请确保正在调用adapter.notifyDataSetChanged();

You set a new adapter each time the OnClick event is triggered. As Jay Snayder wrote you should use adapter.notifyDataSetChanged(); instead. But be sure to set the adapter only once and move the following part of your code (eg to your onCreate() method of the activity):

adapter = new SimpleAdapter(
                    SearchMain.this,
                    list,
                    R.layout.search_result,
                    new String[]{"ProductName"},
                    new int[]{R.id.tvProductName}
                );
            lvSearchResult.setAdapter(adapter);

Just update the DataSet of your adapter (here: 'list').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM