2

I have an app with two activities: 1st - listview with links to html files, second is a webview. For example I press "First Topic" in the listview, it opens "1.html" in the webview. I want to get text value of clicked element in the listview and show it in textview of second activity by this method:

TextView title = (TextView) findViewById(R.id.app_name);
title.setText(getString(R.string.app_name));

Here is a code of ListViewActivity:

public class ListViewActivity extends Activity implements OnClickListener {
    private ListView lv1;
    private String lv_arr[] = { "First Topic", "Second Topic" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list);


        lv1 = (ListView) findViewById(R.id.listView);
        lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
                lv_arr));
        lv1.setTextFilterEnabled(true);
        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                String itemname = new Integer(position).toString();
                Intent intent = new Intent();
                intent.setClass(ListViewActivity.this, WebViewActivity.class);

                Bundle b = new Bundle();
                b.putString("defStrID", itemname);
                intent.putExtras(b);

                startActivity(intent);
            }
        });
    }
}

How can I pass the value which was clicked in listview to webviewactivity? Help, please.

2 Answers 2

3

change the line

String itemname = new Integer(position).toString();

to this

String itemname = lv_arr[position];
Sign up to request clarification or add additional context in comments.

6 Comments

But how can I put it in the next activity in TextView title = (TextView) findViewById(R.id.app_name); title.setText(what should I write here?);
@Sabre: aren't you able to put it into intent extra? if you can,then you should be able to get it in next activity.
Bundle b = new Bundle(); title.setText(b.getString("defStrID").toString);
I can't change this line String itemname = new Integer(position).toString(); because it passes position of file to open it. I think I should do something like this in onItemClick String pressed = lv_arr[position]; Bundle c = new Bundle(); b.putString("pagename", pressed); intent.putExtras(c);
in your next activity, get it like this title.setText(getIntent().getExtras().getString("defStrID"));
|
0

Change your itemname declaration to this.

String itemname = ((TextView) findViewById(R.id.app_name)).getText().toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.