Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用相同的单击访问两个列表视图中的项?_Java_Android_Listview_Adapter - Fatal编程技术网

Java 如何使用相同的单击访问两个列表视图中的项?

Java 如何使用相同的单击访问两个列表视图中的项?,java,android,listview,adapter,Java,Android,Listview,Adapter,我正在构建一个包含两个列表视图的报纸添加(有点):一个列表视图包含2020年的4篇文章,另一个列表视图包含2021年的4篇文章。当用户单击文章标题时,webview将打开该文章 但是,我以前只使用了一个列表视图和一个onItemClick方法。当两个列表的项目都位于位置0,1,2,3时,我真的不知道如何让onItemClick访问两个不同的列表视图 根据当前代码,用户每次单击第一个链接时,都会打开案例0的2021文章,然后打开案例0的2020文章 这是XML代码: <?xml versio

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

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

根据当前代码,用户每次单击第一个链接时,都会打开案例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;
                }

        }
    }
}
package com.example.falcomobile;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Message;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类新闻扩展AppCompativeActivity实现AdapterView.OnItemClickListener{
私有ListView ListView,listview2;
私有字符串url;
私有网络视图;
private ArrayAdapter adapt=null;
专用ArrayAdapter Adapter 2=null;
公共最终静态字符串MESSAGE_KEY=“com.example.falcmobile.MESSAGE_KEY”;
字符串[]vanguardItems=新字符串[]{
“聚光灯:BSIG”,
“苏伊士运河堵塞的影响”,
“Netflix评论:金妮和乔治亚”,
“如何成为反种族主义者”,
};
字符串[]vanguardItems2=新字符串[]{
“游戏停止疯狂”,
“意大利罗马之旅”,
“首都暴动”,
“2020年选举回顾”,
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.vanguard);
//清单1:2021篇文章
listview=(listview)findViewById(R.id.list\u vanguard\u 2021);
setOnItemClickListener(this);
自适应=新阵列自适应(此,R.layout.item,vanguardItems);
setAdapter(adapt);
//清单2:2020篇文章
listview2=(ListView)findViewById(R.id.list\u vanguard\u 2020);
listview2.setOnItemClickListener(此);
Adapter 2=新阵列适配器(此,R.layout.item,VanguardItems 2);
listview2.setAdapter(适配器2);
}
//
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
开关(parent.getId()){
案例R.id.list_vanguard_2021:
开关(位置){
案例0:
url=”https://www.bentley-vanguard.com/post/spotlight-the-bentley-sustainable-investment-group";
Intent Intent=新Intent(这是WebLookUp.class);
intent.putExtra(消息键、url);
星触觉(意向);
打破
案例1:
url=”https://www.bentley-vanguard.com/post/the-blockage-of-ever-given-in-suez-canal-impacts-global-markets";
Intent intent2=新的Intent(这是WebLookUp.class);
intent2.putExtra(消息键,url);
星触觉(intent2);
打破
案例2:
url=”https://www.bentley-vanguard.com/post/netflix-original-review-ginny-and-georgia";
Intent intent3=新的Intent(这是WebLookUp.class);
intent3.putExtra(消息键,url);
起始触觉(intent3);
打破
案例3:
url=”https://www.bentley-vanguard.com/post/i-am-working-to-become-anti-racist-here-s-how-you-can-too";
Intent intent4=新Intent(这是WebLookUp.class);
intent4.putExtra(消息键、url);
起始触觉(强度4);
打破
}
案例R.id.list_vanguard_2020:
开关(位置){
案例0:
url=”https://www.bentley-vanguard.com/post/the-gamestop-frenzy";
Intent Intent=新Intent(这是WebLookUp.class);
intent.putExtra(消息键、url);
星触觉(意向);
打破
案例1:
url=”https://www.bentley-vanguard.com/post/tour-of-rome-italy";
Intent intent2=新的Intent(这是WebLookUp.class);
intent2.putExtra(消息键,url);
星触觉(intent2);
打破
案例2:
url=”https://www.bentley-vanguard.com/post/capitol-riots-are-breaking-point-in-a-tense-time-for-american-politics";
Intent intent3=新的Intent(这是WebLookUp.class);
intent3.putExtra(消息键,url);
起始触觉(intent3);
打破
案例3:
url=”https://www.bentley-vanguard.com/post/2020-election-in-review";
Intent intent4=新Intent(这是WebLookUp.class);
intent4.putExtra(消息键、url);
起始触觉(强度4);
打破
}
}
}
}
WebView代码正在运行。提前谢谢!:(

即使在这种情况下

case R.id.list_vanguard_2021

你需要休息一下;

这很有效,谢谢!!