简体   繁体   中英

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

first of all sorry for my English if its bad :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. 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..

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.

Do you want to send arrayList from one activity to second activity and show to list in second activity

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM