初始化数据,开启异步任务,并在异步任务结束时,发送通知
private String path="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=3";
private Button downDate;
private Button sendDate;
private List<Bean.DataBean> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void startURL() {
new MyAsyncTask(this,list).execute(path);
}
private void initView() {
downDate = (Button) findViewById(R.id.down_date);
sendDate = (Button) findViewById(R.id.send_date);
downDate.setOnClickListener(v->{
startURL();
NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification.Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("json数据")
.setContentText("下载完成")
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX);
Intent intent=new Intent(this,MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(activity);
manager.notify(1,builder.build());
});
sendDate.setOnClickListener(v -> {
Intent intent=new Intent(this,DateActivity.class);
startActivity(intent);
});
}
在异步任务中下载数据,并通过下载来使进度条显示改变
private Context context;
public static List<Bean.DataBean> list;
private ProgressDialog progressDialog;
public static int value=0;
public MyAsyncTask(Context context, List<Bean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
protected List<Bean.DataBean> doInBackground(String... strings) {
StringBuilder stringBuilder=new StringBuilder();
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(3000);
urlConnection.setRequestProperty("Accept-Encoding","identity");
urlConnection.connect();
if(urlConnection.getResponseCode()==200){
long lenth=0;
long contentLength = urlConnection.getContentLengthLong();
InputStream inputStream = urlConnection.getInputStream();
int len=0;
byte[] bys=new byte[1024];
while((len=inputStream.read(bys))!=-1){
stringBuilder.append(new String(bys,0,len));
lenth+=len;
value= (int) ((lenth/(double)contentLength)*100);
publishProgress(value);
}
Log.i("---",stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
Gson gson=new Gson();
Bean bean = gson.fromJson(stringBuilder.toString(), Bean.class);
List<Bean.DataBean> data = bean.getData();
return data;
}
@Override
protected void onPreExecute() {
progressDialog=new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(List<Bean.DataBean> dataBeans) {
progressDialog.dismiss();
list.addAll(dataBeans);
super.onPostExecute(dataBeans);
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
super.onProgressUpdate(values);
}
配置视图,为listView添加数据
private Context context;
private List<Bean.DataBean> list;
private LayoutInflater layoutInflater;
public MyBaseAdapter(Context context) {
this.context = context;
this.list = MyAsyncTask.list;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.item_one,null);
holder.img=convertView.findViewById(R.id.img);
holder.title=convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(list.get(position).getTitle());
RequestOptions requestOptions = new RequestOptions();
requestOptions.circleCrop();
Glide.with(context).load(list.get(position).getPic()).apply(requestOptions).into(holder.img);
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
View inflate = LayoutInflater.from(context).inflate(R.layout.dialog, null);
ImageView img=inflate.findViewById(R.id.img_show);
Glide.with(context).load(list.get(position).getPic()).into(img);
builder.setView(inflate);
builder.show();
}
});
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, list.get(position).getTitle()+"", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
static class ViewHolder{
public static ImageView img;
public static TextView title;
}
设置监听,单击头像,名字,长按item效果
private ListView listView;
private Button delete;
private MyBaseAdapter myBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
listView = (ListView) findViewById(R.id.list_view);
myBaseAdapter = new MyBaseAdapter(DateActivity.this);
listView.setAdapter(myBaseAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
PopupWindow popupWindow=new PopupWindow(DateActivity.this);
View inflate = LayoutInflater.from(DateActivity.this).inflate(R.layout.pop, null);
popupWindow.setContentView(inflate);
View inflate1 = LayoutInflater.from(DateActivity.this).inflate(R.layout.activity_date, null);
popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);
delete = inflate. findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(DateActivity.this);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyAsyncTask.list.remove(position);
myBaseAdapter.notifyDataSetChanged();
popupWindow.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
popupWindow.dismiss();
}
});
builder.show();
}
});
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.black:
listView.setBackgroundColor(getResources().getColor(R.color.black));
MyBaseAdapter.ViewHolder.title.setTextColor(getResources().getColor(R.color.white));
myBaseAdapter.notifyDataSetChanged();
break;
case R.id.defau:
listView.setBackgroundColor(getResources().getColor(R.color.white));
MyBaseAdapter.ViewHolder.title.setTextColor(getResources().getColor(R.color.black));
myBaseAdapter.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}