如何使用相同的 onItemClick 访问两个列表视图中的项目?

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

我正在构建一个带有两个列表视图的报纸添加(有点):一个列表视图包含 2020 年的 4 篇文章,另一个列表视图包含 2021 年的 4 篇文章。当用户点击文章标题时,网络视图会打开这篇文章。

但是,我以前只使用过一种列表视图和一种onItemClick 方法。当两个列表的位置 0、1、2、3 都有项目时,我真的不确定如何让 onItemClick 访问两个不同的列表视图。

使用我的当前代码,例如每次用户点击第一个 link,它会打开案例 0 的 2021 年文章,然后是案例 0 的 2020 年文章。

这是XML代码:

<?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>

这是 java 代码:

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;
                }

        }
    }
}

WebView 代码正在运行。提前致谢! :(

即使在这个案例之后

case R.id.list_vanguard_2021

你需要一个break;