如何从具有不同的项目列表视图相同的活动打开不同的网页? [英] How to open different webpage from same activity from the listview which has different items?

查看:86
本文介绍了如何从具有不同的项目列表视图相同的活动打开不同的网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以看到我有这是在同一个活动Ekantipur.java指着MainActivty.java3案件
但在Ekantipur.java我只有一个链接。所以,当你点击任何三个列表视图中会打开相同的链接。所以,我想打开,当你在不同的列表视图项,请点击点击列表视图中的不同位置时不同的链接。

我DONOT要做出不同的活动的。这仅仅是三案件,但我身边有20个链接,我DONOT要打20 acitvity具有相同的源$ C ​​$ c和唯一不同的网站链接。

MainActivityParent.java

 包com.example.listviewselfmade;进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类MainActivityParent扩展ListActivity {
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //存储字符串资源到数组
        的String [] = adobe_products getResources()getStringArray(R.array.newsparent)。        //绑定数组到ListAdapter
        this.setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.main,
                R.id.label,adobe_products));        ListView控件LV = getListView();        //听上点击一个列表项
        lv.setOnItemClickListener(新OnItemClickListener(){            公共无效onItemClick(适配器视图<>母公司,观景,
                    INT位置,长的id){                开关(位置){
                情况下0:
                    startActivity(新意图(com.example.listviewselfmade.MAINCHILD));                    打破;
                情况1:
                    startActivity(新意图(com.example.listviewselfmade.MAINCHILD));                    打破;
                案例2:
                    startActivity(新意图(com.example.listviewselfmade.MAINCHILD));                    打破;
                }            }
        });
    }
}

MainActivity.java

 包com.example.listviewselfmade;进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类MainActivity扩展ListActivity {
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //存储字符串资源到数组
        的String [] = adobe_products getResources()getStringArray(R.array.news)。        //绑定数组到ListAdapter
        this.setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.main,
                R.id.label,adobe_products));        ListView控件LV = getListView();        //听上点击一个列表项
        lv.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,
                    INT位置,长的id){                开关(位置){
                情况下0:
                    startActivity(新意图(com.example.listviewselfmade.EKANTIPUR));
                    打破;
                情况1:
                    startActivity(新意图(com.example.listviewselfmade.EKANTIPUR));
                    打破;
                案例2:
                    startActivity(新意图(com.example.listviewselfmade.EKANTIPUR));
                    打破;                }            }
        });
    }
}

Ekantipur.java

 包com.example.listviewselfmade;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.webkit.WebChromeClient;
进口android.webkit.WebSettings;
进口android.webkit.WebView;公共类Ekantipurbreaking延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根        //添加进度条支持
        。this.getWindow()requestFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);        的setContentView(R.layout.webviewxml);        //使进度条可见
        getWindow()。setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);        的WebView mainWebView =(的WebView)findViewById(R.id.webview);        最后活动MyActivity =这一点;
        mainWebView.setWebChromeClient(新WebChromeClient(){
            公共无效onProgressChanged(的WebView观点,诠释进度){
                //使栏消失URL被加载后,变化
                //字符串载入中...
                MyActivity.setTitle(正在加载...);
                MyActivity.setProgress(进度* 100); //使酒吧
                                                        // URL后消失
                                                        //为加载                //完成加载后返回应用程序名称
                如果(==进度100)
                    MyActivity.setTitle(R.string.app_name);
            }
        });        //启用JavaScript
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(真);        //设置客户端,以便打开该链接将在同一打​​开
        //活动
        mainWebView.setWebViewClient(新MyCustomWebViewClient());
        //加载的网址        尝试{
            mainWebView.loadUrl(http://tipfortechs.com);
        }赶上(例外五){
            e.printStackTrace();        }
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);    }
}


解决方案

 公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
    意向意图=新意图(MainActivityParent.this,MainActivity.class);
    intent.putExtra(产品,adobe_products [位置]);
    startActivity(意向);
}

然后在MainActivity的OnCreate

 字符串产品= getIntent()getStringExtra(产品)。

和任何你需要与该做的事。 pretty多从第二将数据传递到第三个活动,只需使用相应的类和数据相同。

 公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
    意向意图=新意图(MainActivity.this,Ekantipur.class);
    intent.putExtra(URL,adobe_products [位置]);
    startActivity(意向);
}

和在Ekantipur

  mainWebView.loadUrl(getIntent()getStringExtra(URL));

You can see I have three cases on the MainActivty.java which is pointing on the same activity Ekantipur.java but in Ekantipur.java I have only one link. So when you click any of the three listview it will open the same links. So I want to open different links when you click on different listview items when different position in listview is clicked.

I donot want to make different activity for that. This is just with three cases but I have around 20 links and I donot want to make 20 acitvity with same source code and only different weblinks.

MainActivityParent.java

package com.example.listviewselfmade;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivityParent extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] adobe_products = getResources().getStringArray(R.array.newsparent);

        // Binding Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
                R.id.label, adobe_products));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                switch (position) {
                case 0:
                    startActivity(new Intent("com.example.listviewselfmade.MAINCHILD"));

                    break;
                case 1:
                    startActivity(new Intent("com.example.listviewselfmade.MAINCHILD"));

                    break;
                case 2:
                    startActivity(new Intent("com.example.listviewselfmade.MAINCHILD"));

                    break;


                }

            }
        });
    }
}

MainActivity.java

package com.example.listviewselfmade;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] adobe_products = getResources().getStringArray(R.array.news);

        // Binding Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
                R.id.label, adobe_products));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                switch (position) {
                case 0:
                    startActivity(new Intent("com.example.listviewselfmade.EKANTIPUR"));
                    break;
                case 1:
                    startActivity(new Intent("com.example.listviewselfmade.EKANTIPUR"));
                    break;
                case 2:
                    startActivity(new Intent("com.example.listviewselfmade.EKANTIPUR"));
                    break;

                }

            }
        });
    }
}

Ekantipur.java

package com.example.listviewselfmade;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Ekantipurbreaking extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        // Adds Progress bar Support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.webviewxml);

        // Makes Progress bar Visible
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);

        WebView mainWebView = (WebView) findViewById(R.id.webview);

        final Activity MyActivity = this;
        mainWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Make the bar disappear after URL is loaded, and changes
                // string to Loading...
                MyActivity.setTitle("Loading...");
                MyActivity.setProgress(progress * 100); // Make the bar
                                                        // disappear after URL
                                                        // is loaded

                // Return the app name after finish loading
                if (progress == 100)
                    MyActivity.setTitle(R.string.app_name);
            }
        });

        // enable javascript
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // setting up the client so that the link opened will open in the same
        // activity
        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        // loads the url

        try {
            mainWebView.loadUrl("http://tipfortechs.com");
        } catch (Exception e) {
            e.printStackTrace();

        }
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    }
}

解决方案

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    Intent intent = new Intent(MainActivityParent.this, MainActivity.class);
    intent.putExtra("product", adobe_products[position]);
    startActivity(intent);
}

Then in the onCreate of the MainActivity

String product = getIntent().getStringExtra("product");

And whatever you need to do with that. Pretty much the same with passing data from the second to the third activity, just use the corresponding classes and data.

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    Intent intent = new Intent(MainActivity.this, Ekantipur.class);
    intent.putExtra("url", adobe_products[position]);
    startActivity(intent);
}

And in Ekantipur

mainWebView.loadUrl(getIntent().getStringExtra("url"));

这篇关于如何从具有不同的项目列表视图相同的活动打开不同的网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆