gpt4 book ai didi

java - WebView 未从 ListView 按钮加载正确的 URL

转载 作者:行者123 更新时间:2023-12-01 09:14:50 24 4
gpt4 key购买 nike

所以我想要一个包含一堆项目的 ListView,列表中的每个项目都包含一个打开 webview 的按钮,该按钮打开一个不同的 URL,但是它没有加载正确的 URL。我使用自定义适配器设置 URL 和描述。我将描述设置为与 URL 相同,并且描述的 URL 是准确的,但是 webview 加载了不同的网站,并且看起来不一致,似乎每次启动 Activity 时都会加载不同的 URL

这是MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deities deity_data[] = new deities[]
{
new deities(R.drawable.transparent, "Test 1", "http://google.com", 1, Color.parseColor("#ff3399")),
new deities(R.drawable.transparent, "Test 2", "http://facebook.com", 1, Color.parseColor("#9933ff")),
new deities(R.drawable.transparent, "Test 3", "http://youtube.com", 1, Color.parseColor("#0099ff")),
new deities(R.drawable.transparent, "Test 4", "http://pinterest.com", 1, Color.parseColor("#009900")),
new deities(R.drawable.transparent, "Test 5", "http://twitter.com", 1, Color.parseColor("#cc0000")),
new deities(R.drawable.transparent, "Test 6", "http://facepunch.com", 1, Color.parseColor("#0000cc")),
new deities(R.drawable.transparent, "Test 7", "http://ifttt.com", 1, Color.parseColor("#333300"))

};
deityListAdapter adapter = new deityListAdapter(this,
R.layout.deity_list_item, deity_data);

ListView deityList = (ListView)findViewById(R.id.deityList);
deityList.setAdapter(adapter);



}

dietyListAdapter.java:

public class deityListAdapter extends ArrayAdapter<deities> {

Context context;
int layoutResourceId;
deities data[] = null;
deities deity;

public deityListAdapter(Context context, int layoutResourceId, deities data[]) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
View ListItem = convertView;
Holder holder;

if (ListItem == null) {

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
ListItem = inflater.inflate(layoutResourceId, parent, false);

holder = new Holder();
holder.description = (TextView)ListItem.findViewById(R.id.deityDesc);
holder.banner = (ImageView)ListItem.findViewById(R.id.deityBanner);
holder.website = (Button)ListItem.findViewById(R.id.deityWebsite);
ListItem.setTag(holder);

} else {
holder = (Holder) ListItem.getTag();
}
deity = data[position];
View.OnClickListener holderListener = new View.OnClickListener() {
public void onClick(View v) {
WebView webView = (WebView)((Activity) context).findViewById(R.id.webView);

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
ProgressBar webViewProgressBar = (ProgressBar) ((Activity) context).findViewById(R.id.webViewProgress);
webViewProgressBar.setVisibility(View.VISIBLE);
webViewProgressBar.setProgress(progress);

if (progress == 100) {
webViewProgressBar.setVisibility(View.GONE);
}

}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
return false;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.setVisibility(View.VISIBLE);
webView.loadUrl(deity.url);





}
};


holder.website.setOnClickListener(holderListener);
holder.banner.setImageResource(deity.banner);
holder.description.setText(deity.description);
holder.banner.setBackgroundColor(deity.color);
holder.description.setText(deity.url);
return ListItem;

}
static class Holder {

TextView description;
ImageView banner;
Button website;



}

dieties.java:

public class deities {

public int banner;
public String description;
public String url;
public int color;
public int id;
public deities(){
super();
}

public deities(int banner, String description, String url, int id, int color){
super();
this.banner = banner;
this.description = description;
this.url = url;
this.color = color;
this.id = id;
}
}

最佳答案

deity = data[position]; 替换为 deities deity = data[position];使用像现在这样的代码,您的 url 始终是最后呈现的项目的 url。

还有一件事,这段带有 WebView 的代码应该在 Activity 中。不要将整个 Activity 传递给适配器。创建 Adapter 将通过其与 Activity 进行通信的接口(interface),在 Activity 中实现该接口(interface),并将 Activity 作为该接口(interface)传递到 Adapter 中。与onClick相比,只需调用界面中的一个方法,所有关于WebView的工作都在Activity中完成。

关于java - WebView 未从 ListView 按钮加载正确的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40635154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com