【问题标题】:send string by clicking on ListView items通过单击 ListView 项目发送字符串
【发布时间】:2015-12-23 13:24:38
【问题描述】:

在 Listview 项目中,我有 3 个文本视图,我想通过分别单击每个项目来发送此字符串值(文本视图)......我的意思是通过单击第 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);

        }
    });

谢谢...

【问题讨论】:

  • 你的问题还不够清楚,下节课需要发什么?您可以使用view.findViewById(R.id.textView6)).getText() 从文本视图中获取值。

标签: android listview android-intent onitemclicklistener


【解决方案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);

        }
    });

【讨论】:

  • @hamed 如果有帮助,请不要忘记接受答案。这将帮助其他用户更轻松地找到答案。
【解决方案2】:

试试这个。

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);
  }
});

【讨论】:

    【解决方案3】:

    如果你的意思是你想通过点击来传递每个项目的值,那么

    消息应该是

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

    编辑

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多