package com
.example
.zuoye1
;
import android
.content
.DialogInterface
;
import android
.os
.Bundle
;
import android
.view
.LayoutInflater
;
import android
.view
.View
;
import android
.widget
.AdapterView
;
import android
.widget
.Button
;
import android
.widget
.ImageView
;
import android
.widget
.ListView
;
import android
.widget
.TextView
;
import android
.widget
.Toast
;
import androidx
.appcompat
.app
.AlertDialog
;
import androidx
.appcompat
.app
.AppCompatActivity
;
import com
.bumptech
.glide
.Glide
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class MainActivity extends AppCompatActivity {
private String path
= "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private List
<JavaBean.DataBean> list
= new ArrayList<>();
private MyApater myApater
;
private ListView tv
;
private AlertDialog alertDialog
;
@Override
protected void onCreate(Bundle savedInstanceState
) {
super.onCreate(savedInstanceState
);
setContentView(R
.layout
.activity_main
);
myApater
= new MyApater(this, list
);
MyAsync myAsync
= (MyAsync
) new MyAsync(list
, this.myApater
).execute(path
);
initView();
tv
.setAdapter(myApater
);
tv
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView
<?> adapterView
, View view
, final int i
, long l
) {
final View view1
= LayoutInflater
.from(MainActivity
.this).inflate(R
.layout
.itemduan
,null
);
final AlertDialog
.Builder builder
= new AlertDialog.Builder(MainActivity
.this);
TextView textView
= view1
.findViewById(R
.id
.itemduan_tv
);
textView
.setText(list
.get(i
).getTitle());
ImageView imageView
= view1
.findViewById(R
.id
.itemduan_im
);
Glide
.with(MainActivity
.this).load(list
.get(i
).getPic()).into(imageView
);
final Button button
= view1
.findViewById(R
.id
.itemduan_qx
);
button
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view
) {
alertDialog
.cancel();
}
});
Button button1
= view1
.findViewById(R
.id
.itemduna_q
);
button1
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view
) {
alertDialog
.cancel();
Toast
.makeText(MainActivity
.this, "这个是"+list
.get(i
).getId(), Toast
.LENGTH_SHORT
).show();
}
});
builder
.setView(view1
);
tv
.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView
<?> adapterView
, final View view
, int i
, long l
) {
AlertDialog
.Builder builder1
= new AlertDialog.Builder(MainActivity
.this);
builder1
.setIcon(R
.mipmap
.ic_launcher
);
builder1
.setTitle("Duoduo");
builder1
.setNegativeButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface
, int i
) {
list
.remove(i
+"");
myApater
.notifyDataSetChanged();
}
});
builder1
.create();
builder1
.show();
return false;
}
});
alertDialog
= builder
.create();
builder
.show();
}
});
}
private void initView() {
tv
= (ListView
) findViewById(R
.id
.tv
);
}
}
package com
.example
.zuoye1
;
import java
.util
.List
;
public class JavaBean {
private int ret
;
private List
<DataBean> data
;
public int getRet() {
return ret
;
}
public void setRet(int ret
) {
this.ret
= ret
;
}
public List
<DataBean> getData() {
return data
;
}
public void setData(List
<DataBean> data
) {
this.data
= data
;
}
public static class DataBean {
private String id
;
private String title
;
private String pic
;
private String collect_num
;
private String food_str
;
private int num
;
public String
getId() {
return id
;
}
public void setId(String id
) {
this.id
= id
;
}
public String
getTitle() {
return title
;
}
public void setTitle(String title
) {
this.title
= title
;
}
public String
getPic() {
return pic
;
}
public void setPic(String pic
) {
this.pic
= pic
;
}
public String
getCollect_num() {
return collect_num
;
}
public void setCollect_num(String collect_num
) {
this.collect_num
= collect_num
;
}
public String
getFood_str() {
return food_str
;
}
public void setFood_str(String food_str
) {
this.food_str
= food_str
;
}
public int getNum() {
return num
;
}
public void setNum(int num
) {
this.num
= num
;
}
}
}
package com
.example
.zuoye1
;
import android
.content
.Context
;
import android
.view
.LayoutInflater
;
import android
.view
.View
;
import android
.view
.ViewGroup
;
import android
.widget
.BaseAdapter
;
import android
.widget
.ImageView
;
import android
.widget
.TextView
;
import com
.bumptech
.glide
.Glide
;
import java
.util
.List
;
public class MyApater extends BaseAdapter {
private Context context
;
private List
<JavaBean.DataBean> list
;
private LayoutInflater layoutInflater
;
public MyApater(Context context
, List
<JavaBean.DataBean> list
) {
this.context
= context
;
this.list
= list
;
layoutInflater
= LayoutInflater
.from(context
);
}
@Override
public int getCount() {
return list
.size();
}
@Override
public Object
getItem(int i
) {
return list
.get(i
);
}
@Override
public long getItemId(int i
) {
return i
;
}
@Override
public View
getView(int i
, View view
, ViewGroup viewGroup
) {
ViewHolder viewHolder
= null
;
if (view
==null
){
viewHolder
= new ViewHolder();
view
= layoutInflater
.inflate(R
.layout
.jsonitem
,null
);
viewHolder
.textView
= view
.findViewById(R
.id
.jsonitem_tv
);
viewHolder
.imageView
= view
.findViewById(R
.id
.jsonitem_im
);
view
.setTag(viewHolder
);
}else{
viewHolder
= (ViewHolder
) view
.getTag();
}
viewHolder
.textView
.setText(list
.get(i
).getTitle());
Glide
.with(context
).load(list
.get(i
).getPic()).into(viewHolder
.imageView
);
return view
;
}
private class ViewHolder {
private TextView textView
;
private ImageView imageView
;
}
}
package com
.example
.zuoye1
;
import android
.os
.AsyncTask
;
import android
.util
.Log
;
import com
.google
.gson
.Gson
;
import java
.util
.List
;
public class MyAsync extends AsyncTask<String
,Void
, List
<JavaBean.DataBean>> {
private static final String TAG
= "MyAsync";
private List
<JavaBean.DataBean> list
;
private MyApater myApater
;
public MyAsync(List
<JavaBean.DataBean> list
, MyApater myApater
) {
this.list
= list
;
this.myApater
= myApater
;
}
@Override
protected List
<JavaBean.DataBean> doInBackground(String
... strings
) {
String bean
= Utils
.Bean(strings
[0]);
if (bean
!=null
){
return new Gson().fromJson(bean
,JavaBean
.class).getData();
}else{
Log
.i(TAG
, "doInBackground: 下载失败");
}
return null
;
}
@Override
protected void onPostExecute(List
<JavaBean.DataBean> dataBeans
) {
super.onPostExecute(dataBeans
);
if (dataBeans
!=null
){
list
.addAll(dataBeans
);
myApater
.notifyDataSetChanged();
}
}
}
package com
.example
.zuoye1
;
import java
.io
.File
;
import java
.io
.InputStream
;
import java
.net
.HttpURLConnection
;
import java
.net
.MalformedURLException
;
import java
.net
.URL
;
import java
.net
.URLConnection
;
public class Utils {
public static String
Bean(String path
){
InputStream is
;
StringBuffer sb
= new StringBuffer();
try {
URL url
= new URL(path
);
HttpURLConnection connection
= (HttpURLConnection
) url
.openConnection();
connection
.setRequestMethod("GET");
is
= connection
.getInputStream();
connection
.connect();
if (connection
.getResponseCode()==200){
int len
= 0;
byte[] b
= new byte[1024];
while((len
= is
.read(b
))!=-1){
sb
.append(new String(b
,0,len
));
}
}
return sb
.toString();
} catch (Exception e
) {
e
.printStackTrace();
}
return null
;
}
}
<?xml version
="1.0" encoding
="utf-8"?>
<LinearLayout xmlns
:android
="http://schemas.android.com/apk/res/android"
android
:orientation
="vertical" android
:layout_width
="match_parent"
android
:layout_height
="match_parent">
<TextView
android
:layout_width
="match_parent"
android
:layout_height
="50dp"
android
:id
="@+id/text_item"
android
:text
="菜名"
android
:gravity
="center"
/>
<ImageView
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:layout_gravity
="center"
android
:src
="@mipmap/ic_launcher"
android
:id
="@+id/image_item"
/>
<LinearLayout
android
:layout_width
="match_parent"
android
:layout_height
="40dp"
android
:gravity
="center"
android
:orientation
="horizontal">
<Button
android
:layout_width
="100dp"
android
:layout_height
="40dp"
android
:text
="-"
android
:id
="@+id/bt_itemj"
/>
<TextView
android
:layout_width
="100dp"
android
:layout_height
="40dp"
android
:text
="0"
android
:gravity
="center"
android
:id
="@+id/shu_item"
/>
<Button
android
:layout_width
="100dp"
android
:layout_height
="40dp"
android
:text
="+"
android
:id
="@+id/bt_itemjia"
/>
</LinearLayout
>
<RelativeLayout
android
:layout_width
="match_parent"
android
:layout_height
="40dp"
android
:orientation
="horizontal">
<Button
android
:layout_width
="100dp"
android
:layout_height
="40dp"
android
:text
="确定"
android
:id
="@+id/item_qd"
/>
<Button
android
:layout_width
="100dp"
android
:layout_height
="40dp"
android
:text
="取消"
android
:id
="@+id/item_qx"
android
:layout_alignParentRight
="true"
/>
</RelativeLayout
>
</LinearLayout
>
<?xml version
="1.0" encoding
="utf-8"?>
<LinearLayout xmlns
:android
="http://schemas.android.com/apk/res/android"
android
:orientation
="vertical" android
:layout_width
="match_parent"
android
:layout_height
="match_parent">
<TextView
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:text
="菜名"
android
:id
="@+id/itemduan_tv"
android
:layout_gravity
="center"/>
<ImageView
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:src
="@mipmap/ic_launcher"
android
:layout_gravity
="center"
android
:id
="@+id/itemduan_im"
/>
<RelativeLayout
android
:layout_width
="match_parent"
android
:orientation
="horizontal"
android
:layout_height
="50dp">
<Button
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:text
="确定"
android
:id
="@+id/itemduna_q"
/>
<Button
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:text
="取消"
android
:id
="@+id/itemduan_qx"
android
:layout_alignParentRight
="true"
/>
</RelativeLayout
>
</LinearLayout
>
<?xml version
="1.0" encoding
="utf-8"?>
<LinearLayout xmlns
:android
="http://schemas.android.com/apk/res/android"
android
:orientation
="vertical" android
:layout_width
="match_parent"
android
:layout_height
="match_parent">
<TextView
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:text
="0"
android
:id
="@+id/jsonitem_tv"
android
:layout_gravity
="center"
/>
<ImageView
android
:layout_width
="wrap_content"
android
:layout_height
="wrap_content"
android
:src
="@mipmap/ic_launcher"
android
:layout_gravity
="center"
android
:id
="@+id/jsonitem_im"
/>
</LinearLayout
>