开发者社区> 问答> 正文

点击button实现数据库查询的不同结果,就像有道词典上面,显示释义,隐藏释义

想要实现点击button姓名,屏幕NO+序号+姓名,点击学号,屏幕显示NO+序号+学号,实在是不会,请大神帮忙,本人菜鸟,代码都是粘来的,还有啊,我想直接访问在asset下面的数据库,不想有新建,添加数据这个过程,求大神指教
界面:
screenshot

MainActivity
 package com.example.shujuku1;

import java.util.List;

import com.example.shujuku1.Dao.Person;
import com.example.shujuku1.Dao.PersonDao;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {


    private ListView lv;
    private List<Person> persons;
    private Button btn1,btn2;
    private PersonDao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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

        dao = new PersonDao(this);

        persons = dao.findAll();

        if(persons.size()==0){

             dao.add("花花","123456");
             dao.add("花花","123456");
             dao.add("花花","123456");
             dao.add("花花","123456");
             dao.add("花花","123456");
             dao.add("花花","123456");
             dao.add("花花","123456");

                    lv.setAdapter(new MyListAdapter());

       }else if(persons.size()!=0){
           lv.setAdapter(new MyListAdapter());

       }



}

Person类
 package com.example.shujuku1.Dao;

public class Person {
    int id;
    String name;
    String number;

    public Person() {

    }


    public Person(int id, String name, String number) {
        super();
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return " NO." + id + "   " + name + ", " + number;
    }


}

PersonDao
 package com.example.shujuku1.Dao;

import java.util.ArrayList;
import java.util.List;

import com.example.shujuku1.PersonSQLiteOpenHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.GpsStatus.Listener;

public class PersonDao {
    private PersonSQLiteOpenHelper helper;

    public PersonDao(Context context){
        helper = new PersonSQLiteOpenHelper(context);
    }

    public long add(String name, String number){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("number", number);
        long id = db.insert("person", null, values);

        db.close();
        return id;
    }


    public List<Person> findAll(){
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Person> persons = new ArrayList<Person>();
        Cursor cursor = db.query("person", new String[]{"id","name","number"}, null, null, null, null, null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String number = cursor.getString(cursor.getColumnIndex("number"));
            Person p = new Person(id, name, number);

            persons.add(p);
        }
        cursor.close();
        db.close();

        return persons;
    }

    public List<Person> findYingwen(){
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Person> persons = new ArrayList<Person>();
        Cursor cursor = db.query("person", new String[]{"id","name"}, null, null, null, null, null);

        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String number = null;
            Person p = new Person(id, name, number );
            System.out.println(cursor.toString());

            persons.add(p);

        }
        cursor.close();
        db.close();

        return persons;
    }

    public List<Person> findFanyi(){
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Person> persons = new ArrayList<Person>();
        Cursor cursor = db.query("person", new String[]{"id","number"}, null, null, null, null, null);
        //Cursor cursor = db.execSQL("select id,name from person");

        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String number = cursor.getString(cursor.getColumnIndex("number"));
            String name = null;
            Person p = new Person(id, name ,number);

            persons.add(p);
        }
        cursor.close();
        db.close();

        return persons;
    }

}

展开
收起
a123456678 2016-07-18 15:54:15 3331 0
1 条回答
写回答
取消 提交回答
  • 点击不同的按钮,调用不同的方法就行了;点击button姓名,出现No和姓名,这个按钮调用findYingwen()方法;点击学号,出现No和学号调用findFanyi()方法就可以了

    2019-07-17 19:58:00
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载