想要實現點擊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()方法就可以了