343N 343N - 19 days ago 4
Android Question

Set listview item button to load specific url without declaring final listview data?

So I was having an issue before with the URL's not loading correctly, and I've fixed that, but now I can't figure out how to set OnClick to load a specific URL since I need to declare it final, but I can't otherwise it won't work properly.

(I'm somewhat new to Java/Android)

Here's DietyAdapter.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();
}
deities deity = data[position];

View.OnClickListener holderListener = new View.OnClickListener() {
public void onClick(View v) {
WebView webView = (WebView) ((Activity) context).findViewById(R.id.webView);
webView.loadUrl(deity.url); // i want this to work without declaring diety to be final
}
}



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


MainActivity.java

public class MainActivity extends AppCompatActivity {

@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://linustechtips.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);
WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
ProgressBar webViewProgressBar = (ProgressBar) 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);






}


@Override
public void onBackPressed(){
WebView webView = (WebView)findViewById(R.id.webView);
ProgressBar progressBar = (ProgressBar)findViewById(R.id.webViewProgress);
if (webView.getVisibility() == View.VISIBLE) {
webView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}

}
}

Answer

The real problem is that you create a listener but never use it. Somewhere you need to call setOnClickListener().

Comments