简体   繁体   English

Android-使活动根据单击的内容显示不同的内容

[英]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.

相关问题 Android活动生命周期-取决于安装方法吗? - Android activity lifecycle - different depending on method of install? 使userInput从主要活动显示在Android的第二个活动中 - Making userInput from main activity to display on second activity in Android 单击“获取警报”对话框项列表以显示在其他片段,适配器或活动中 - Getting alert dialog item list to display in a different fragment, adapter or activity once clicked 单击来自两个不同活动的两个单选按钮时,如何在新活动中显示文本? - How to display text in a new activity when two radio buttons from two different activities are clicked? 上下文菜单中的不同链接取决于单击的ImageView - Different Link in context menu depending on ImageView Clicked 根据用户角色使用不同的逻辑制作单个应用程序的最佳方法是什么? - What's the best way of making single app with different logic depending on the user's role? 如何使文本视图根据单击的按钮来更改其内容? (机器人,蚀) - How to make a text view that changes it content depending on what button is clicked? (android,eclipse) 使相同的常规JButton根据其子类做不同的事情 - Make the same general JButton do different things depending on its subclass 根据Javafx中的对象,使鼠标按下时发生不同的事情 - Make different things occur on Mouse Pressed depending on Object in Javafx 如何使用 JAVA 在 android 的 RecyclerView 的其他活动中显示不同的数据 - How to display different data in other activity in RecyclerView in android using JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM