单击ListView项目发送字符串

时间:2015-12-23 13:24:39

标签: android listview android-intent onitemclicklistener

在Listview项目中,我有3个textview,我想通过单独点击每个项目来发送这个字符串值(textviews)....我的意思是点击第1项,使用意图我可以达到4个字符串来自其他活动的那个项目......这是我的代码:

lvMsg = (ListView) findViewById(R.id.listView);
    //.
    //. using cursor and attach it to adapter
    //.
    adapter = new SimpleCursorAdapter(this, R.layout.row, c,
            new String[]{"_id","address", "body"}, new int[]{
            R.id.textView6, R.id.textView5, R.id.textView7});
    lvMsg.setAdapter(adapter);


    lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            Intent i=new Intent(MainActivity.this,otherActivity.class);

            //???message should be ??

            i.putExtra( "MESSAGE" , message);
            startActivity(i);

        }
    });

感谢...

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题......将每个textview的文本都写成字符串并发送给它......

lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

String _id = ((TextView) view.findViewById(R.id.textView6)).getText().toString();
String address = ((TextView) view.findViewById(R.id.textView5)).getText().toString();
String body = ((TextView) view.findViewById(R.id.textView7)).getText().toString();

            Intent i=new Intent(MainActivity.this,otherActivity.class);

            //???message should be ??
            String message = id + address + body;

            i.putExtra( "MESSAGE" , message);
            startActivity(i);

        }
    });

答案 1 :(得分:0)

如果您想通过点击它来传递每个项目值,那么

消息应该是

String message = adapter.getItem(position).toString;

编辑

String message = ((TextView)adapter.getItem(position)).getText().toString();

答案 2 :(得分:0)

尝试这个。

lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

     TextView v = (TextView) view.findViewById(R.id.R.id.textView6);

     //to pass id value             
     Intent i=new Intent(MainActivity.this,otherActivity.class);
     i.putExtra( "MESSAGE" ,  v.getText());
     startActivity(i);
  }
});