public class MainActivity extends AppCompatActivity {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配接器物件,裝入模糊搜索的結果 SimpleCu
uj5u.com熱心網友回復:
如果可以,把代碼貼一下
public class MainActivity extends AppCompatActivity {
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;
}*/
});
}}
seach.setOnClickListener(new View.OnClickListener() {
@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);
}
報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......