简体   繁体   English

如何从具有不同项目的列表视图打开同一活动的不同网页?

[英]How to open different webpage from same activity from the listview which has different items?

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. 您可以在MainActivty.java上看到三种情况,它们指向同一活动Ekantipur.java,但是在Ekantipur.java中我只有一个链接。 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. 这只是三种情况,但是我有大约20个链接,我不想使用相同的源代码和仅不同的Web链接来获得20个提示。

MainActivityParent.java 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 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 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 然后在MainActivity的onCreate中

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 而在Ekantipur

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从列表视图中的数字选择器中获取不同值的不同项目 - how to take different value from numberpicker in listview for different items Listview的项目位置0包含另一个包含10个项目的listview,每个项目都打开一个活动,但文本数据不同 - Listview's item position 0 hold another listview with 10 items which each item open single activity but different text data 如何用listView项打开不同的活动? - How to open different Activities with my listView items? 打开相同的活动但结果不同。 来自MainActivity - Open same activity with different result. From MainActivity ANDROID:从2个不同的Activity(不同的Intent)开始相同的Activity - ANDROID: Start same Activity from 2 different Activity (different Intent) 如何启动一个从另一个类调用方法的活动? - How to start an activity which calls a method from a different class? RefreshingView与ListView有何不同? - How is RefreshingView different from ListView? 如何使用ListView在同一WebView活动中启动不同的URL - How to launch different URL within same WebView Activity using ListView 如何在同一活动中更改不同工具栏的菜单项? - How can I change menu items of different toolbars in the same activity? 该应用程序如何在首次启动时打开一个不同的活动,然后又打开另一个活动? - How to have the app open a different activity for the first time it is launched and a different one from then on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM