AndroidDev:AsyncTask用于股票应用程序-未发生任何事情

AndroidDev:AsyncTask用于股票应用程序-未发生任何事情,android,android-asynctask,Android,Android Asynctask,我正在开发一个简单的应用程序来收集股票的最新信息。我正在应用AsyncTask,并构建了GUI,但在我将股票符号键入EditText后,似乎什么也没有发生。非常感谢您的帮助。我曾尝试在谷歌上搜索这些主题,并查看了文档和教程,但不确定我遗漏了什么。我学习安卓开发大约2个月了 这是我的密码: package cornez.com.stockquotes; import android.content.Context; import android.graphics.Bitmap; import a

我正在开发一个简单的应用程序来收集股票的最新信息。我正在应用AsyncTask,并构建了GUI,但在我将股票符号键入EditText后,似乎什么也没有发生。非常感谢您的帮助。我曾尝试在谷歌上搜索这些主题,并查看了文档和教程,但不确定我遗漏了什么。我学习安卓开发大约2个月了

这是我的密码:

package cornez.com.stockquotes;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    String symbol;
    TextView symbolText;

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

        TextView nameText, tradePriceText, tradeTimeText, changeText, rangeText;

        symbolText = (TextView) findViewById(R.id.symbolText);
        nameText = (TextView) findViewById(R.id.nameText);
        tradePriceText = (TextView) findViewById(R.id.tradePriceText);
        tradeTimeText = (TextView) findViewById(R.id.tradeTimeText);
        changeText= (TextView) findViewById(R.id.changeText);
        rangeText = (TextView) findViewById(R.id.rangeText);


        editText = (EditText) findViewById(R.id.editText);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_DONE)
                {
                    // actions when "Done" key is pressed

                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;

                }
                return false;
            }
        });



        editText.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.
                SOFT_INPUT_STATE_VISIBLE);

        getStockInfoTask stockTask = new getStockInfoTask();
        symbol = editText.getText().toString();
        stockTask.execute(symbol);


    }

    //AsyncTask
    private class getStockInfoTask extends AsyncTask<String, Void, Stock>
    {

        protected Stock doInBackground(String... params)
        {
            Stock stock = new Stock(symbol);
            try {
                stock.load();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return stock;
        }

        protected void onPostExecute(Stock stock)
        {
            symbolText.setText(stock.getSymbol());
        }
    }



}
package cornez.com.stockquotes;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.view.WindowManager;
导入android.view.inputmethod.EditorInfo;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.EditText;
导入android.widget.TextView;
导入java.io.IOException;
公共类MainActivity扩展了AppCompatActivity{
编辑文本编辑文本;
字符串符号;
文本视图symbolText;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text查看名称文本、交易价格文本、交易时间文本、更改文本、范围文本;
symbolText=(TextView)findViewById(R.id.symbolText);
nameText=(TextView)findViewById(R.id.nameText);
tradePriceText=(TextView)findViewById(R.id.tradePriceText);
tradeTimeText=(TextView)findViewById(R.id.tradeTimeText);
changeText=(TextView)findViewById(R.id.changeText);
rangeText=(TextView)findViewById(R.id.rangeText);
editText=(editText)findViewById(R.id.editText);
editText.setOnEditorActionListener(新的TextView.OnEditorActionListener()
{
@凌驾
公共布尔onEditorAction(TextView v,int actionId,
关键事件(事件)
{
if(actionId==EditorInfo.IME\u ACTION\u DONE)
{
//按下“完成”键时的操作
InputMethodManager imm=(InputMethodManager)v.getContext().getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
返回true;
}
返回false;
}
});
editText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams。
软输入(状态可见);
getStockInfoTask stockTask=新建getStockInfoTask();
symbol=editText.getText().toString();
stockTask.execute(符号);
}
//异步任务
私有类getStockInfoTask扩展了AsyncTask
{
受保护的库存doInBackground(字符串…参数)
{
存量=新存量(符号);
试一试{
stock.load();
}捕获(IOE异常){
e、 printStackTrace();
}
退货;
}
后期执行时受保护的无效(库存)
{
setText(stock.getSymbol());
}
}
}
  • 里面的代码

    onEditorAction
    
  • 当textView收到操作时,在新线程中运行

  • 代码

    symbol = editText.getText().toString();
    
  • 在侦听器初始化后运行同步

    因此:


    我试图为我的Stock类添加代码,以防有人也想看到,但它不允许我这样做。
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_DONE)
                {
                    // actions when "Done" key is pressed
    
                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    
                    getStockInfoTask stockTask = new getStockInfoTask();
                    symbol = editText.getText().toString();
                    stockTask.execute(symbol);
    
                    return true;
                }
                return false;
            }
        });
    
    
    
        editText.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.
                SOFT_INPUT_STATE_VISIBLE);