[英]Android - Making an activity display different things depending on what is clicked
I have a listview that is populated by an arraylist of classes. 我有一个由类的arraylist填充的listview。
public void populateThingsList(){
listExample.add(new Example("Example name", 1, "Example detail 1", "Example detail 2", R.drawable.1_icon, "Example detail 3"));
listExample.add(new Example("Example name1", 2, "Example detail 1", "Example detail 2", R.drawable.2_icon, "Example detail 3"));
listExample.add(new Example("Example name2", 3, "Example detail 1", "Example detail 2", R.drawable.3_icon, "Example detail 3"));
listExample.add(new Example("Example name3", 4, "Example detail 1", "Example detail 2", R.drawable.4_icon, "Example detail 3"));
listExample.add(new Example("Example name4", 5, "Example detail 1", "Example detail 2", R.drawable.5_icon, "Example detail 3"));
listExample.add(new Example("Example name5", 6, "Example detail 1", "Example detail 2", R.drawable.6_icon, "Example detail 3"));
listExample.add(new Example("Example name6", 7, "Example detail 1", "Example detail 2", R.drawable.7_icon, "Example detail 3"));
listExample.add(new Example("Example name7", 8, "Example detail 1", "Example detail 2", R.drawable.8_icon, "Example detail 3"));
listExample.add(new Example("Example name8", 9, "Example detail 1", "Example detail 2", R.drawable.9_icon, "Example detail 3"));
listExample.add(new Example("Example name9", 10, "Example detail 1", "Example detail 2", R.drawable.10_icon, "Example detail 3"));
}
This is the code for the clicklistener. 这是单击侦听器的代码。
ListView list = (ListView) findViewById(R.id.exampleListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Example clickedexample = listExample.get(position);
startActivity(new Intent("com.example.app.EXAMPLEACTIVITY"));
}
}
Is there any way I can get the information from clickedExample and set the textViews and imageView in ExampleActivity depending on which one was clicked? 有什么方法可以从clickedExample中获取信息,并根据被单击的内容在ExampleActivity中设置textViews和imageView?
You can use an Intent and extras. 您可以使用Intent和其他功能。 Call putExtra with any primitive type or String on the Intent before calling startActivity and in ExampleActivity use getIntent and getExtra to retrieve the data you added.
在调用startActivity之前,在Intent上调用具有任何原始类型或String的putExtra,并在ExampleActivity中使用getIntent和getExtra检索添加的数据。
When calling Activity
, use put methods. 调用
Activity
,请使用put方法。 When retrieving, use getExtras()
. 检索时,请使用
getExtras()
。
ListView list = (ListView) findViewById(R.id.exampleListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Example clickedexample = listExample.get(position);
String text = clickedexample.getDetails1(); // Assuming you have this in your Example code
Integer drawableInt = clickedexample.getIcon(); // Same assumption
Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY");
intent.putString("text",text);
intent.putInt("icon",drawableInt);
startActivity(intent);
}
}
There's a few ways you could do this. 有几种方法可以做到这一点。 One is to make your Example object implement Parcelable and then set that in an intent.
一种是使您的Example对象实现Parcelable,然后将其设置为意图。 Your second option is to create an intent and just set the resource and each string individually like so:
您的第二个选择是创建一个意图,并像下面这样分别设置资源和每个字符串:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Example clickedexample = listExample.get(position);
Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY");
intent.putExtra("detail_1", clickedexample.getDetail1());
intent.putExtra("detail_2", clickedexample.getDetail2());
intent.putExtra("detail_3", clickedexample.getDetail3());
intent.putExtra("detail_icon", clickedexample.getIcon());
startActivity(intent);
}
then in your activity... 然后在你的活动中...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String e1 = getIntent().getStringExtra("detail_1");
String e2 = getIntent().getStringExtra("detail_2");
String e3 = getIntent().getStringExtra("detail_3");
int icon = getIntent().getIntExtra("detail_icon", 0);
Example e = new Example(e1, e2, icon, e3);
}
Use putExtra and then getExtra : 使用putExtra然后使用getExtra :
Intent intent = new Intent("com.example.app.EXAMPLEACTIVITY");
intent.putExtra("Example_Name", clickedexample.name);
intent.putExtra("Example_detail", clickedexample.detail);
startActivity(intent);
In your EXAMPLEACTIVITY
: 在您的
EXAMPLEACTIVITY
:
void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String name = intent.getStringExtra("Example_Name");
String detail = intent.getStringExtra("Example_detail");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.