码迷,mamicode.com
首页 > 其他好文 > 详细

黑名单的练习

时间:2016-04-15 00:40:49      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

package com.example.dell.wodelianxi;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class heimingdan extends AppCompatActivity {

    TextView tname ;
    TextView tnumber;
    List<Map<String, Object>> lm;


    class MyHelper extends SQLiteOpenHelper {
        public MyHelper(String name, int version) {
            super(heimingdan.this, name, null, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String sql = "CREATE TABLE user(_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,name varchar,number varchar)";
            db.execSQL(sql);

            ContentValues cv = new ContentValues();

            cv.put("name", "推销");
            cv.put("number", "95510");

            db.insert("user", null, cv);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heimingdan);


        ListView lv_hei = (ListView) findViewById(R.id.lv_hei);


        MyHelper mh = new MyHelper("heimingdan.db", 1);

        SQLiteDatabase sd = mh.getWritableDatabase();

        Cursor cursor = sd.query("user", null, null, null, null, null, null);


        lm = new ArrayList<Map<String, Object>>();

        while (cursor.moveToNext()) {
            Map<String, Object> map = new HashMap<String, Object>();
            String name = cursor.getString(cursor.getColumnIndex("name"));

            int number = cursor.getInt(cursor.getColumnIndex("number"));

            Log.e("tag", name + number);

            map.put("name", name);
            map.put("number", number);

            lm.add(map);
        }
        cursor.close();
        sd.close();

        String[] key = {"name", "number"};

        int[] viewid = {R.id.name, R.id.number};



           final SimpleAdapter sa = new SimpleAdapter(this, lm, R.layout.heimingdan, key, viewid);

            lv_hei.setAdapter(sa);

        lv_hei.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                tname = (TextView) heimingdan.this.findViewById(R.id.name);
                tnumber = (TextView) heimingdan.this.findViewById(R.id.number);

//                String ming = tname.getText().toString();
//
//                String haoma = tnumber.getText().toString();

                LayoutInflater lif = getLayoutInflater();

                final View tan = lif.inflate(R.layout.tanchuanglayout, null);



                final EditText et_name = (EditText) tan.findViewById(R.id.et_name);

                final EditText et_number = (EditText) tan.findViewById(R.id.et_number);

                final String gaimingqian = tname.getText().toString();

                final String gaihaoqian = tnumber.getText().toString();

                et_name.setText(lm.get(position).get("name").toString());

                et_number.setText(lm.get(position).get("number").toString());
                new AlertDialog.Builder(heimingdan.this)
                        .setView(tan)
                        .setNeutralButton("修改", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                String gaiminghou = et_name.getText().toString();
                                String gaihaohou = et_number.getText().toString();
//                                    lm.get(position).put("name",gaiminghou);
//                                    lm.get(position).put("number",gaihaohou);

                                
                                tnumber.setText(gaihaohou);


                                Log.e("tag", gaimingqian + gaiminghou + gaihaoqian + gaihaohou);

                                MyHelper mh = new MyHelper("heimingdan.db", 1);

                                SQLiteDatabase sd = mh.getWritableDatabase();

                                ContentValues cv = new ContentValues();
                                cv.put("name", gaiminghou);
                                cv.put("number", gaihaohou);

                                sd.update("user", cv, "name=? and number = ?", new String[]{gaimingqian, gaihaoqian});

                                // Log.e("tag", "count" + count);
                                sd.close();

                                Log.e("tag", gaimingqian + gaiminghou + gaihaoqian + gaihaohou);


                            }
                        })
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                MyHelper mh = new MyHelper("heimingdan.db", 1);

                                SQLiteDatabase sd = mh.getWritableDatabase();

                                ContentValues cv = new ContentValues();
                                cv.put("name", gaimingqian);
                                cv.put("number", gaihaoqian);

                                sd.delete("user", "name=? and number=?", new String[]{gaimingqian, gaihaoqian});

                                sd.close();


                            }
                        })
                        .show();


                return false;
            }
        });

    }
    public void tianjia(View view)
    {
        LayoutInflater lif = getLayoutInflater();

        final View tan = lif.inflate(R.layout.tanchuanglayout, null);
        new AlertDialog.Builder(heimingdan.this)
                .setView(tan)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final EditText et_name = (EditText) tan.findViewById(R.id.et_name);

                        final EditText et_number = (EditText) tan.findViewById(R.id.et_number);

                        String tianming = et_name.getText().toString();

                        String tianhao = et_number.getText().toString();

                        MyHelper mh = new MyHelper("heimingdan.db", 1);

                        SQLiteDatabase sd = mh.getWritableDatabase();

                        ContentValues cv = new ContentValues();

                        cv.put("name",tianming);
                        cv.put("number",tianhao);

                        sd.insert("user", null, cv);

                        sd.close();
                    }
                })
                .show();


    }
   }

 

黑名单的练习

标签:

原文地址:http://www.cnblogs.com/youshashuosha/p/5393626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!