Android单词查找器

三个布局文件: main.xml




    







		
 
  

 line.xml

 

 

 




		
	


list.xml



    
    




ACTIVITY文件

首先是数据库类

package xuyan.sword.test;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ResultActivity extends  Activity{
 ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		listview=(ListView)this.findViewById(R.id.listView1);
		
		Intent  intent=getIntent();
		//获取intent的数据
		Bundle  data=intent.getExtras();
		//从Bundle包中取出数据
		
		@SuppressWarnings("unchecked")
		List>  list=(List>)data.getSerializable("data");
		
		//将LIST封装成SIMPLEADAPTER
		
		SimpleAdapter  adapter=new SimpleAdapter(ResultActivity.this, list, R.layout.line, new String[]{"word","detail"}, new int[]{R.id.word,R.id.detail});
		
		//填充LISTVIEW
		
		listview.setAdapter(adapter);
		
	}

}

 

 

 

 

package xuyan.sword.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchWordActivity extends Activity {
    /** Called when the activity is first created. */
	
	MyDataBaseHelper dbHelper;
	Button insert = null;
	Button search = null;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);		
		// 创建MyDatabaseHelper对象,指定数据库版本为1,此处使用相对路径即可,
		// 数据库文件自动会保存在程序的数据文件夹的databases目录下。
		dbHelper = new MyDataBaseHelper(this, "myDict.db3", 1);
		insert = (Button)findViewById(R.id.button1);
		search = (Button)findViewById(R.id.button2);	
		insert.setOnClickListener(new OnClickListener()
		{
			public void onClick(View source)
			{
				//获取用户输入
				String word = ((EditText)findViewById(R.id.editText1))
					.getText().toString();
				String detail = ((EditText)findViewById(R.id.editText2))
					.getText().toString();
				//插入生词记录
				insertData(dbHelper.getReadableDatabase() , word , detail);
				//显示提示信息
				Toast.makeText(SearchWordActivity.this, "添加生词成功!" , 8000)
					.show();
			}			
		});	

		search.setOnClickListener(new OnClickListener()
		{
			public void onClick(View source)
			{
				// 获取用户输入
				String key = ((EditText) findViewById(R.id.editText3)).getText()
					.toString();
				// 执行查询
				Cursor cursor = dbHelper.getReadableDatabase().rawQuery(
					"select * from dict where word like ? or detail like ?", 
					new String[]{"%" + key + "%" , "%" + key + "%"});
				
				//创建一个Bundle对象
				Bundle data = new Bundle();
				data.putSerializable("data", converCursorToList(cursor));
				//创建一个Intent
				Intent intent = new Intent(SearchWordActivity.this
					, ResultActivity.class);
				intent.putExtras(data);
				//启动Activity
				startActivity(intent);
			}
		});
	}

	protected ArrayList>
		converCursorToList(Cursor cursor)
	{
		ArrayList> result = 
			new ArrayList>();
		//遍历Cursor结果集
		while(cursor.moveToNext())
		{
			//将结果集中的数据存入ArrayList中
			Map map = new 
				HashMap();
			//取出查询记录中第2列、第3列的值
			map.put("word" , cursor.getString(1));
			map.put("detail" , cursor.getString(2));
			result.add(map);
		}
		return result;		
	}
	private void insertData(SQLiteDatabase db
		, String word , String detail)
	{
		//执行插入语句
		db.execSQL("insert into dict values(null , ? , ?)"
			, new String[]{word , detail});
	}
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		//退出程序时关闭MyDatabaseHelper里的SQLiteDatabase
		if (dbHelper != null)
		{
			dbHelper.close();
		}
	}
}


 


 

package xuyan.sword.test;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ResultActivity extends  Activity{
 ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		listview=(ListView)this.findViewById(R.id.listView1);
		
		Intent  intent=getIntent();
		//获取intent的数据
		Bundle  data=intent.getExtras();
		//从Bundle包中取出数据
		
		@SuppressWarnings("unchecked")
		List>  list=(List>)data.getSerializable("data");
		
		//将LIST封装成SIMPLEADAPTER
		
		SimpleAdapter  adapter=new SimpleAdapter(ResultActivity.this, list, R.layout.line, new String[]{"word","detail"}, new int[]{R.id.word,R.id.detail});
		
		//填充LISTVIEW
		
		listview.setAdapter(adapter);
		
	}

}


 

 

 

 
 
  
 
 

你可能感兴趣的:(Android)