本文主要是介绍黑名单号码页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.点击按钮能弹出对话框,并保存输入的号码
2.点击listview条目里的删除按钮,删除一个条目,自动更新UI
3.根据手指滚动listview到底端,自动加载数据
package com.example.mobilesafe;import java.util.ArrayList;
import java.util.List;
import java.util.Random;import javax.crypto.spec.IvParameterSpec;import com.example.mobilesafe.bean.blackNumberBean;
import com.example.mobilesafe.db.dao.blackNumberDao;import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class blackNumberActivity extends Activity {private int mode = 1;private List<blackNumberBean> al;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_black_number);initAdd();initListView();}/*** 展示黑名单号码*/private Handler mhHandler = new Handler() {private myAdapter myAdapter;public void handleMessage(android.os.Message msg) {if (myAdapter == null) {// 给listview设置适配器myAdapter = new myAdapter();lv_blackNumber_List.setAdapter(myAdapter);} else {// 更新listviewmyAdapter.notifyDataSetChanged();}};};private ListView lv_blackNumber_List;/*** 初始化适配器*/private void initListView() {lv_blackNumber_List = (ListView) findViewById(R.id.lv_blackNumber_List);initData();// 设置自动加载数据事件lv_blackNumber_List.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {if (scrollState == OnScrollListener.SCROLL_STATE_IDLE&& lv_blackNumber_List.getMaxScrollAmount() >= al.size() - 1) {blackNumberDao dao = blackNumberDao.getInstence(getApplicationContext());List<blackNumberBean> query = dao.query(al.size());al.addAll(query);mhHandler.sendEmptyMessage(0);}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// TODO Auto-generated method stub}});}// 适配器类private class myAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn al.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn al.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {// 优化适配器if (convertView == null) {// 得到控件convertView = View.inflate(getApplicationContext(),R.layout.blacknumber_item, null);//ViewHolder viewHolder = new ViewHolder();viewHolder.phone = (TextView) convertView.findViewById(R.id.tv_blackNumber_phone);viewHolder.mode = (TextView) convertView.findViewById(R.id.tv_blackNumber_mode);viewHolder.del = (ImageView) convertView.findViewById(R.id.iv_delBlackNumber);convertView.setTag(viewHolder);}ViewHolder viewHolder = (ViewHolder) convertView.getTag();final blackNumberBean bnb = al.get(position);viewHolder.phone.setText(bnb.phone);String mode = "短信";int intMode = Integer.parseInt(bnb.mode);switch (intMode) {case 1:mode = "短信";break;case 2:mode = "电话";break;case 3:mode = "全部";break;default:break;}viewHolder.mode.setText(mode);// 设置listview每个条目里面的点击事件viewHolder.del.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {blackNumberDao dao = blackNumberDao.getInstence(getApplicationContext());dao.delete(bnb.phone + "");al.remove(position);// 更新适配器,和显示数据mhHandler.sendEmptyMessage(0);}});return convertView;}}private class ViewHolder {public TextView phone;public TextView mode;public ImageView del;}/*** 初始化显示数据*/private void initData() {new Thread(new Runnable() {@Overridepublic void run() {// 第一次得到数据blackNumberDao numberDao = blackNumberDao.getInstence(getApplicationContext());al = numberDao.query(0);mhHandler.sendEmptyMessage(0);}}).start();}/*** 点击按钮,添加号码到黑名单*/private void initAdd() {Button bt_addBlackNumber = (Button) findViewById(R.id.bt_addBlackNumber);bt_addBlackNumber.setOnClickListener(new OnClickListener() {public void onClick(View v) {showDialog();}});}/*** 展示对话框*/protected void showDialog() {// 建立对话框Builder builder = new AlertDialog.Builder(this);final AlertDialog dialog = builder.create();View view = View.inflate(getApplicationContext(),R.layout.dialog_add_blacknumber, null);dialog.setView(view, 0, 0, 0, 0);dialog.show();final EditText et_put_blackNumber = (EditText) view.findViewById(R.id.et_put_blackNumber);Button bt_submit_yes = (Button) view.findViewById(R.id.bt_submit_yes);Button bt_submit_no = (Button) view.findViewById(R.id.bt_submit_no);final RadioGroup rg_mode = (RadioGroup) view.findViewById(R.id.rg_mode);rg_mode.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.rb_sms:mode = 1;Log.d("单选钮", 1 + "");break;case R.id.rb_phone:mode = 2;Log.d("单选钮", 2 + "");break;case R.id.rb_all:mode = 3;Log.d("单选钮", 3 + "");break;default:break;}}});// 设置对话框确定按钮,保存数据,并关闭对话框bt_submit_yes.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {final String phone = et_put_blackNumber.getText().toString();if (TextUtils.isEmpty(phone)) {Toast.makeText(getApplicationContext(), "请输入号码", 0).show();return;}blackNumberDao numberDao = blackNumberDao.getInstence(getApplicationContext());numberDao.insert(phone, mode + "");blackNumberDao dao = blackNumberDao.getInstence(getApplicationContext());al = dao.query(0);mhHandler.sendEmptyMessage(0);dialog.dismiss();}});// 设置取消按钮,关闭对话框bt_submit_no.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});}
}
这篇关于黑名单号码页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!