如何将Jsoup提取的字符串发送到Android Studio中的另一个活动?

时间:2022-10-31 09:19:45

I have this problem. In my Activity (TripActivity.java) I have extracted a string from a URL. Then, when I press a button in my activity I want that the string extracted should be inserted in a textview in the second activity (EventActivity.java). The string that I want to transmit is linkText. I tried with putExtra but it doesn't work.

我有这个问题。在我的Activity(TripActivity.java)中,我从URL中提取了一个字符串。然后,当我在我的活动中按下一个按钮时,我希望提取的字符串应插入第二个活动(EventActivity.java)的textview中。我想传输的字符串是linkText。我尝试使用putExtra但它不起作用。

My first Activity is:

我的第一个活动是:

public class TripActivity extends Activity implements View.OnClickListener {

private ImageButton btColosseo;
private ImageButton btArcoCostantino;
private ImageButton btDomus;
private ImageButton btForo;
private ImageButton btConfermaViaggio;
private String monumento = "null";

private String Visita1 = "Colosseo";
private String Visita2 = "Domus Aurea";
private String Visita3 = "Arco di Costantino";
private String Visita4 = "Foro Romano e Palatino";

public static final String BASE_URL =    "http://archeoroma.beniculturali.it/siti-archeologici/";


private static final String LOG_TAG = "TripActivity";
private String NomeMonumento;
public String linkText= "";


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

    Bundle datipassati = getIntent().getExtras();
    String Viaggio = datipassati.getString("Viaggio");
    String Citta = datipassati.getString("Citta");
    int Budget = getIntent().getIntExtra("Budget", -1);
    String Percorso = datipassati.getString("Percorso");
    Boolean Shopping = datipassati.getBoolean("Shopping");
    Boolean Cultura = datipassati.getBoolean("Cultura");
    Boolean Cucina = datipassati.getBoolean("Cucina");
    Boolean Eventi = datipassati.getBoolean("Eventi");


    Log.d(LOG_TAG, Viaggio);
    Log.d(LOG_TAG, Citta);
    Log.d(LOG_TAG, "Il budget e':" + Budget);
    Log.d(LOG_TAG, Percorso);
    Log.d(LOG_TAG, "L'opzione shopping e' settata su: " + Shopping);
    Log.d(LOG_TAG, "L'opzione cucina e' settata su: " + Cucina);
    Log.d(LOG_TAG, "L'opzione cultura e' settata su: " + Cultura);
    Log.d(LOG_TAG, "L'opzione eventi e' settata su: " + Eventi);

    btColosseo = (ImageButton) findViewById(R.id.btcolosseo);
    btColosseo.setOnClickListener(this);

    btArcoCostantino = (ImageButton) findViewById(R.id.btarcocostantino);
    btArcoCostantino.setOnClickListener(this);

    btDomus = (ImageButton) findViewById(R.id.btdomus);
    btDomus.setOnClickListener(this);

    btForo = (ImageButton) findViewById(R.id.btforopalatino);
    btForo.setOnClickListener(this);

    btConfermaViaggio = (ImageButton) findViewById(R.id.btconfermaviaggio);
    btConfermaViaggio.setOnClickListener(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case (Const.IntentRequest.SET_OPTIONS): {
            if (resultCode == Activity.RESULT_OK) {
                Log.d(LOG_TAG, "Risultato " + getIntent().getExtras().getBoolean("ButtonVisible"));
                if (getIntent().getExtras().getBoolean("ButtonVisible") == false && monumento == "Colosseo") {
                    btColosseo.setVisibility(View.GONE);
                    Visita1 = "null";
                }
                if (getIntent().getExtras().getBoolean("ButtonVisible") == false && monumento == "Domus Aurea") {
                    btDomus.setVisibility(View.GONE);
                    Visita2 = "null";
                }
                if (getIntent().getExtras().getBoolean("ButtonVisible") == false && monumento == "Arco di Costantino") {
                    btArcoCostantino.setVisibility(View.GONE);
                    Visita3 = "null";
                }
                if (getIntent().getExtras().getBoolean("ButtonVisible") == false && monumento == "Foro Romano e Palatino") {
                    btForo.setVisibility(View.GONE);
                    Visita4 = "null";
                }
            }
            break;
        }
    }
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btcolosseo: {
            monumento = "Colosseo";
            Log.d(LOG_TAG, "Hai premuto Colosseo");
            NomeMonumento = monumento.trim().toLowerCase().replace(" ","-");
            new Description().execute();
            Intent startEventActivity = new Intent(TripActivity.this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivityForResult(startEventActivity, Const.IntentRequest.SET_OPTIONS);
            break;
        }
        case R.id.btdomus: {
            monumento = "Domus Aurea";
            Log.d(LOG_TAG, "Hai premuto Domus Aurea");
            NomeMonumento = monumento.trim().toLowerCase().replace(" ","-");
            new Description().execute();
            Intent startEventActivity = new Intent(this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivityForResult(startEventActivity, Const.IntentRequest.SET_OPTIONS);
            break;
        }
        case R.id.btarcocostantino: {
            monumento = "Arco di Costantino";
            Log.d(LOG_TAG, "Hai premuto Arco di Costantino");
            NomeMonumento = monumento.trim().toLowerCase().replace(" di ", "-");
            new Description().execute();
            Intent startEventActivity = new Intent(this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivityForResult(startEventActivity, Const.IntentRequest.SET_OPTIONS);
            break;
        }
        case R.id.btforopalatino: {
            monumento = "Foro Romano e Palatino";
            Log.d(LOG_TAG, "Hai premuto Foro Romano");
            NomeMonumento = monumento.trim().toLowerCase().replace(" e ", "-");
            NomeMonumento = NomeMonumento.trim().toLowerCase().replace(" ", "-");
            new Description().execute();
            Intent startEventActivity = new Intent(this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivityForResult(startEventActivity, Const.IntentRequest.SET_OPTIONS);
            break;
        }
        case R.id.btconfermaviaggio: {
            Intent startFinalActivity = new Intent(this, FinalActivity.class);
            startFinalActivity.putExtra("Visita1", Visita1);
            startFinalActivity.putExtra("Visita2", Visita2);
            startFinalActivity.putExtra("Visita3", Visita3);
            startFinalActivity.putExtra("Visita4", Visita4);
            this.startActivityForResult(startFinalActivity, Const.IntentRequest.SET_OPTIONS);
        }
        }
    }

private class Description extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(BASE_URL + NomeMonumento).get();
            Element content = document.getElementById("content-area");
            Elements desc = content.getElementsByTag("p");
            linkText=" ";
            String keyword = "prenotazione";
            String keyword2 = "weekend";
            String keyword3 = "3000";
            String keyword4 = "2015";

            for (Element link: desc) {
                if((link.text().length() >= 150) && (link.text().toLowerCase().indexOf(keyword.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword2.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword3.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword4.toLowerCase()) == -1)){
                    linkText = linkText + " " + link.text();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}}

The second activity is:

第二项活动是:

public class EventActivity extends Activity implements View.OnClickListener{

private ViewFlipper viewFlipper;
private GestureDetector mGestureDetector;

private ImageButton btConferma2;
private ImageButton btElimina;
private TextView tvMonumento;
private WebView wvMap;
private TextView tvDescrizione;
//private String NomeMonumento;

public static final String MAP = "https://www.google.it/maps/";

int[] colosseo = {R.drawable.colosseo1, R.drawable.colosseo2};
int[] arco = {R.drawable.arcocostantino1, R.drawable.arcocostantino2};
int [] domus = {R.drawable.domusaurea1, R.drawable.domusaurea2};
int [] foroepalatino = {R.drawable.fororomanoepalatino1, R.drawable.fororomanoepalatino2};


private static final String LOG_TAG = "EventActivity";

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

    btConferma2 = (ImageButton) findViewById(R.id.btconferma2);
    btElimina = (ImageButton) findViewById(R.id.btelimina);
    viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
    tvMonumento =(TextView) findViewById(R.id.tvmonumento);
    tvDescrizione = (TextView) findViewById(R.id.tvtesto);
    wvMap = (WebView) findViewById(R.id.wvmap);

    wvMap.setWebViewClient(new WebViewClient());
    wvMap.getSettings().setJavaScriptEnabled(true);

    btConferma2.setOnClickListener(this);
    btElimina.setOnClickListener(this);

    String monumento = getIntent().getExtras().getString("Monumento");

    Log.d(LOG_TAG, monumento);

    tvMonumento.setText(monumento);

    switch (monumento) {
        case ("Colosseo"): {
            for (int i = 0; i < colosseo.length; i++) {
                wvMap.loadUrl(MAP+"@41.8902102,12.4922309,17z");
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(colosseo[i]);
                viewFlipper.addView(imageView);
            }
            break;
        }
        case ("Arco di Costantino"):{

            for (int i = 0; i < arco.length; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(arco[i]);
                viewFlipper.addView(imageView);
                //wvMap.loadUrl(MAP+);
            }
        }
        case ("Domus Aurea"): {
            for (int i = 0; i < domus.length; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(domus[i]);
                viewFlipper.addView(imageView);
                //wvMap.loadUrl(MAP+);
            }
            break;
        }
        case ("Foro Romano e Palatino"): {
            for (int i = 0; i < foroepalatino.length; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(foroepalatino[i]);
                viewFlipper.addView(imageView);
                //wvMap.loadUrl(MAP+);
            }
            break;
        }
    }


    CustomGestureDetector customGestureDetector = new CustomGestureDetector();
    mGestureDetector = new GestureDetector(this, customGestureDetector);
}

// classe per controllare lo swipe prodotto dalle dita
class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        // Swipe left (next)
        if (e1.getX() > e2.getX() && (e1.getY()>0 && e2.getY()< 900)) {
            viewFlipper.setInAnimation(EventActivity.this, R.anim.left_in);
            viewFlipper.setOutAnimation(EventActivity.this, R.anim.left_out);
            viewFlipper.showNext();
        }
        // Swipe right (previous)
        if (e1.getX() < e2.getX()&& (e1.getY()>0 && e2.getY()< 900)) {
            viewFlipper.setInAnimation(EventActivity.this, R.anim.right_in);
            viewFlipper.setOutAnimation(EventActivity.this, R.anim.right_out);
            viewFlipper.showPrevious();
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btconferma2: {
            finish();
            break;
        }
        case R.id.btelimina: {
            Intent startTripActivity = new Intent(this, TripActivity.class);
            startTripActivity.putExtra("ButtonVisible", false);
            setResult(Activity.RESULT_OK,startTripActivity);
            finish();
            break;
        }
    }
}}

Please help me

请帮我

1 个解决方案

#1


0  

I simplify the code and I add a comment to make easier the problem resolution

我简化了代码并添加了注释,以便更轻松地解决问题

public class TripActivity extends Activity implements View.OnClickListener {

private ImageButton btColosseo;
private String monumento = "null";

private String Visita1 = "Colosseo";

public static final String BASE_URL = "http://archeoroma.beniculturali.it/siti-archeologici/";


private static final String LOG_TAG = "TripActivity";
private String NomeMonumento;
public String linkText= "";


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

    btColosseo = (ImageButton) findViewById(R.id.btcolosseo);
    btColosseo.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btcolosseo: {
            monumento = "Colosseo";
            Log.d(LOG_TAG, "Hai premuto Colosseo");
            NomeMonumento = monumento.trim().toLowerCase().replace(" ","-");
            new Description().execute();
            Intent startEventActivity = new Intent(TripActivity.this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivity(startEventActivity);
            break;
        }

    }}

private class Description extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(BASE_URL + NomeMonumento).get();
            Element content = document.getElementById("content-area");
            Elements desc = content.getElementsByTag("p");
            linkText=" ";
            String keyword = "prenotazione";
            String keyword2 = "weekend";
            String keyword3 = "3000";
            String keyword4 = "2015";

            for (Element link: desc) {
                if((link.text().length() >= 150) && (link.text().toLowerCase().indexOf(keyword.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword2.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword3.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword4.toLowerCase()) == -1)){
                    linkText = linkText + " " + link.text();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } // Here I want to send the string linkText to another activity and set a textView with the text extracted from the URL
}}

#1


0  

I simplify the code and I add a comment to make easier the problem resolution

我简化了代码并添加了注释,以便更轻松地解决问题

public class TripActivity extends Activity implements View.OnClickListener {

private ImageButton btColosseo;
private String monumento = "null";

private String Visita1 = "Colosseo";

public static final String BASE_URL = "http://archeoroma.beniculturali.it/siti-archeologici/";


private static final String LOG_TAG = "TripActivity";
private String NomeMonumento;
public String linkText= "";


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

    btColosseo = (ImageButton) findViewById(R.id.btcolosseo);
    btColosseo.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btcolosseo: {
            monumento = "Colosseo";
            Log.d(LOG_TAG, "Hai premuto Colosseo");
            NomeMonumento = monumento.trim().toLowerCase().replace(" ","-");
            new Description().execute();
            Intent startEventActivity = new Intent(TripActivity.this, EventActivity.class);
            startEventActivity.putExtra("Monumento", monumento);
            this.startActivity(startEventActivity);
            break;
        }

    }}

private class Description extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(BASE_URL + NomeMonumento).get();
            Element content = document.getElementById("content-area");
            Elements desc = content.getElementsByTag("p");
            linkText=" ";
            String keyword = "prenotazione";
            String keyword2 = "weekend";
            String keyword3 = "3000";
            String keyword4 = "2015";

            for (Element link: desc) {
                if((link.text().length() >= 150) && (link.text().toLowerCase().indexOf(keyword.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword2.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword3.toLowerCase()) == -1)
                        && (link.text().toLowerCase().indexOf(keyword4.toLowerCase()) == -1)){
                    linkText = linkText + " " + link.text();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } // Here I want to send the string linkText to another activity and set a textView with the text extracted from the URL
}}