xiaoyan_12 2013-05-27 09:20 采纳率: 25%
浏览 4653

如何从ListView中的string ArrayAdapter中获取文本值?

在程序中有两个 activity,第一个,列表视图的链接到html文件;第二个是一个 webview。例如,当我点击 listview 中的"First Topic" ,就会打开 webview中的 "1.html"。
我想获得 listview 中点击元素的 text值,再用下面的方法显示在第二个activity中的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 中点击的值传递到 webview activity?

  • 写回答

1条回答

  • luhuajcdd 2013-05-27 09:39
    关注
      public void onItemClick(AdapterView<?> a, View v, int position,
                        long id) {
                    String itemname = ((TextView)v).getText().toString(); //这里应该是这样的
                    Intent intent = new Intent();
                    intent.setClass(ListViewActivity.this, WebViewActivity.class);
                      Bundle b = new Bundle();
                    b.putString("defStrID", itemname);
                    intent.putExtras(b);
                     startActivity(intent);
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试