通知,监听

初始化数据,开启异步任务,并在异步任务结束时,发送通知

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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值