Search code examples
javaandroidlistviewadapter

How can the items in two list views be accessed with the same onItemClick?


I'm building a newspaper add (kinda) with two lists views: one list view contains 4 articles from 2020 and the other one has 4 articles from 2021. When the user clicks on an article title, webview opens up the article.

However, I've only previously used one list view and one onItemClick method. I'm really not sure how to get onItemClick to access two different list views when both lists have items in position 0,1,2,3.

With the current code I have each time a user clicks for example on the first link, it will open the article of 2021 of case 0 and then the article of 2020 of case 0.

This is the XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DFE7EC">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="#0075BE"
        android:gravity="center"
        android:text="The Vanguard"
        android:textColor="#FFFFFF"
        android:textSize="35sp"
        android:textStyle="bold"
        android:layout_marginBottom="30dp"/>

    <TextView
        android:id="@+id/year_2021"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="2021"
        android:textColor="#000000"
        android:textSize="25sp"
        android:textStyle="bold"
        android:fontFamily="monospace"
        android:layout_marginBottom="10dp"/>

    <ListView
        android:id="@+id/list_vanguard_2021"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:divider="#0075BE"
        android:dividerHeight="5dp"
        android:drawSelectorOnTop="true"
        />

    <TextView
        android:id="@+id/year_2020"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="2021"
        android:textColor="#000000"
        android:textSize="25sp"
        android:textStyle="bold"
        android:fontFamily="monospace"
        android:layout_marginBottom="10dp"/>

    <ListView
        android:id="@+id/list_vanguard_2020"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:divider="#0075BE"
        android:dividerHeight="5dp"
        android:drawSelectorOnTop="true"
        />

</LinearLayout>

This is the java code:

package com.example.falcmobile;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class News extends AppCompatActivity implements AdapterView.OnItemClickListener{

    private ListView listview, listview2;
    private String url;
    private WebView webView;
    private ArrayAdapter<String> adapt = null;
private ArrayAdapter<String> adapt2 = null;
public final static String MESSAGE_KEY ="com.example.falcmobile.message_key";

String[] vanguardItems = new String[]{
        "Spotlight: BSIG",
        "The Impact of the Blockage of the Suez Canal",
        "Netflix Review: Ginny & Georgia",
        "How to Become Anti-Racist",
};

String[] vanguardItems2 = new String[]{
        "The GameStop Frenzy",
        "Tour of Rome, Italy",
        "Capital Riots",
        "2020 Election in Review",
};

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

    // LIST 1: 2021 ARTICLES
    listview = (ListView) findViewById(R.id.list_vanguard_2021);
    listview.setOnItemClickListener(this);
    adapt = new ArrayAdapter<String>(this, R.layout.item, vanguardItems);
    listview.setAdapter(adapt);

    // LIST 2: 2020 ARTICLES
    listview2 = (ListView) findViewById(R.id.list_vanguard_2020);
    listview2.setOnItemClickListener(this);
    adapt2 = new ArrayAdapter<String>(this, R.layout.item, vanguardItems2);
    listview2.setAdapter(adapt2);
}


//
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


    switch (parent.getId()) {
        case R.id.list_vanguard_2021:

            switch (position) {
                case 0:
                    url = "https://www.bentley-vanguard.com/post/spotlight-the-bentley-sustainable-investment-group";
                    Intent intent= new Intent(this ,WebLookUp.class);
                    intent.putExtra(MESSAGE_KEY,url);
                    startActivity(intent);
                    break;
                case 1:
                    url = "https://www.bentley-vanguard.com/post/the-blockage-of-ever-given-in-suez-canal-impacts-global-markets";
                    Intent intent2= new Intent(this ,WebLookUp.class);
                    intent2.putExtra(MESSAGE_KEY,url);
                    startActivity(intent2);
                    break;
                case 2:
                    url = "https://www.bentley-vanguard.com/post/netflix-original-review-ginny-and-georgia";
                    Intent intent3= new Intent(this ,WebLookUp.class);
                    intent3.putExtra(MESSAGE_KEY,url);
                    startActivity(intent3);
                    break;
                case 3:
                    url = "https://www.bentley-vanguard.com/post/i-am-working-to-become-anti-racist-here-s-how-you-can-too";
                    Intent intent4= new Intent(this ,WebLookUp.class);
                    intent4.putExtra(MESSAGE_KEY,url);
                    startActivity(intent4);
                    break;
            }

        case R.id.list_vanguard_2020:

            switch (position) {
                case 0:
                    url = "https://www.bentley-vanguard.com/post/the-gamestop-frenzy";
                    Intent intent= new Intent(this ,WebLookUp.class);
                    intent.putExtra(MESSAGE_KEY,url);
                    startActivity(intent);
                    break;
                case 1:
                    url = "https://www.bentley-vanguard.com/post/tour-of-rome-italy";
                    Intent intent2= new Intent(this ,WebLookUp.class);
                    intent2.putExtra(MESSAGE_KEY,url);
                    startActivity(intent2);
                    break;
                case 2:
                    url = "https://www.bentley-vanguard.com/post/capitol-riots-are-breaking-point-in-a-tense-time-for-american-politics";
                    Intent intent3= new Intent(this ,WebLookUp.class);
                    intent3.putExtra(MESSAGE_KEY,url);
                    startActivity(intent3);
                    break;
                case 3:
                    url = "https://www.bentley-vanguard.com/post/2020-election-in-review";
                    Intent intent4= new Intent(this ,WebLookUp.class);
                    intent4.putExtra(MESSAGE_KEY,url);
                    startActivity(intent4);
                    break;
                }

        }
    }
}

the WebView code is working. Thanks in advance! :(


Solution

  • Even after this case

    case R.id.list_vanguard_2021
    

    you need a break;