Android studio Android Studio数据库查询中未显示文本
我正在尝试制作一个应用程序,它将根据配料搜索数据库,原始查询将显示在配方名称的列表视图中。然后,用户可以选择他们想要查看的配方。这将使他们进入一个不同的活动页面,该页面将显示配方名称、成分和说明。因此,在我的活动中,它会显示配方名称,但查询中不会显示成分和说明。我通过硬编码字符串来检查配方名称,结果显示一切正常。因此,我不确定当字符串没有硬编码时,为什么它不能工作。下面是我的代码活动和数据库类Android studio Android Studio数据库查询中未显示文本,android-studio,android-sqlite,Android Studio,Android Sqlite,我正在尝试制作一个应用程序,它将根据配料搜索数据库,原始查询将显示在配方名称的列表视图中。然后,用户可以选择他们想要查看的配方。这将使他们进入一个不同的活动页面,该页面将显示配方名称、成分和说明。因此,在我的活动中,它会显示配方名称,但查询中不会显示成分和说明。我通过硬编码字符串来检查配方名称,结果显示一切正常。因此,我不确定当字符串没有硬编码时,为什么它不能工作。下面是我的代码活动和数据库类 EditText search; ListView list; Button
EditText search;
ListView list;
Button search_btn;
String [] rec;
ArrayList<String> arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingredientlist_activity);
search = (EditText) findViewById(R.id.search);
list = (ListView) findViewById(R.id.listRecipe);
search_btn = (Button) findViewById(R.id.button3);
search_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
String n = search.getText().toString();
String recipe = databaseAccess.getName(n);
rec = recipe.split("\\n");
arr = new ArrayList<String>(Arrays.asList(rec));
list.setAdapter(new ArrayAdapter<String>(SearchActivity.this,
android.R.layout.simple_list_item_1, arr));
databaseAccess.close();
closeKeyboard();
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String recipe = arr.get(position);
Intent intent = new Intent(getApplicationContext(), RecipeViewer.class);
intent.putExtra("name",recipe);
startActivity(intent);
}
});
}
private TextWatcher inputTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int stat, int before, int count) {
try{
String n = search.getText().toString();
search_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
String recipe = databaseAccess.getName(n);
rec = recipe.split("\\n");
ArrayList<String> arr = new ArrayList<String>(Arrays.asList(rec));
list.setAdapter(new ArrayAdapter<String>(SearchActivity.this,
android.R.layout.simple_list_item_1, arr));
databaseAccess.close();
}
});
}catch (Exception e)
{
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
public void closeKeyboard(){
View view = this.getCurrentFocus();
if(view!=null){
InputMethodManager imm = (InputMethodManager)getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
}
public class RecipeViewer extends Activity {
String holder;
TextView name;
TextView ingredients;
TextView directions;
String ing, dir;
Button viewIng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choice_activity);
holder = getIntent().getStringExtra("name");
viewIng = (Button) findViewById(R.id.button6);
name = findViewById(R.id.textView12);
ingredients = findViewById(R.id.textView14);
directions = findViewById(R.id.textView16);
viewIng = findViewById(R.id.button6);
name.setText(holder);
}
public void displayRecipe(View view){
DatabaseAccess db = DatabaseAccess.getInstance(getApplicationContext());
db.open();
ing = db.getIngredients(holder);
dir = db.getDirections(holder);
ingredients.setText(ing);
directions.setText(dir);
db.close();
}
}
public class DatabaseAccess {
private String tableName = "Recipes";
private String col_Name = "Name";
private String col_Ing = "Ingredients";
private String col_Dir = "Directions";
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DatabaseAccess instance;
Cursor c = null;
public DatabaseAccess(Context context){
this.openHelper = new DatabaseOpenHelper(context);
}
public static DatabaseAccess getInstance(Context context){
if(instance == null){
instance = new DatabaseAccess(context);
}
return instance;
}
public void open(){
this.db = openHelper.getWritableDatabase();
}
public void close(){
if(db!= null){
this.db.close();
}
}
public String getName(String listofIngredients){
String ingredients = listofIngredients.replace(" ",",");
String [] input = ingredients.split(",");
c = db.rawQuery("select * from Recipes ",new String [] {});
StringBuffer buffer = new StringBuffer();
for(String a : input){
while(c.moveToNext()){
if(c.getString(1).contains(a)){
String recipename = c.getString(0);
buffer.append(" " +recipename +"\n");
}
}
}
return buffer.toString();
}
public String getIngredients(String name){
String recipeIngredients= "";
c = db.rawQuery("select * from " + tableName + " where " +col_Name + " = ?; ", new String [] {name});
while (c.moveToNext()) {
recipeIngredients= c.getString(c.getColumnIndexOrThrow(col_Ing));
}
return recipeIngredients;
}
public String getDirections(String name){
String directions = " ";
c = db.rawQuery("select " + col_Dir + " from " + tableName + " where " +col_Name + " = ?; ", new String [] {name});
while (c.moveToNext()) {
directions = c.getString(0);
}
return directions;
}
public String randomRecipe(){
String recipename= "";
String ing = "";
String dir ="";
c = db.rawQuery("select * from Recipes order by random() limit 1 ",new String [] {});
StringBuffer buffer = new StringBuffer();
while(c.moveToNext()){
recipename = c.getString(0);
ing = c.getString(c.getColumnIndexOrThrow(col_Ing));
dir = c.getString(c.getColumnIndexOrThrow(col_Dir));
}
String random = recipename +":" + ing +":" + dir;
return random;
}
}`
编辑文本搜索;
列表视图列表;
按钮搜索;
字符串[]rec;
ArrayList-arr;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.IngreditList_活动);
search=(EditText)findViewById(R.id.search);
列表=(ListView)findViewById(R.id.listRecipe);
search_btn=(按钮)findviewbyd(R.id.button3);
search_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
DatabaseAccess DatabaseAccess=DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
字符串n=search.getText().toString();
String recipe=databaseAccess.getName(n);
rec=配方分割(\\n”);
arr=newarraylist(Arrays.asList(rec));
list.setAdapter(新的ArrayAdapter)(SearchActivity.this、,
android.R.layout.simple_list_item_1,arr));
databaseAccess.close();
关闭键盘();
}
});
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串配方=arr.get(位置);
Intent Intent=新的Intent(getApplicationContext(),RecipeViewer.class);
意图。putExtra(“名称”,配方);
星触觉(意向);
}
});
}
私有TextWatcher InputExtWatcher=新TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(字符序列、int stat、int before、int count){
试一试{
字符串n=search.getText().toString();
search_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
DatabaseAccess DatabaseAccess=DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
String recipe=databaseAccess.getName(n);
rec=配方分割(\\n”);
ArrayList arr=新的ArrayList(Arrays.asList(rec));
list.setAdapter(新的ArrayAdapter)(SearchActivity.this、,
android.R.layout.simple_list_item_1,arr));
databaseAccess.close();
}
});
}捕获(例外e)
{
}
}
@凌驾
public void PostTextChanged(可编辑){
}
};
公用键盘(){
视图=this.getCurrentFocus();
如果(视图!=null){
InputMethodManager imm=(InputMethodManager)getSystemService(getApplicationContext().INPUT\方法\服务);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
}
公共类RecipeViewer扩展活动{
绳夹;
文本视图名称;
文本视图成分;
文本视图方向;
字符串ing,dir;
按钮查看;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.choice_活动);
holder=getIntent().getStringExtra(“名称”);
查看=(按钮)findViewById(R.id.button6);
name=findViewById(R.id.textView12);
成分=findViewById(R.id.textView14);
方向=findViewById(R.id.textView16);
查看=findViewById(R.id.按钮6);
名称.setText(持有人);
}
公共无效显示配方(视图){
DatabaseAccess db=DatabaseAccess.getInstance(getApplicationContext());
db.open();
ing=db.GetComponents(支架);
dir=db.getDirections(持有者);
成分:塞特(塞特);
方向.setText(dir);
db.close();
}
}
公共类数据库访问{
私有字符串tableName=“Recipes”;
私有字符串col_Name=“Name”;
私有字符串colu_Ing=“配料”;
专用字符串col_Dir=“方向”;
私有SQLiteOpenHelper;
专用数据库数据库;
私有静态数据库访问实例;
光标c=null;
公共数据库访问(上下文){
this.openHelper=新数据库openHelper(上下文);
}
公共静态数据库访问getInstance(上下文){
if(实例==null){
实例=新数据库访问(上下文);
}
返回实例;
}
公开作废{
this.db=openHelper.getWritableDatabase();
}
公众假期结束(){
如果(db!=null){
this.db.close();
}
}
公共字符串getName(字符串列表){
字符串成分=成分列表。替换(“,”,”);
字符串[]输入=成分。拆分(“,”);
c=db.rawQuery(“从配方中选择*”,新字符串[]{});
StringBuffer=新的StringBuffer();
for(字符串a:inp)