使用数据库在EditText中搜索Listview

时间:2018-04-28 15:00:41

标签: android listview android-database

这是我第一次来这里。我搜索了很多但不理解以下内容:

我已将我的数据库连接到listview投影,但现在我想根据您在listview中输入的项目搜索或过滤EditText。我已在数据库文件夹中手动保存数据库。

所以这是我的 StudentProfile.java

public class StudentProfile {

private int studIDnum;
private String studFname;
private String studLname;
private String studMI;
private String courseYr;

//constructor
public StudentProfile(int studIDnum, String studFname, String studLname, String studMI, String courseYr) {
    this.studIDnum = studIDnum;
    this.studFname = studFname;
    this.studLname = studLname;
    this.studMI = studMI;
    this.courseYr = courseYr;
}

//setter and getter
public int getStudIDnum() {
    return studIDnum;
}

public void setStudIDnum(int studIDnum) {
    this.studIDnum = studIDnum;
}

public String getStudFname() {
    return studFname;
}

public void setStudFname(String studFname) {
    this.studFname = studFname;
}

public String getStudLname() {
    return studLname;
}

public void setStudLname(String studLname) {
    this.studLname = studLname;
}

public String getStudMI() {
    return studMI;
}

public void setStudMI(String studMI) {
    this.studMI = studMI;
}

public String getCourseYr() {
    return courseYr;
}

public void setCourseYr(String courseYr) {
    this.courseYr = courseYr;
}

} // class StudentProfile

我的StudentProfileAdapter.java

public class StudentProfileListAdapter extends BaseAdapter {


private Context mContext;
private List<StudentProfile> mStudentList;

public StudentProfileListAdapter(Context mContext, List<StudentProfile> mStudentList) {
    this.mContext = mContext;
    this.mStudentList = mStudentList;
}

@Override
public int getCount() {
    return mStudentList.size();
}

@Override
public Object getItem(int position) {
    return mStudentList.get(position);
}

@Override
public long getItemId(int position) {
    return mStudentList.get(position).getStudIDnum();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    View v = View.inflate(mContext, R.layout.item_listviewstudent, null);
    TextView tvStudID = (TextView) v.findViewById(R.id.StudID);
    TextView tvStudF = (TextView) v.findViewById(R.id.StudF);
    TextView tvStudMI = (TextView) v.findViewById(R.id.StudM);
    TextView tvStudL = (TextView) v.findViewById(R.id.StudL);
    TextView tvStudCY = (TextView) v.findViewById(R.id.StudCY);

tvStudID.setText(String.valueOf(mStudentList.get(position).getStudIDnum()));
    tvStudF.setText(mStudentList.get(position).getStudFname());
    tvStudMI.setText(mStudentList.get(position).getStudMI());
    tvStudL.setText(mStudentList.get(position).getStudLname());
    tvStudCY.setText(mStudentList.get(position).getCourseYr());


    return v;
}
} //  class StudentProfileAdapter

我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATBASE_NAME="StudentViolationDatabase.db";

private Context mContext;
private SQLiteDatabase mDatabase;

public DatabaseHelper(Context context) {
    super(context, DATBASE_NAME,null,1);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
   }

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

}


public void openDatabase(){
    String dbPath = mContext.getDatabasePath(DATBASE_NAME).getPath();
    if(mDatabase != null && mDatabase.isOpen()){
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath,null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase(){
    if(mDatabase!=null){
        mDatabase.close();
    }
}

public List<StudentProfile> getListStudent() {
    StudentProfile studentProfile = null;
    List<StudentProfile> studentProfileList = new ArrayList<>();
    openDatabase();
    Cursor cursor = mDatabase.rawQuery("SELECT StudIDnum , StudFname , StudLname , StudMI , StudCourseYr FROM StudentProfile" , null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        studentProfile = new StudentProfile(cursor.getInt(0), cursor.getString(1) , cursor.getString(2) , cursor.getString(3) , cursor.getString(4));
        studentProfileList.add(studentProfile);
        cursor.moveToNext();
    }

    cursor.close();
    closeDatabase();
    return studentProfileList;
}

} // class DatabaseHelper 

我的活动

public class Dialog2 extends AppCompatActivity {

private ListView lvStudent;
private StudentProfileListAdapter adapter;
private List<StudentProfile> mStudentList;
private DatabaseHelper mDBHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog2);


    Button btnConfirm = (Button)findViewById(R.id.btnConfirm);
    Button btnCancel = (Button)findViewById(R.id.btnCancel);
    Button btnClear = (Button)findViewById(R.id.btnClear);
    final EditText eSearch = (EditText)findViewById(R.id.EditSearch);

    lvStudent = (ListView)findViewById(R.id.listviewstudent);
    mDBHelper = new DatabaseHelper(this);

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog2.this.finish();
        }
    });

    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            eSearch.setText("");
        }
    });
}
}  // class Dialog2 

我的活动/对话图片: Sample Pic

提前致谢。

0 个答案:

没有答案