343N 343N - 8 months ago 37
Android Question

WebView isn't loading the right URL from ListView button

So I want to have a ListView with a bunch of items, each item in the list containing a button opening a webview opening a different URL, however it's not loading the right URL's. using a custom adapter I set the URL and the description. I set the description to the same as the URL, and the description's URL is accurate, but the webview loads a different website, and it doesn't seem consistent it seems like it loads a different URL everytime I launch the activity

Here's MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
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);



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;

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

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

if (progress == 100) {

webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url)
return false;
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
return false;


return ListItem;

static class Holder {

TextView description;
ImageView banner;
Button website;



public class deities {

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

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

Answer Source

Replace deity = data[position]; with deities deity = data[position]; With code like it is now, your url is always url of last rendered item.

And one more thing, this code with WebView should be in Activity. Do not pass whole Activity to Adapter. Create interface over which Adapter will communicate with Activity, implement it in Activity and pass Activity as that interface in Adapter. Than onClick, just call one method in interface, and do whole your work about WebView in Activity.