简体   繁体   English

从AsyncTask访问TextView(在一个片段内)

[英]Accessing a TextView from AsyncTask (within a fragment)

Firstly, i'm new to android programming, so sorry if this seems like a silly question, i'm still trying ot get my head around it all! 首先,我是android编程的新手,所以很抱歉,如果这看起来像是一个愚蠢的问题,我仍在努力使自己全神贯注! I am trying to update a TextView in a fragment from within my onPostExecute() AsyncTask. 我正在尝试从onPostExecute()AsyncTask内部的片段中更新TextView。 But i'm struggling on how to get a TextView from the layout and update it. 但是我正在努力如何从布局中获取一个TextView并更新它。 I seem to be able to create the TextView on the fly and update it, but I wanted to a textview that i've created in my xml layout. 我似乎能够即时创建TextView并对其进行更新,但是我想使用在xml布局中创建的Textview。 I have seen loads of similar questions here, but none are using fragments (its the fragment bit i'm trying to learn). 我在这里看到了很多类似的问题,但是没有一个人使用片段(我尝试学习的片段片段)。

You can see from the code below, that I tried at first to create the TextView (tv) in the code (commented out) and that worked fine. 您可以从下面的代码中看到,我首先尝试在代码中创建TextView(tv)(注释掉),并且效果很好。 Then I tried to get the TextView from the layout ((TextView)rootView.findViewById(R.id.tv2)), but because I cannot declare it as a public string, I now don't have access to it in the onPostExecute(). 然后我尝试从布局((TextView)rootView.findViewById(R.id.tv2))中获取TextView,但是由于无法将其声明为公共字符串,因此现在无法在onPostExecute( )。

Am i doing something really silly here?? 我在这里真的很傻吗?

public static class PlaceholderFragment extends Fragment {
    String str;
    //public TextView tv;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tug_boat_main,
                container, false);

        tv = (TextView)rootView.findViewById(R.id.tv2);
        //tv = new TextView(container.getContext());
        tv.setText("hi there");
        ReadExtPortfolio rext = new ReadExtPortfolio();
        rext.execute();
        return tv;
        //return rootView;

    }

    class ReadExtPortfolio extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute() {
            tv.setText("Fetching data, please wait: ");
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            try {

                 URL url = new URL("http://mywebsite.com/andy.txt");
                 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 str = in.readLine();
                 in.close();
               } catch (IOException e) {
                  str = "error";
                }
            return null;
        }

        // Executed on the UI thread after the
        // time taking process is completed
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tv.setText("Completed the task, and the result is : " + str);
        }
    }   
}

XML file (fragment_tug_boat_main) contains... XML档案(fragment_tug_boat_main)包含...

 <TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world2" />

Also, I've used stackoverflow for years and the people who answer have saved me hours and hours of work over those years. 另外,我已经使用stackoverflow多年了,那些年来回答问题的人为我节省了很多时间。 So thanks for reading this and thanks to everyone who answers. 因此,感谢您阅读本文并感谢所有回答。

EDIT: sorry, i should have said. 编辑:对不起,我应该说。 If i uncomment "public TextView tv;" 如果我取消注释“ public TextView tv”; then I get a runtime error: The specified child already has a parent. 然后我得到一个运行时错误:指定的子代已经有一个父代。 You must call removeView() on the childs parent first. 您必须先在子级父级上调用removeView()。 I didn't think that calling removeView() sounded right as removeView does not exist on my TextView 我不认为调用removeView()听起来很正确,因为TextView不存在removeView

All you have to do is to uncomment //public TextView tv; 您要做的就是取消注释// public TextView tv; and then you will be able to use it in the onPostExecute method. 然后您将可以在onPostExecute方法中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM