1

I am making android app that takes user input from EditText on press of button from button.setOnClick() and use the string.getText() to search the database. But the string becomes null and no way i can send it

DbBackend.class where the search function is. I checked and database works fine on static query which you can set to = "xyz" but no way i am able to use the user input to query the database.

Here is my code: 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;
    }
}

Any help please on how to use the string when user press the search button and search that through the database and return the query?

Updated the moved code

Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
MMG
  • 390
  • 1
  • 6
  • 20
  • 1
    what is DatabaseObject? – khusrav May 24 '17 at 16:54
  • Those are two supporting java classes DatabaseObject.java and DictionaryDatabase.java. I am using com.readystatesoftware.sqliteasset:sqliteassethelper:+ from github – MMG May 24 '17 at 16:59
  • Everything else is running fine. All the object can be retrieved and displayed in listview if i want. but i dont need that so removed that code – MMG May 24 '17 at 17:00
  • 1
    Wrote the answer not sure if it's clear enough. If no, can try to elaborate. – khusrav May 24 '17 at 17:02
  • @khusrav did as you said. moved the stringbuilder code inside onClick but got the null error. is there any other way to extract data from editText on button click – MMG May 24 '17 at 17:22

1 Answers1

2

Assuming that inside the searchButton.setOnClickListener(new View.OnClickListener() {...}); the search is working, and outside - not. The problem is your

//Option 1 not working
ArrayList<String> terms2 = dbBackend2.getList(input);

is called during the Activity setup before any view is shown to the user. So, the input is not filled at that time. You have to move this code into an onclick listener, like you did in the code above these lines.

Or provide some other source for the input.

Upd: For the NullPointer: in your layout file activity_main you need to have a TextView with id textView. Because you do the following:

    // 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());
khusrav
  • 5,267
  • 5
  • 27
  • 38
  • W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference – MMG May 24 '17 at 17:20
  • 1
    @Sykox which TextView are you using there? Have you found it using `findViewById`? – khusrav May 24 '17 at 17:23
  • yes textView is decalred globally and have found it using findviewbyid – MMG May 24 '17 at 17:39
  • 1
    Can you update the code with the moved part? It's obvious that some textview is null at the moment of the text being set, we just need to find out which one and when. – khusrav May 24 '17 at 17:41
  • 1
    It worked... Thanks a million buddy! now i just want to create an intent and move the textView to new activity. – MMG May 24 '17 at 17:50