如何在新的公开活动中点击项目名称

时间:2016-05-14 13:44:28

标签: android listview listitem

当我点击List项目时,我想打开一个新活动并显示项目名称被点击。我尝试了很多,但点击项目名称不是吐司,新活动打开但不显示项目名称,我如何在新的开放活动中获取点击项目名称?那我该怎么做呢?

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
        // TODO Auto-generated method stub
        String strName = listitem.get(arg2).getOppid();

        Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
                .show();
        Intent intent = new Intent(getApplicationContext(), Second_activity.class);
        startActivity(intent);

    }
});

它在同一活动中成功举杯项目,但我也希望在新活动中显示项目名称。

我想在第二次活动中点击项目名称,我尝试了这种方式,但未能解决。

public class Second_activity extends Activity {

    ListView lv;
    ArrayList<Services>listitem;
    String title,name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);

        Intent i = getIntent();
        name= i.getStringExtra("name");
        Toast.makeText(Second_activity.this, name, Toast.LENGTH_LONG).show();

9 个答案:

答案 0 :(得分:2)

您可以通过Intent(intent.putExtra())或EventBus发送字符串,也可以将其放入SharedPreferences中。选择你喜欢的任何一个。

  • 如果您不需要发送任何复杂的数据,Intent将会很好地完成 一个简单的stringint或其他东西。
  • 如果您需要在何时访问该数据,则SharedPreferences是有意义的 您下次访问您的应用程序,这意味着应该保存数据。
  • 如果您希望发送自己的自定义对象,请使用EventBus 包含存储在其中的各种数据。

答案 1 :(得分:1)

修改了您的代码

lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                     String strName = listitem.get(arg2).getOppid();

                    Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
                    .show();
                    Intent intent = new Intent(getApplicationContext(), Second_activity.class);
          //add below line to take clicked item to next activity
          intent.putExtra("ITEMCLICKED",strName);
                    startActivity(intent);

                }
            });

在第二个代码中添加以下代码创建方法

  String mClickedItem;
//get the inten from the previous activity
Intent intent=getIntent();
//intent.hasExtra("ITEMCLICKED") to check intent has the value which we have set in previous activity
if(intent.hasExtra("ITEMCLICKED")){
mClickedItem = intent.getStringExtra("ITEMCLICKED");
 }
 Toast.makeText(Welcome.this, mClickedItem,Toast.LENGTH_LONG)
                    .show();

答案 2 :(得分:0)

试试这个

Toast.makeText(getApplicationContext(), strName, Toast.LENGTH_LONG).show();

答案 3 :(得分:0)

将名称添加到附加内容并将其传递给新活动。在新活动中,从临时演员和节目中获取名称。

答案 4 :(得分:0)

将此添加到您的代码中 Intent intent = new Intent(getApplicationContext(), Second_activity.class); intent.putStringExtra("name",strName);

答案 5 :(得分:0)

Aesthetics must be either length 1 or the same as the data (99): x, y

您的第二项活动

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
        // TODO Auto-generated method stub
        String strName = listitem.get(arg2).getName();

        Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
                .show();
        Intent intent = new Intent(this, Second_activity.class);
        intent.putExtra("string", strName);
        startActivity(intent);

    }
});

答案 6 :(得分:0)

Bundle bundle = new Bundle();
String strName = listitem.get(arg2).getOppid();

bundle.putString("msg", strName);
Intent intent =newIntent(getApplicationContext(),Second_activity.class);
intent.putExtras(bundle);

然后在secondActivity的onCreate

String value = getIntent().getExtras().getString(key);
Toast.makeText(Welcome.this, value, Toast.LENGTH_LONG).show();

答案 7 :(得分:0)

看看这个:

var parent = document.getElementById("leftSide");
parent.removeChild(parent.lastElementChild);

答案 8 :(得分:0)

这是你的代码:

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
        // TODO Auto-generated method stub
        String strName = listitem.get(arg2).getOppid();

        Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), Second_activity.class);
            // you need to set the string here
        startActivity(intent);

    }
});

所以,你必须在你的第一个活动上设置它:

intent.putExtra("item-name", strName);

在你的第二个活动中:

 Intent intent = getIntent();
 String name = intent.getExtras().getString("item-name");
 Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

另外,我建议你先检查一下额外的内容:

Intent intent = getIntent();
Bundle extras  = intent.getExtras();
if(extras.containsKey("item-name")){
    //asking for your key and do something with it
}