2 mengmengyu mengmengyu_ 于 2015.06.18 17:21 提问

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

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

}

1个回答

strutce
strutce   Ds   Rxr 2015.06.18 17:33
已采纳

点击不同的按钮,调用不同的方法就行了;点击button姓名,出现No和姓名,这个按钮调用findYingwen()方法;点击学号,出现No和学号调用findFanyi()方法就可以了

mengmengyu_
mengmengyu_ findYingwen(),findFanyi()这两个方法是我写的,不能真正实现功能,怎么只从数据库中获得某一列呀,实在是不会,而且,程序第一次部署到模拟器信息不能显示到listview里面,第二次部署才可以
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片