将变量从列表视图传递到另一个活动

时间:2013-04-04 11:54:55

标签: android listview onitemclicklistener

我一直在努力使用一些教程将listview选项传递给一个新活动并将其作为标题(我稍后会用它来做其他事情)。我已经设置了一个最好放在其中的OnClickListener

ListView listView1 = (ListView) findViewById(R.id.sportslist);

String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

listView1.setAdapter(adapter);

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

    ?????

}
});

由于

编辑:额外代码

final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);

4 个答案:

答案 0 :(得分:2)

首先,您需要获取所选项目:

final String selected = items[position];

或者正如刮刀驱动建议

final String selected = (String) parent.getSelectedItem();

然后,您需要将该字符串作为额外内容传递给新活动

Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);

最后是你的下一个活动

    Intent in = getIntent();
    String name = in.getStringExtra(("name"));//gets name from intent

答案 1 :(得分:0)

public void onItemClick(AdapterView<?> parent, View view,int position, long id)中添加此代码。

Intent i = new Intent(getApplicationContext(), NextClass.class);
i.putExtra("selectedItem", items[position]);
startActivity (i);

获取下一课程活动的价值:

String SelectedItem = getIntent().getStringExtra("selectedItem");

答案 2 :(得分:0)

public void onItemClick(AdapterView parent,View view,int position,long id)

String str = items[position];
Intent in = new Intent(getApplicationContext(), NextClass.class);
in.putExtra("itemkey", str);
startActivity (in);

}

答案 3 :(得分:0)

将以下内容添加到您拥有listview

的活动中

以下是variable,其中包含您要传递给value的{​​{1}}

other activity声明

之前声明它
Oncreate

将以下代码添加到// Activity_1 public final static String send_to_other_activity="ListViewSelected_ID";

listView1.setOnItemClickListener

现在在其他Intent i= new Intent(Recipe_List.this,Recipe_View.class); i.putExtra(send_to_other_activity, string.valueof(position)); // itz (key-value) pair on the left key thru which u will access it on other place. on the right value that you want to pass // Iam passing posiion to other activity here startActivity(i); 上通过向其他活动的oonCreate添加以下语句,从activity中提取此值

key

现在你在//Activity_2 getdata_from_list =getIntent().getStringExtra(Activity_1.send_to_other_activity);

中有了所需的价值