Home > Mobile >  "setOnEditorListener" is never triggerd despite userinput
"setOnEditorListener" is never triggerd despite userinput

Time:02-05

I am currently programming an app in android studio and right now I am trying to get a stock Symbol from the user and print out the current price of the stock.So i made this:

package com.example.finalproject;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.loopj.android.http.*;
import androidx.appcompat.app.AppCompatActivity;
import cz.msebera.android.httpclient.Header;
import android.widget.EditText;
import android.view.inputmethod.EditorInfo;
import android.text.InputType;

public class StockPriceActivity extends AppCompatActivity {

    private EditText mStockPriceEditText;
    private TextView mStockPriceTextView;

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

        mStockPriceEditText = findViewById(R.id.stock_price_edit_text);
        mStockPriceTextView = findViewById(R.id.stock_price_text_view);

        mStockPriceEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        mStockPriceEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        mStockPriceEditText.requestFocus();




        mStockPriceEditText.setOnEditorActionListener((v, actionId, event) -> {
            Log.d("YYYYY", "!!!!!!!!");
            // Initialize the HTTP client
            AsyncHttpClient client = new AsyncHttpClient();

            // Set the API key
            String apiKey = getString(R.string.alpha_vantage_api_key);
            Log.d("YYYYY", "!!");

            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Log.d("YYYYY", "!!!");
                String stockSymbol = mStockPriceEditText.getText().toString();
                // Set the URL for the API request
                String url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="   stockSymbol   "&apikey="   apiKey;
                // Make the request
                client.get(url, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        // The request was successful.
                        String response = new String(responseBody);
                        // Parse the JSON response to get the stock price
                        String stockPrice = StockPriceParser.parseStockPriceFromJSON(response);
                        // Update the TextView with the stock price
                        mStockPriceTextView.setText(stockPrice);
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        // The request failed. Handle the error.
                        Log.d("YYYYY", "Everything went wrong when you see this!"); //TODO
                    }
                });
                return false;
            }
            return true;
        });
    }
}

My problem is that the ActionListener never gets triggerd, even when the user provides some input.This is the corresponding xml code:

<EditText
    android:id="@ id/stock_price_edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter text here"
    android:imeOptions="actionDone"
    android:inputType="textCapSentences"
    />

I tried the hole thing without userinput by directly implementing the stockSymbol in the url, for example "AAPL" and it worked fine, so no problem with that.

CodePudding user response:

In case someone is interested how I solved my problem:

package com.example.finalproject;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.loopj.android.http.*;
import androidx.appcompat.app.AppCompatActivity;
import cz.msebera.android.httpclient.Header;
import android.widget.EditText;
import android.widget.Button;

public class StockPriceActivity extends AppCompatActivity {

    private EditText mStockPriceEditText;
    private TextView mStockPriceTextView;

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

        mStockPriceEditText = findViewById(R.id.stock_price_edit_text);
        mStockPriceTextView = findViewById(R.id.stock_price_text_view);

        Button button = findViewById(R.id.button2);

        button.setOnClickListener((view) -> {
            // Initialize the HTTP client
            AsyncHttpClient client = new AsyncHttpClient();

            // Set the API key
            String apiKey = getString(R.string.alpha_vantage_api_key);

            if (view.getId() == R.id.button2) {
                String stockSymbol = mStockPriceEditText.getText().toString();
                // Set the URL for the API request
                String url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="   stockSymbol   "&apikey="   apiKey;
                // Make the request
                client.get(url, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        // The request was successful.
                        String response = new String(responseBody);
                        // Parse the JSON response to get the stock price
                        String stockPrice = StockPriceParser.parseStockPriceFromJSON(response);
                        // Update the TextView with the stock price
                        mStockPriceTextView.setText(stockPrice);
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        // The request failed. Handle the error.
                        Log.d("YYYYY", "Everything went wrong when you see this!"); //TODO
                    }
                });
            }
        });
   

 }
}

I used a button, which the user has to click to submit his input. So i could use OnClickListener instead to run the code

Button button = findViewById(R.id.button2);

        button.setOnClickListener((view) -> {
  • Related