当前位置: 代码迷 >> Android >> 新手求解啊解决方案
  详细解决方案

新手求解啊解决方案

热度:25   发布时间:2016-05-01 21:11:03.0
新手求解啊~~
Java code
package com.android.test;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class ResultActivity extends Activity {        public void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.popup);        ListView listView = (ListView)findViewById(R.id.lshow);        Intent intent = getIntent();        //获取该intent所携带的数据        Bundle data = intent.getExtras();        //从Bundle数据包中取出数据        @SuppressWarnings("unchecked")        List<Map<String,String>> list = (List<Map<String,String>>)data.getSerializable("data");        if(list == null){            Toast.makeText(ResultActivity.this, "list为null!", 8000).show();        }        //将List封装成SimpleAdapter        SimpleAdapter adapter = new SimpleAdapter(ResultActivity.this,list,R.layout.popup,new String[]{"word","detail"},new int[]{R.id.pword,R.id.pdetail});        //填充ListView        listView.setAdapter(adapter);    }}

Java code
package com.android.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 Dict extends Activity {    MyDatabaseHelper dbHelper;    Button insert = null;    Button search = null;    public void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.mydatabase);        //创建MyDatabaseHelper对象,指定数据库版本为1,此处使用相对路径即可        //数据库文件会自动保存在程序的数据文件夹的databases目录下        dbHelper = new MyDatabaseHelper(this,"myDict.db3",1);        insert = (Button)findViewById(R.id.insert);        search = (Button)findViewById(R.id.search);        insert.setOnClickListener(new OnClickListener(){            public void onClick(View v) {                //获取用户输入                String word = ((EditText)findViewById(R.id.word)).getText().toString();                String detail = ((EditText)findViewById(R.id.detail)).getText().toString();                //插入生词记录                insertData(dbHelper.getReadableDatabase(),word,detail);                //显示提示信息                Toast.makeText(Dict.this, "添加生词成功!", 8000).show();            }        });        search.setOnClickListener(new OnClickListener(){            public void onClick(View v) {                //获取用户输入                String key = ((EditText)findViewById(R.id.key)).getText().toString();                //执行查询                String sql = "select * from dict where word like ? or detail like ?";                Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql, new String[]{"%"+key+"%","%"+key+"%"});                //创建一个Bundle对象                Bundle data = new Bundle();                data.putSerializable("data", converCursorToList(cursor));                //创建一个Intent                Intent intent = new Intent(Dict.this,ResultActivity.class);                //启动Activity                startActivity(intent);            }        });    }    protected ArrayList<Map<String,String>> converCursorToList(Cursor cursor){        ArrayList<Map<String,String>> result = new ArrayList<Map<String,String>>();        //遍历Cursor结果集        while(cursor.moveToNext()){            //将结果集中的数据存入ArrayList中            Map<String , String> map = new HashMap<String , String>();            //取出查询记录中第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();        }    }}
  相关解决方案