ExpandableListView 根据输入的名称搜索对应的内容并展示,点击可打开二级列表

本文介绍了一款基于Android平台的菜谱搜索应用开发过程。该应用利用JuHe API获取菜谱数据,并通过ExpandableListView展示搜索结果。文章详细阐述了使用AsyncTask进行后台数据加载、Gson解析JSON数据及Universal Image Loader加载图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MainActivity


import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean.ResultBean.DataBean> list;
    private ArrayList llist = new ArrayList();
    private Gson gson;
    private ExpandableListView listView;
    private String path;
    //private String path = "http://apis.juhe.cn/cook/query?key=ab2da3f9abd4556dfd68086a66ffc4a8&menu=%E7%BA%A2%E7%83%A7%E8%82%89&rn=10&pn=3";
    private EditText et;
    private Button bt;
    private boolean bool = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ExpandableListView) findViewById(R.id.listview);
        et = (EditText) findViewById(R.id.edit);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = et.getText().toString().trim();
                if (trim ==null||trim.equals("")){
                    Toast.makeText(MainActivity.this, "输入的菜名不能为空,请重新输入", Toast.LENGTH_SHORT).show();
                }else {
                    try {
                        path = "http://apis.juhe.cn/cook/query?key=ab2da3f9abd4556dfd68086a66ffc4a8&menu=" + URLEncoder.encode(trim, "utf-8") + "&rn=10&pn=3";
                        new My().execute();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        });

    }

    class My extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            String string = "";
            try {
                URL url = new URL(path);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setConnectTimeout(5000);
                con.setReadTimeout(5000);
                int code = con.getResponseCode();
                if (code == 200) {
                    InputStream is = con.getInputStream();
                    byte[] b = new byte[1024];
                    int l = 0;
                    while ((l = is.read(b)) != -1) {
                        String str = new String(b, 0, l);
                        string += str;
                        bool = true;
                    }
                } else {
                    return string;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return string;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (bool) {
                gson = new Gson();
                Bean bean = gson.fromJson((String) o, Bean.class);
                list = bean.getResult().getData();
                MyAdapter myAdapter = new MyAdapter();
                listView.setAdapter(myAdapter);
            } else {
                Toast.makeText(MainActivity.this, "输入的菜名有误,请重新输入", Toast.LENGTH_SHORT).show();
            }

        }
    }

    class MyAdapter extends BaseExpandableListAdapter{

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

        @Override
        public int getChildrenCount(int i) {
            return list.get(i).getSteps().size();
        }

        @Override
        public Object getGroup(int i) {
            return list.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return list.get(i).getSteps().get(i1);
        }

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

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

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

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            Hand hand = null;
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.item, null);
                hand = new Hand();
                hand.te = view.findViewById(R.id.te1);
                hand.img = view.findViewById(R.id.img1);
                view.setTag(hand);
            } else {
                hand = (Hand) view.getTag();
            }
            hand.te.setText(list.get(i).getTitle());
            List<String> albums = list.get(i).getAlbums();
            String s = albums.toString().trim();
            ImageLoader il = ImageLoader.getInstance();
            il.displayImage(s,hand.img);
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            Hand hand = null;
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.item_iten, null);
                hand = new Hand();
                hand.img = view.findViewById(R.id.img2);
                hand.te = view.findViewById(R.id.te2);
                view.setTag(hand);
            } else {
                hand = (Hand) view.getTag();
            }
            hand.te.setText(list.get(i).getSteps().get(i1).getStep());
            String img = list.get(i).getSteps().get(i1).getImg();
            ImageLoader il = ImageLoader.getInstance();
            il.displayImage(img,hand.img);
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }
    }
    class Hand {
        TextView te;
        ImageView img;
    }
}


Myapp


import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by lenovo on 2017/09/06.
 */

public class Myapp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(getApplicationContext());
        ImageLoader.getInstance().init(aDefault);
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
>

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name=".Myapp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


activity_main

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edit"
                android:hint="请输入菜名..."
                android:layout_width="240dp"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="搜索" />
        </LinearLayout>

        <ExpandableListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ExpandableListView>
    </LinearLayout>


</RelativeLayout>

item

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/te1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="456" />
    </LinearLayout>
</LinearLayout>


item_iten

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img2"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/te2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="456" />
    </LinearLayout>
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值