大佬教程收集整理的这篇文章主要介绍了如何使用同一个 onItemClick 访问两个列表视图中的项目?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个带有两个列表视图的报纸添加(有点):一个列表视图包含 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://scheR_593_11845@as.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,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,url);
startActivity(intent);
break;
case 1:
url = "https://www.bentley-vanguard.com/post/tour-of-rome-italy";
Intent intent2= new Intent(this,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,url);
startActivity(intent3);
break;
case 3:
url = "https://www.bentley-vanguard.com/post/2020-election-in-revIEw";
Intent intent4= new Intent(this,url);
startActivity(intent4);
break;
}
}
}
}
WebVIEw 代码正在运行。提前致谢! :(
以上是大佬教程为你收集整理的如何使用同一个 onItemClick 访问两个列表视图中的项目?全部内容,希望文章能够帮你解决如何使用同一个 onItemClick 访问两个列表视图中的项目?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。