Complete APP project source code (including services) - APP search function

The third lesson of TPshop complete APP project (including server source code) - shopping mall search function.

See the picture below:



Function description: Click the top text input box to pop up the soft keyboard. After entering the keyword, click the "Search" button on the keyboard to request the background search API

and display the results in a list.

Structural analysis: From top to bottom, the top is a searchView of a custom View, the next is a list of hot words set in the background, and the following is a listview of search records

Top SPSearchView

 

public class SPSearchView extends LinearLayout {

	private ImageView backImgv;
	private EditText searchEdtv;
	private SPSearchViewListener searchListener;

	/**
	 * @param context
	 * @param attrs
	 */
	public SPSearchView(Context context, AttributeSet attrs) {
		super(context, attrs);
	    View view = LayoutInflater.from(context).inflate(R.layout.search_heard_view, this);
		searchEdtv = (EditText)view.findViewById(R.id.search_edtv);
		backImgv = (ImageView)view.findViewById(R.id.back_imgv);
		searchEdtv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (searchListener != null) {
					//searchListener.onSearchBoxClick(searchEdtv.getText().toString());
				}
			}
		});
		backImgv.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (searchListener != null) searchListener.onBackClick();
			}
		});

		searchEdtv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { //Response to keyboard "search" key
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {

					notifyStartSearching(textView.getText().toString());
				}
				return true;
			}
		});
	}

	/**
	 * Notify listeners to perform search operations
	 * @param text
	 */
	private void notifyStartSearching(String text){
		if (searchListener != null) {
			searchListener.onSearchBoxClick(text);
		}
	}

	public void setSearchKey(String searchKey){
		if (this.searchEdtv!=null && !SPStringUtils.isEmpty(searchKey)){
			this.searchEdtv.setText(searchKey);
		}
	}

	public EditText getSearchEditText(){
		return this.searchEdtv;
	}

	public void setSearchViewListener(SPSearchViewListener listener){
		this.searchListener = listener;
	}

	public interface SPSearchViewListener{
		public void onBackClick();
		public void onSearchBoxClick(String keyword);
	}

}

The implementation of the search interface Activity:

layout file:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.soubao.tpshop.view.SPSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="50dip" />

    <ListView
        android:id="@+id/search_key_listv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/separator_line"
        android:dividerHeight="1px"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:layout_marginTop="50dip"
        android:layout_marginBottom="50dip"
        android:scrollbars="none"
        android:smoothScrollbar="true" />

    <Button
        android:id="@+id/search_delete_btn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_button"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginBottom="10dip"
        style="@style/textStyle.Normal.black"
        android:text="@string/delete_history"
        android:background="@drawable/tag_button_bg_unchecked"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

Set custom title in onCreate

 

@Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate (savedInstanceState);
    }

Search records are cached in SharedPrefreences

 

public void loadKey(){
        mSearchkeys = new ArrayList<String>();
        String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY);
        if (!SPStringUtils.isEmpty(searchKey)){
            String[] keys = searchKey.split(",");
            if (keys !=null)
                for(int i=0; i< keys.length; i++){
                    if (!SPStringUtils.isEmpty(keys[i])){
                        mSearchkeys.add(keys[i]);
                    }
                }
        }

    }

    public void saveKey(String key){
        String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY);
        if (!SPStringUtils.isEmpty(searchKey) && !searchKey.contains(key)) {
            searchKey+=","+key;
        }else{
            searchKey = key;
        }
        SPSaveData.putValue(this, SPMobileConstants.KEY_SEARCH_KEY, searchKey);
    }

Complete source code download address:  demo tpshop Android source code download

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565673&siteId=291194637
Recommended