想要实现点击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;
}
}
点击不同的按钮,调用不同的方法就行了;点击button姓名,出现No和姓名,这个按钮调用findYingwen()方法;点击学号,出现No和学号调用findFanyi()方法就可以了
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。