简体   繁体   中英

same webview for different activities

i have many buttons in main activity that each button link to another activity that contain listview that each item in listview link one html file that is in asset folder. in my codes i'm using one webview activity for each activity. how can i use one same webview activity for all activity. i have eight different activities with different html files. and i try to use one webview for others and always result is one html for all address.

one of my activities:

public class Dia_Page extends ListActivity {
public ListView lv;
public String number_of_keys;
public String[] values = new String[] {"dia 1",
        "dia 2","dia 3"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dia__page);

    setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1,values));


    lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
            int position, long id) {                        
            Intent myIntent = new Intent(Dia_Page.this,Dia_sec.class);
            myIntent.putExtra("key",position);
            startActivity(myIntent);    
        }

    });
}

my webview activity:

web2.setWebViewClient(new myWebClient());
    web2.getSettings().setJavaScriptEnabled(true);
    int pos = getIntent().getIntExtra("key", 0);
    if (pos == 0) {
        web2.loadUrl("file:///android_asset/dia/diaone.html");
    } else if (pos == 1) {
        web2.loadUrl("file:///android_asset/dia/diatwo.html");
    } else if (pos == 2) {
        web2.loadUrl("file:///android_asset/dia/diathree.html");
    }

how can i use same webview for show all activities html that are in different activities.

Try changing the code in webview activity like this. `

String pos="";
in = getIntent();

web2.setWebViewClient(new myWebClient());
web2.getSettings().setJavaScriptEnabled(true);

pos= in.getStringExtra("key");

if (pos.equals("0")) {
         web2.loadUrl("file:///android_asset/dia/diaone.html");
} else if (pos.equals("1")) {
         web2.loadUrl("file:///android_asset/dia/diatwo.html");
} else if (pos.equals("2")) {
         web2.loadUrl("file:///android_asset/dia/diathree.html");
}`

I'm posting this answer to my question to those friends that have a problem like mine. in each activity that contains listview i put this codes:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
            int position, long id) {                        
            Intent intent = new Intent(getApplicationContext(), Dia_sec.class);
            switch(position){   
            case 0 :
                intent.setData(Uri.parse("file:///android_asset/can/canone.html"));
                break;
            case 1 :
                intent.setData(Uri.parse("file:///android_asset/can/cantwo.html"));
                break;
            case 2 :
                intent.setData(Uri.parse("file:///android_asset/can/canthree.html"));
                break;
            // etc
            }
            startActivity(intent);

and in my webview "dia_sec" i add this code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dia_sec);
    web2.loadUrl(getIntent().getDataString());

I had about 8 different activities that contains their own listview with different html files. and now i put code in activity and link them to my webview page. problem solved and now i have one webview that i use it to show all html files from different source and different listviews. Thanks alot every body to try help me in this way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM