简体   繁体   English

单击ListView中的项目时在另一个活动中显示列表

[英]Showing a List in another activity when clicked on an item in ListView

first of all sorry for my English if its bad :D 首先对我的英语不好,如果不好的话:D

My question is: I have 2 arraylists, one is for items on ListView, other is for when you click items they will come in other activity. 我的问题是:我有2个arraylist,一个用于ListView上的项目,另一个用于当您单击项目时它们将进入其他活动。 How can I do that? 我怎样才能做到这一点?

I wrote adapter and I can show the first ArrayList in other activity but I wanna show the second arraylist on other activity.. 我写了适配器,我可以在其他活动中显示第一个ArrayList,但是我想在其他活动中显示第二个ArrayList。

this is my first activity 这是我的第一次活动

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

    ListView listView = (ListView) findViewById(R.id.listMahluk);

    final ArrayList<String> list1= new ArrayList<String>();

ArrayList<String> list2 = new ArrayList<String>();

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_white_text, list1);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), MahlukdetayActivity.class);
            intent.putExtra("name", list1.get(position));
            startActivity(intent);
        }
    });
}

this is my second 这是我的第二次

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

    TextView exampleText = (TextView) findViewById(R.id.exampleText);

    Intent intent = getIntent();
    String name = intent.getStringExtra("name");

    exampleText.setText(name);

}

with this way i can show list1 but I wanna show list2 instead of list1. 通过这种方式,我可以显示list1,但我想显示list2而不是list1。

Do you want to send arrayList from one activity to second activity and show to list in second activity 您是否要将arrayList从一个活动发送到第二个活动并显示到第二个活动中的列表

ArrayList<String> list = new ArrayList<String>();

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("list",list);
intent.putExtras(bundle);       
this.startActivity(intent);

In Second Activity 在第二活动中

Bundle bundle = getIntent().getExtras();
ArrayList<String> list = bundle.getParcelableArrayList("list");

You can list pass values to another activity through storing in database if needed or store then in first activity preference and get them in second activity or else use can pass values through extras as 1 answer. 您可以通过在数据库中存储(如果需要)来将传递值列出到另一个活动中,或者将其存储在第一个活动首选项中,然后在第二个活动中获取它们,否则使用可以通过Extras作为1个答案传递值。

You can also make you list as static and use it in another activity. 您还可以将其列为静态列表并在其他活动中使用它。

Make It simple : Declare second arraylist as static and access it in second activity. 简单点:将第二个arraylist声明为static并在第二个活动中对其进行访问。

FirstActivity.java
publid static ArrayList<String> mMyList = new ArrayList<>();

SecondActivity.java
//access 
System.out.println("My List : "+FirstActivity.mMyList );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击ListView项时将_id从一个活动传递到另一个活动 - Passing an _id from one activity to another when ListView item is clicked 单击展开式列表视图中的子项时如何在另一个活动中打开列表视图 - How to open a listview in another activity when a child item in an expandable listview is clicked 在Android上单击列表视图项时,过滤列表视图未显示正确的结果 - Filtering Listview not showing correct result when listview item is clicked on Android 将数据从listview中的单击项传递到另一个活动时出错 - Error while passing data from clicked item in listview to another activity 单击 ListView 项时打开有关新活动的详细信息 - Open detail on new activity when ListView item is clicked 单击另一个活动时如何发送自定义数组列表的列表项的数据? - How to send data of the list item of a custom arraylist when clicked to another activity? 如何将单击的列表项值传递给另一个活动? - How To Pass Clicked List Item Value to another Activity? 如何在另一个活动中将列表项名称从列表视图转换为文本视图? - How to get the list item name from a listview to a textview in another activity? 如何在单击列表视图项目时出现勾选并且在单击下一个项目之前不会消失当 go 到另一个活动时也不会消失 - How to appeared tick when List View item clicked and do not disappeared till the next item clicked also do not disappeared when go to another activity 单击列表视图中的项目可打开活动,其中包含基于android studio中单击的项目的列表 - Click on item in listview opens activity with a list based on the item that was clicked in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM