1

同じアクティビティ Ekantipur.java を指している MainActivty.java に 3 つのケースがあることがわかりますが、Ekantipur.java にはリンクが 1 つしかありません。したがって、3 つのリストビューのいずれかをクリックすると、同じリンクが開きます。リストビューの別の位置がクリックされたときに別のリストビュー項目をクリックすると、別のリンクが開きます。

そのために別の活動をしたくありません。これは 3 つのケースだけですが、私は約 20 のリンクを持っており、同じソース コードと異なる Web リンクのみで 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);

    }
}
4

1 に答える 1

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

そして、あなたがそれを行う必要があることは何でも。2 番目のアクティビティから 3 番目のアクティビティにデータを渡す場合とほとんど同じで、対応するクラスとデータを使用するだけです。

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"));
于 2013-10-24T21:39:46.353 に答える