V
V
Vetal Matitskiy2016-04-05 03:10:14
Android
Vetal Matitskiy, 2016-04-05 03:10:14

Is it possible to handle two ListViews within the same activity on android?

I'm making a small application that shows multiple ListViews through a ViewFlipper. To process clicks, I use the code found on developer.alexanderklimov.ru/android/views/listview.php , writing it separately for each component.

ArrayAdapter<String> eventAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, events);
        lstEvents.setAdapter(eventAdapter);
        lstGreetings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
                display("for event " + position);
            }
        });
        
        ArrayAdapter<String> greetAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, greetings);
        lstGreetings.setAdapter(greetAdapter);
        lstGreetings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
                display("for greet " + position);
            }
        });

Unfortunately, this only processes clicks on the last of the lists. I can’t figure out how to modify the code so that clicks in each of the lists are correctly processed. I will be very grateful for hints. the original processing code looks like
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
      long id) {
    Toast.makeText(getApplicationContext(), ((TextView) itemClicked).getText(),
            Toast.LENGTH_SHORT).show();
  }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Reznichenko, 2016-04-05
@ReznichenkoLex

Well, you put a listener twice on the same listview.
lstGreetings.setOnItemClickListener

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question