-2

Please help me, I want to show other activity when clicked in "about us", "more from us" of listView, how can I do that?

   ArrayList<Word> words = new ArrayList<Word>();

    words.add(new Word("About Us", R.id.main_images));
    words.add(new Word("More From Us", R.id.main_images));

    WordAdapter adapter = new WordAdapter(this, words);

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

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //what should I write here, so I can go other when clicked in listview
        }
    });
alirza
  • 153
  • 2
  • 13
Dibas Dauliya
  • 639
  • 5
  • 20
  • Can you explain your code please? What is `R` for example? Some code is missing (`findViewById`)... – Sébastien Temprado Jun 15 '19 at 16:36
  • 1
    Possible duplicate of [How to start new activity on button click](https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – Zoe Jun 15 '19 at 19:48
  • Possible duplicate of [How to handle the click event in Listview in android?](https://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android) – Payam Asefi Jun 15 '19 at 20:02
  • I'm using R.id.main_images to import image in list view, and I'm using findViewById(R.id.list); because i've given the id of listView "list". please help me. – Dibas Dauliya Jun 16 '19 at 07:40

3 Answers3

0

You can start a new Activity like this onItemClick

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
          Word word = words.get(i); // it's the selected word
          Intent intent = new Intent(CurrentActivity.this, DetailsActivity.class);
          intent.putExtra("word", word);
          startActivity(intent);
        }
    });

Make your Word class implement Serializable/Parcelable, this way only you can send it and extract in another Activity class.

In the DetailsActivity (or whatever name you have given to the second Activity), You can extract the selected word from extra like in OnCreate method.

Word word = (Word)(getIntent().getSerializableExtra("word"));
// Work on Word

You can read more here

@whoever is voting down answers, buddy we are just trying to help. I understand you don't get notification about being polite. The answer and question may not be clear to you but the answers here are relevant and related to the question.

Deˣ
  • 4,191
  • 15
  • 24
-1

You need to create an intent to do that.

Read this link to learn how to start another activity: https://developer.android.com/training/basics/firstapp/starting-activity

jbarat
  • 2,382
  • 21
  • 23
-1

Do it in your onItemClick:

Switch(i){
 case 0:
    // "About Us" is the first item in your list(position 0) and you can call it here, like
    startActivity(new Intent(this,AboutUsActivity.class))
    break;
case 1:
    // "More From Us" is the second item in your list(position 1) and you can call it here, like
    startActivity(new Intent(this,MoreAboutUs.class))
    break;

}

Payam Asefi
  • 2,677
  • 2
  • 15
  • 26