[cp]从数据库user表中搜索,显示到listview中,点击列表项可以有列表框弹出来,有三个按钮,分别是添加,查看他的信息和查看我的好友,点击添加好友后toast提示已经添加,查看我的好友可以将数据库myfriend(我的好友)表的内容显示到listview中,要求是这样的,用sqlite,
然后应该是,一个表两个类吗,还有一个主类,就是一共是5个类吗?如果可以的话,能给我看看实现代码吗?
如果有差不多源码的就更好啦
然后应该是,一个表两个类吗,还有一个主类,就是一共是5个类吗?如果可以的话,能给我看看实现代码吗?
如果有差不多源码的就更好啦
解决方案 »
- 为ExpandableListView的childView添加header
- andorid 源码 下载遇到的问题
- 有两按钮共用一张背景图,修改其中一个半透明会相互影响
- 北京有什么比较好的android公司
- webview页面显示不全也滑动不了(求助啊!)
- 请教如何判断物理键盘是否存在?
- 菜鸟请教。。android图片显示的问题!!!!!!!!!!
- 关于android中textview如果有中文字符,排版参差不齐的情况
- com.android.camera.action.CROP 图片裁剪的问题
- Android开发工具Eclipse的暗转问题
- 各位大佬,请问 安卓7.0还能够实现短信拦截吗?
- java.security.KeyStoreException, java.security.UnrecoverableKeyException
但是代码调试没成功
但是代码调试没成功
哦哦,那没什么了,主要就是sqlite的增删改查,你找个博客看一下怎么用就可以了啊
但是代码调试没成功
哦哦,那没什么了,主要就是sqlite的增删改查,你找个博客看一下怎么用就可以了啊
不知道是不是查询语句有问题,
,怎么获取到关键字呢?放在listview里需要用到arraylist吗?
EditText input;Button send;ListView show;
SQLiteDatabase db;
PopupMenu popop=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/user.db3", null); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取组件 input = findViewById(R.id.input);
show = findViewById(R.id.show);
send = findViewById(R.id.button);
//搜索按钮被点击从user表中搜索合适的数据 send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
private void queryRecords(String tempName) {
//模糊搜索
Cursor c = db.rawQuery(
"select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null);
// 创建adapter适配器对象,装入模糊搜索的结果 SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,R.layout.activity_main, c, new String[]{"name"},
new int[]{android.R.id.text1}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
show.setAdapter(adapter); adapter.notifyDataSetChanged();
}
/*
//搜索好友
String titile = input.getText().toString();
Cursor c = db.rawQuery("select * from user where name like ?" , new String[]{"&" + titile + "&", "&" + titile + "&"});
Bundle data = new Bundle();
public Bundle getData() {
data.putSerializable("data", converCursorToList(c)); return data;
}
//显示到列表框
protected ArrayList<Map<String, String>> converCursorToList(Cursor c) {
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
while
(c.moveToNext()) {
Map<String, String> map = new HashMap<>(); map.put("name", c.getString(1)); result.add(map);
List<Map<String, String>> list = (List<Map<String, String>>)
data.getSerializable("data"); SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.activity_main, c, new String[]{"id", "name"}, new int[]{R.id.show}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
show.setAdapter(adapter);
}
return result;
}*/
});
}}
2.你的模糊查询语句好像不太对吧。。我一般都是使用的GreenDao,所以我也不太会写语句,你最好去查一下。
3.遍历Cursor,将结果封装成一个对象,创建一个List集合保存这些对象即可。
@Override
public void onClick(View v) {
String title = input.getText().toString();
Cursor cursor = dbhelp.getReadableDatabase().rawQuery("select * from user where name like ? or phone like ?"
+ new String[]{"%" + title + "%","%"+title+"%"},null); //创建一个List集合
result = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<>();
map.put("name", cursor.getString(1));
map.put("phone", cursor.getString(2));
result.add(map);
cursor.moveToNext(); //创建一个SimpleAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_item, cursor, new String[]{"name", "phone"}
, new int[]{R.id.show});
//填充listview
show.setAdapter(adapter);
}});
是查询语句有问题吗? public List<Person> query(String content){
List<Person> personList = new ArrayList<Person>(); SQLiteDatabase db = personDBOpenHelper.getReadableDatabase();
Cursor c = db.query("person",
new String[]{"id","username","password"},
"username LIKE ? or password LIKE ? ",
new String[]{"%" + content + "%" ,"%" + content + "%" },null,null,null); while(c.moveToNext()){
int id = c.getInt(c.getColumnIndex("id"));
String username = c.getString(c.getColumnIndex("username"));
String password = c.getString(c.getColumnIndex("password"));
Person person = new Person(id,username,password);
personList.add(person);
} c.close();
db.close(); return personList;
}
可以直接在原先的代码上改吗?username、password、id 都是表的字段名(姓名、密码、序号)。我建的表是这几个字段,你根据你建的表来改下这几个就行了。你那个不是phone吗,就直接改成phone就行了。。这个代码我试过了,没问题的。