This is a webview question from the Expandable List View

Asked 2 years ago, Updated 2 years ago, 49 views

It's a code that even creates an expandable list view in Android Studio and displays related toast when you click on the Child list.

I want to display toast here and at the same time type a different url in each child list to display the corresponding web views, what should I do?

MainActivity.java

package com.example.myexpandable;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    ExpandableListView listMain;

    private ArrayList<String> arrayGroup = new ArrayList<String>();
    private HashMap<String,ArrayList<String>> arrayChild = new HashMap<String, ArrayList<String>>();

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

        listMain = (ExpandableListView) this.findViewById(R.id.expandableListView1);
        setArrayDate();
        listMain.setAdapter(new AdptMain(this, arrayGroup, arrayChild));


        listMain.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String menu = arrayChild.get( arrayGroup.get(groupPosition)).get(childPosition);
                Toast.makeText(getApplicationContext(), menu+" Go to Home Page~", ~", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

    }

    private void setArrayDate() {
        arrayGroup.add ("Hamburger Store");
        arrayGroup.add ("Pizza Store");
        arrayGroup.add ("Chinese restaurant");

        ArrayList<String> arrayHamberger = new ArrayList<String>();
        arrayHamberger.add ("McDonald");
        arrayHamberger.add ("Burger King");
        arrayHamberger.add ("Lotteria");

        ArrayList<String> arrayPizza = new ArrayList<String>();
        arrayPizza.add ("Pizza Hut");
        arrayPizza.add ("Pizza School");
        arrayPizza.add ("Mr. Pizza");
        arrayPizza.add ("Pizza Land");
        arrayPizza.add ("Pizza Alvolo");

        ArrayList<String> arrayChinese = new ArrayList<String>();
        arrayChinese.add ("Cheongmaru");
        arrayChinese.add ("difference");
        arrayChinese.add ("Hong Kong Spot";

        arrayChild.put(arrayGroup.get(0), arrayHamberger);
        arrayChild.put(arrayGroup.get(1), arrayPizza);
        arrayChild.put(arrayGroup.get(2), arrayChinese);
    }

}


AdptMain.java

package com.example.myexpandable;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class AdptMain extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<String> arrayGroup;
    private HashMap<String, ArrayList<String>> arrayChild;

    public AdptMain( Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<String>> arrayChild){
        super();
        this.context = context;
        this.arrayChild= arrayChild;
        this.arrayGroup= arrayGroup;


    }



    @Override
    public int getGroupCount() {
        return arrayGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arrayChild.get( arrayGroup.get( groupPosition )).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return arrayGroup.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arrayChild.get( arrayGroup.get( groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String groupName = arrayGroup.get(groupPosition);
        View v  = convertView;

        if( v == null){
            LayoutInflater inflater=  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (RelativeLayout)inflater.inflate(R.layout.listviewitem_group, null);
        }
        TextView textGroup = (TextView) v.findViewById(R.id.textGroup);
        textGroup.setText(groupName);

        return v;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
        View v  = convertView;


        if( v == null){
            LayoutInflater inflater=  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (RelativeLayout)inflater.inflate(R.layout.listviewitem_child, null);
        }
        TextView textChild = (TextView) v.findViewById(R.id.textChild);
        textChild.setText(childName);

        return v;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:andriod="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myexpandable.MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

listviewitem_group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:padding="10dp"
        android:text="Child"/>

</RelativeLayout>

listviewitem_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:padding="20dp"
        android:text="Group" />

</RelativeLayout>

android listview

2022-09-22 11:07

1 Answers

When you create a Child list, please modify the code so that you can add Url information along with the name. We are currently adding items to the Child list as follows.

ArrayList<String> arrayPizza = new ArrayList<String>();
arrayPizza.add ("Pizza Hut");
arrayPizza.add ("Pizza School");
arrayPizza.add ("Mr. Pizza");
arrayPizza.add ("Pizza Land");
arrayPizza.add ("Pizza Alvolo");

Please create a separate model that can store both the name and Url information and add items to the list as follows. The code has only written the necessary parts.

ArrayList<Store> arrayPizza = new ArrayList<Store>();
arrayPizza.add (new Store ("Pizza Hut", "http://..."));
arrayPizza.add ("pizza school", "http://...");
arrayPizza.add ("Mr. Pizza", "http://...");
arrayPizza.add (new Store ("Pizza Land", "http://..."));
arrayPizza.add (new Store ("Pizza Alvolo", "http://..."));

...

arrayChild.put(arrayGroup.get(0), arrayPizza);

...

static class Store {

    String name;
    String url;

    public Model(String name, String url) {
        this.name = name;
        this.url = url;
    }
}

If you don't want to create a separate model, you can also use the Pair class. (Note, you can get the name and Url from the click listener through the first, second field of the pair. For more information, see the document .)

ArrayList<Pair> arrayPizza = new ArrayList<>();
arrayPizza.add (new Pair<>("Pizza Hut", "http://..."));
arrayPizza.add (new Pair<>("Pizza School", "http://..."));
arrayPizza.add (new Pair<>("Mr. Pizza", "http://..."));
arrayPizza.add("pizza land", "http://...");
arrayPizza.add("pizza albolo", "http://...");

...
arrayChild.put(arrayGroup.get(0), arrayPizza);

...

Currently, the code prints the name as toast when clicked. You can import Url in the same way and display the web view. I didn't write the code because I thought I could solve this part without the code.


2022-09-22 11:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.