无法从 button.setOnClick.getText 搜索数据库,因为字符串变为空

unable to search database from button.setOnClick .getText as String becomes null

我正在制作 android 应用程序,它在按下 button.setOnClick() 按钮时从 EditText 获取用户输入,并使用 string.getText() 搜索数据库。但是字符串变为空,我无法发送它

DbBackend.class 搜索功能所在。 我检查过,数据库在静态查询上工作正常,您可以将其设置为 = "xyz",但我无法使用用户输入来查询数据库。

这是我的代码:MainActivity.java

package com.vadhod.dictcopy;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.sql.Array;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import static android.app.PendingIntent.getActivity;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    EditText searchBar;
    Button searchButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final DbBackend dbBackend = new DbBackend(MainActivity.this);

        searchBar = (EditText) findViewById(R.id.searchBar);
        searchButton = (Button) findViewById(R.id.searchButton);
        textView = (TextView)findViewById(R.id.textView);




        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {

                    String input = searchBar.getText().toString();

                    ArrayList<String> terms = dbBackend.getList(input);

                    StringBuilder builder = new StringBuilder();
                    for (String details : terms) {
                        builder.append(details + "\n");
                    }

                    textView.setText(builder.toString());


                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        });

    }

}

DbBackend.java

package com.vadhod.dictcopy;

import ...

public class DbBackend extends DatabaseObject {

    public DbBackend(Context context){
        super(context);
    }

        public ArrayList<String> getList(String search){
        ArrayList<String> kWords = new ArrayList<String>();
        String searchQuery = "SELECT kName FROM dictionary WHERE eName LIKE '%" + search + "%'";

        try {
            Cursor kCursor = this.getDbConnection().rawQuery(searchQuery, null);
            if (kCursor != null){
                kCursor.moveToFirst();
                for (int i = 0; i < kCursor.getCount(); i++ ){
                    String sword = kCursor.getString(kCursor.getColumnIndexOrThrow("kName"));
                    kWords.add(sword);
                    kCursor.moveToNext();
                }
                kCursor.close();
                return kWords;
            }
            return kWords;
        }catch (Exception e){
            e.printStackTrace();
        }
        return kWords;
    }
}

关于当用户按下搜索按钮并通过数据库和return查询时如何使用字符串的任何帮助?

更新了移动的代码

假设在 searchButton.setOnClickListener(new View.OnClickListener() {...}); 内部搜索有效,而在外部 - 不行。问题是你的

//Option 1 not working
ArrayList<String> terms2 = dbBackend2.getList(input);
在向用户显示任何视图之前,

在 Activity 设置期间被调用。所以,input 那个时候还没有填满。您必须将此代码移动到 onclick 侦听器中,就像您在这些行上方的代码中所做的那样。

或为 input 提供一些其他来源。

更新: 对于 NullPointer:在您的布局文件 activity_main 中,您需要有一个 ID 为 textView 的 TextView。因为您执行了以下操作:

    // find the textview in the layout
            textView = (TextView)findViewById(R.id.textView);

    // do the search, get results
...
    // set textview's text, at this moment textView must not be null
            textView.setText(kbuilder.toString());