ExpandableListView 根据输入的名称搜索对应的内容并展示,点击可打开二级列表
2017-09-07 16:53
543 查看
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(
4000
);
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>
相关文章推荐
- ExpandableListView 动态加载 点击一级菜单时再加载二级菜单的内容实现
- IE8浏览器访问百度首页,输入关键字,点击“百度一下”,搜索内容出现在一个自动打开的新窗口的问题的一个原因
- 实现ExpandableListView 二级列表,点击二级条目状态的改变
- flex----遍历树Tree,根据输入的名称搜索树节点,并展示该节点
- 从键盘上输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中
- react文档demo实现输入展示搜索结果列表
- Android Studio 根据点击的列表项内容的不同生成不同的ScrollingActivity
- Android_ExpandableListView_一个ListView实现二级列表
- 追溯-----ExpandableListView,二级列表的基本使用
- ExpandableListView中包含EditText 时不能输入内容
- 从键盘上输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中
- ExpandableListView 二级列表的一个简单案例
- andriod———1.实现原生登录注册。 2.首页完成商品列表展示点击相对应的商品ID进入商品详情 3.商品详情页面展示数据,点击加入购物车进入购物车页面 4.购物车页面完成购物车编辑,删除,功能
- 从键盘上输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中
- 仿购物车二级列表Expandablelistview以及价格
- 二级列表ExpandableListView的使用
- ExpandableListView+Dialog实现弹出二级下拉列表选项。可自由用于Activity和Fragment中
- 从键盘上输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中
- Android::ExpandableListView二级列表控件的使用
- 从键盘上输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中