【问题标题】:How to open different webpage from same activity from the listview which has different items?如何从具有不同项目的列表视图中打开来自同一活动的不同网页?
【发布时间】:2013-10-24 21:26:48
【问题描述】:

你可以看到我在 MainActivty.java 上有三个案例,它们指向同一个活动 Ekantipur.java 但在 Ekantipur.java 我只有一个链接。因此,当您单击三个列表视图中的任何一个时,它将打开相同的链接。所以我想在单击列表视图中的不同位置时单击不同的列表视图项目时打开不同的链接。

我不想为此做不同的活动。这只是三个案例,但我有大约 20 个链接,我不想用相同的源代码和不同的网络链接制作 20 个活动。

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

    }
}

【问题讨论】:

    标签: java android listview android-listview


    【解决方案1】:
    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);
    }
    

    然后在MainActivity的onCreate中

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

    无论你需要做什么。将数据从第二个活动传递到第三个活动几乎相同,只需使用相应的类和数据即可。

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

    在埃坎蒂普尔

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

    【讨论】:

    • 谢谢。希望我能实现你所做的。我知道你是什么意思。但确实无法实施。我是一只新蜜蜂。
    • 好吧,你几乎可以复制粘贴这个。只需确保在 MainActivity 中加载需要加载的数组即可。
    • 好的。我复制粘贴了所有内容。似乎没有错误。但在 Ekantipur.java mainWebView.loadUrl(getIntent().getExtra("url"));它说该方法未定义类型意图。
    • 我在答案中进行了编辑。确保在“String[] adobe_products = getResources().getStringArray(R.array.news);”中加载正确的内容,调用“String product = getIntent().getStringExtra("product");”在此之前并相应地选择。您也可以以几乎相同的方式传递整数,只需使用 getIntegerExtra || p.s.抱歉,已经晚了,我错过了一些东西,它是 getStringExtra,而不是 getExtra
    • 哈!!!!!!!!!!!!它工作......哈哈哈谢谢男人。你很棒。俄罗斯很棒。谢谢老兄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多