我有一个有两个活动的应用程序:第一个 - 列表视图,链接到html文件,第二个是webview。 例如,我在列表视图中按“第一主题”,它在webview中打开“1.html”。我想在listview中获取clicked元素的文本值,并通过此方法在第二个活动的textview中显示它:
TextView title = (TextView) findViewById(R.id.app_name);
title.setText(getString(R.string.app_name));
以下是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);
}
});
}
}
如何将listview中单击的值传递给webviewactivity?求助。
答案 0 :(得分:3)
更改行
String itemname = new Integer(position).toString();
到这个
String itemname = lv_arr[position];
答案 1 :(得分:0)
将您的itemname
声明更改为此。
String itemname = ((TextView) findViewById(R.id.app_name)).getText().toString();