如何在Android中实现Search Activity

废话少说,先放效果图:


Layout文件内容如下:

这里要注意的是ListView的id一定要命名为 “@android:id/list",否则会出现异常。

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/txtSearchKeyword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <requestFocus >
            </requestFocus>
        </EditText>

        <Button
            android:id="@+id/btnSearch"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="搜索" >
        </Button>
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <!--
             Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it
        -->

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" />

        <!-- Here is the view to show if the list is emtpy -->

        <TextView
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="test" />
    </FrameLayout>

</LinearLayout>

Activity的Java代码如下:

其中用到的DBHelper是自己实现的数据库辅助类,用到的search方法请大家自行实现。

package xxxx.xxxxx;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.TextView;


public class SearchActivity extends ListActivity{


	DBHelper db;
	Context context = null;
	BibleArrayAdapter<String> mAdapter;
	Button btnSearch;
	EditText txtSearchKeyword;
	java.lang.String mKeyword = null;
	
	private String[] mStrings = new String[] {};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		
		setContentView(R.layout.search_activity);

		// Tell the list view which view to display when the list is empty
		getListView().setEmptyView(findViewById(R.id.empty));
		
 		txtSearchKeyword = (EditText) findViewById(R.id.txtSearchKeyword);
        
		//为搜索按钮添加事件
		btnSearch = (Button) findViewById(R.id.btnSearch);		
		btnSearch.setOnClickListener(mSearchButtonListener);
		
		setListAdapter(getMyListAdapter());
	}
	

	private OnClickListener mSearchButtonListener = new OnClickListener() {
		public void onClick(View v) {
			mAdapter.Search(txtSearchKeyword.getText().toString());
		}
	};
	
	private ListAdapter getMyListAdapter() {
		mAdapter = new BibleArrayAdapter<String>(this,
				R.layout.my_list_view, mStrings);
		return mAdapter;
	}
	
	@SuppressWarnings({ "hiding", "rawtypes" })
	class BibleArrayAdapter<String> extends ArrayAdapter {

		@SuppressWarnings("unchecked")
		public BibleArrayAdapter(SearchActivity searchActivity, int resource,
				Object[] objects) {
			super(searchActivity, resource, objects);
		}

		/**
		 * 重画listview中每个Item
		 */
		public View getView(int position, View convertView, ViewGroup parent) {

			View row = convertView;
			java.lang.String content;
			java.lang.String label;

			StringBuffer sb = new StringBuffer(mStrings[position]);

			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.c_row, parent, false);
			}
					
			content = sb.toString();
	
			TextView tvContent = (TextView) row.findViewById(R.id.content);
			tvContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Constant.fontsize);
			tvContent.setTextColor(Color.BLACK);
			tvContent.setBackgroundColor(Color.WHITE);
			tvContent.setPadding(0, 5, 0, 5);

			//高亮显示关键字
			tvContent.setText(content,TextView.BufferType.SPANNABLE);
			if (content.indexOf(mKeyword) >= 0) {
				Spannable sp = (Spannable) tvContent.getText();  
				sp.setSpan(new ForegroundColorSpan(Color.RED), content.indexOf(mKeyword), content.indexOf(mKeyword)+mKeyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

			return row;
		}
		
		/**
		 * 搜索框,写在内部类中是因为要用到notifyDataSetChanged方法
		 * @param keyword
		 */
		public void Search(java.lang.String keyword) {
			db = new DBHelper(context);
			try {
				mKeyword = keyword;
				mStrings = db.search(keyword);
				setListAdapter(getMyListAdapter());
				notifyDataSetChanged();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (db != null) db.close();
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值