在函数之间传递变量(Java Android Studio)

问题描述 投票:0回答:1

我是android studio和Java的新手。我的应用程序使用jsoup将网站的内容传递到一个数组中,其中每个元素都显示在可滑动抽认卡上(例如tinder)]

[我遇到了一个问题,当我尝试将变量'words'的结果从onPostExecute()(第123行)传递给第49行的String num时,我的应用程序崩溃了。我想在onPostExcecute并将其设置为String num,但我不确定该怎么做。

public class AppHome extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
    TextView texx;
    private ArrayList<String> al;
    private ArrayAdapter<String> arrayAdapter;
    private int i;
    String words;


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

        texx= findViewById(R.id.text1);
        new doit().execute();

        String num = words;
        String str[] = num.split(",");
        final ArrayList al = new ArrayList<String>(Arrays.asList(str));

        arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );

        SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);


        registerForContextMenu(flingContainer);


        flingContainer.setAdapter(arrayAdapter);

        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                al.remove(0);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                Toast.makeText(AppHome.this, "left", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                Toast.makeText(AppHome.this, "right", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                al.add("XML ".concat(String.valueOf(i)));
                arrayAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
            }
        });

    }
    public class doit extends AsyncTask<Void,Void,Void> {
        //String words;
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").get();
                words=doc.text();
            }catch(Exception e){e.printStackTrace();}
            return null;
        }

        @Override
        public void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            texx.setText(words);

            //String str = (words);
            //List<String> elephantList = Arrays.asList(str.split(","));
            //texx.setText(elephantList.toString());
            // texx.setText(elephantList);
        }
    }

 }




java android android-studio parameter-passing
1个回答
0
投票
public class doit extends AsyncTask<Void, Void, String> {
    @Override
    protected Void doInBackground(Void... voids) {
        String words = "";
        try {
            Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").get();
            words = doc.text();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public void onPostExecute(String words) {
        super.onPostExecute(aVoid);
        texx.setText(words);

        //String str = (words);
        //List<String> elephantList = Arrays.asList(str.split(","));
        //texx.setText(elephantList.toString());
        // texx.setText(elephantList);
    }
}

现在应该可以了。

问题是,您没有从doInBackground方法返回任何内容,因此在onPostExecute函数中没有任何内容。

您可能考虑查看AsyncTask here的文档。

© www.soinside.com 2019 - 2024. All rights reserved.