【问题标题】:how to pass the selected value to another activity?如何将选定的值传递给另一个活动?
【发布时间】:2013-02-07 13:25:16
【问题描述】:

我是安卓新手..

我正面临意图问题..

我的问题是...我想将用户选择的值从一个活动的字符串数组列表传递给另一个活动...这些值是字符串..通过 JSON 从数据库获取。并且这些值存储在一个数组列表中..

现在我需要将值从一个活动传递到另一个活动......通过使用意图......

      lvForDialog = (ListView) viewList.findViewById(R.id.List_view);
        ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist));
        lvForDialog.setAdapter(adapter);        
        lvForDialog.setOnItemClickListener(new OnItemClickListener()
    {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position,long id)        
          {            
        Intent intent = new Intent(Nexttopic.this,Question.class);     
    intent.putExtra(TAG_TOPICNAME, tname);

我想将TAG_TOPICNAME 传递给另一个活动。用户选择哪个主题名称我想传递该名称...

如何做到这一点?

非常感谢..

【问题讨论】:

  • 你必须看到这个链接:- stackoverflow.com/questions/14705964/…>
  • @hcd 那是按钮...我需要 onItemclickListener...
  • 感谢大家..回答我的问题..并指导我...

标签: android android-intent arraylist


【解决方案1】:

试试这个:

i.putExtra("Name", tname );
startActivity(i);

在您的第一个 Activity 中,然后您可以将下面的代码添加到您的第二个 Activity 并获取您的数据。

Intent intent = getIntent();
String Name = intent.getExtras().getString("Name");

编辑: 以此为例在 onitemclick 监听器中获取选定项目

@Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{ 
    Cursor GettName = (Cursor)viewList.getItemAtPosition(position);//Get a Cursor from the selected position to access the selected Item
    String tname = GettName.getString(GettName.getColumnIndex(CustomerDBAdapter.KEY_TNAME)); // get approporiate String from that cursor
    i.putExtra("Name", tname );
    startActivity(i);
}

【讨论】:

  • 用户选择的值应该通过什么
  • 例如在列表视图中,当用户单击列表项时,您应该使用 selecteditem 并实现 onitemclick 侦听器,然后您可以使用它传递给第二个 Activity。它在其他实现中没有区别,这里 tname 是一个字符串,所以你只需要用用户选择的项目来填充它
  • 就像只有我在 onitemclick 监听器中写的那样,请查看我的代码..无论用户选择它单独传递第一个值..未选择值..
  • 看看我编辑的答案中的一个例子希望它给了你这个想法。祝你好运
  • 如何实现我无法理解的代码?我是android新手..请指导我..
【解决方案2】:

你应该调用startActivity(learnintent);,在下面几行之后你的代码中缺少它

Intent learnintent = new Intent(Nexttopic.this,Question.class);     
learnintent.putExtra(TAG_TOPICNAME, tname);

我希望你在使用之前也定义了常量TAG_TOPICNAME

然后在Question 活动中执行如下操作来访问该值。

String topicName = intent.getIntent().getStringExtra(TAG_TOPICNAME);

这里的TAG_TOPICNAME 应该和我在之前的活动中分配的值相同,最好定义一个class Constants 并将这个字符串常量放在那里,并在两个地方都使用它。

【讨论】:

  • 不,我没有得到..我想要用户选择我应该传递的值...请指导我
【解决方案3】:

执行此操作的最简单方法是将会话 ID 传递给您用于启动活动的 Intent 中的注销活动:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

意图的docs 有更多信息(查看标题为“附加”的部分)。

【讨论】:

    【解决方案4】:

    您可以使用捆绑包传递值。您可以捆绑它并使用意图传递它。示例代码如下所示......

    捆绑 = 新捆绑(); data1 = Double.valueOf(myEditText.getText().toString());

        bundle.putDouble("data1", data1);
    
        Intent intent = new Intent(this, AnotherActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    

    从意图中获取它的示例

     Bundle b = getIntent().getExtras();
            noqs = b.getDouble("data1");
            mTvCat.setText("hhhhhh"+point+noqs);
    

    如果它适合你,那么你可以将它存储在一个静态变量中。您可以从应用程序的任何位置访问它。

    【讨论】:

    • 值的类型是什么???可以存储在一个常量值中。我认为这会有所帮助,因为如果你将它存储在一个常量值中,你不需要通过 bundle 传递它,你也可以使用类名直接从其他活动中调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多