我正在尝试在自定义列表视图中实现搜索功能。我使用菜单进行搜索。当我在搜索栏中键入内容时,我的项目因该错误而崩溃。
2019-11-19 18:50:05.762 17072-17072 /? E / AndroidRuntime:致命 例外:主要 流程:com.example.medicationmanagementmanagementsystem,PID:17072 java.lang.NullPointerException:尝试在空对象上调用虚拟方法“ java.util.Iterator java.util.ArrayList.iterator()” 参考 在com.example.medicationmanagementsystem.ViewListContents $ 1.onQueryTextChange(ViewListContents.java:72)
我认为该数组的值为null,因此无法显示任何详细信息。任何帮助将不胜感激。
ViewListContents.java
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;
import com.example.medicationmanagementsystem.DAO.DatabaseHelper;
import java.util.ArrayList;
public class ViewListContents extends AppCompatActivity {
DatabaseHelper myDB;
ArrayList<Prescription> prescriptionList;
ListView listView;
Prescription prescription;
ArrayList<String> listItem;
ArrayAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontents_layout);
myDB = new DatabaseHelper(this);
prescriptionList = new ArrayList<Prescription>();
Cursor data = myDB.getListContents();
int numRows = data.getCount();
if(numRows == 0){
Toast.makeText(ViewListContents.this, "There is nothing in this database", Toast.LENGTH_LONG).show();
}
else {
while(data.moveToNext()){
prescription = new Prescription(data.getString(1), data.getString(2), data.getString(3),
data.getString(4), data.getString(5), data.getString(6), data.getString(7),
data.getString(8), data.getString(9));
prescriptionList.add(prescription);
}
EightColumn_ListAdapter adapter = new EightColumn_ListAdapter(this,R.layout.prescription_view,prescriptionList);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
}
//END
//This code is based on SQLite Database to ListView- Part 4:Search Items- Android Studio Tutorial, KOD Dev, https://www.youtube.com/watch?v=QY-O49a_Ags
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
ArrayList<String> listViews = new ArrayList<>();
for (String prescription : listItem){
if (prescription.toLowerCase().contains(newText.toLowerCase())){
listViews.add(prescription);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ViewListContents.this,R.layout.prescription_view,listViews);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
}
DatabaseHelper.java
package com.example.medicationmanagementsystem.DAO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
//Create Database
public static final String DATABASE_NAME = "ManagementSystem.db";
//Create patient table
public static final String TABLE_PATIENT = "patient_table";
public static final String COL_PATIENT_PATIENTID = "PATIENTID";
public static final String COL_PATIENT_FNAME = "FNAME";
public static final String COL_PATIENT_SNAME = "SNAME";
public static final String COL_PATIENT_PPS = "PPS";
public static final String COL_PATIENT_DOB = "DOB";
public static final String COL_PATIENT_ADDRESS = "ADDRESS";
public static final String COL_PATIENT_TYPE = "PATIENTTYPE";
public static final String COL_PATIENT_MEDCOND = "PATIENTMEDCON";
public static final String COL_PATIENT_CARINGID = "CARINGID";
//Create prescription table
public static final String TABLE_PRESCRIPTION = "prescription_table";
public static final String COL_PRESCRIPTION_ID = "PRESCRIPTIONID";
public static final String COL_PRESCRIPTION__PATIENTID = COL_PATIENT_PATIENTID;
public static final String COL_PRESCRIPTION__DATE = "DATE";
public static final String COL_PRESCRIPTION__DRUGNAME = "DRUGNAME";
public static final String COL_PRESCRIPTION__CONCENTRATION = "CONCENTRATION";
public static final String COL_PRESCRIPTION__DOSAGE = "DOSAGE";
public static final String COL_PRESCRIPTION__PREPARATION = "PREPARATION";
public static final String COL_PRESCRIPTION__STARTDATE = "STARTDATE";
public static final String COL_PRESCRIPTION__ENDDATE = "ENDDATE";
public static final String COL_PRESCRIPTION__DOCTORID = "DOCTORID";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate (SQLiteDatabase db) {
String patienttable = "CREATE TABLE " + TABLE_PATIENT + "(PATIENTID INTEGER PRIMARY KEY AUTOINCREMENT, FNAME TEXT, SNAME TEXT, PPS TEXT, DOB TEXT, ADDRESS TEXT, PATIENTTYPE TEXT, PATIENTMEDCON TEXT, CARINGID INTEGER)";
String prescriptiontable = "CREATE TABLE " + TABLE_PRESCRIPTION + "(PRESCRIPTIONID INTEGER PRIMARY KEY AUTOINCREMENT, PATIENTID INTEGER, DATE TEXT, DRUGNAME TEXT, CONCENTRATION TEXT, DOSAGE TEXT, PREPARATION TEXT, STARTDATE TEXT, ENDDATE TEXT, DOCTORID INTEGER)";
db.execSQL(patienttable);
db.execSQL(prescriptiontable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PATIENT);
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PRESCRIPTION);
onCreate(db);
}
//insert patient data
public boolean insertPatientData(String fname, String sname, String pps, String dob, String address, String patienttype, String patientmedcon, String caringid) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues1 = new ContentValues();
contentValues1.put(COL_PATIENT_FNAME, fname);
contentValues1.put(COL_PATIENT_SNAME, sname);
contentValues1.put(COL_PATIENT_PPS, pps);
contentValues1.put(COL_PATIENT_DOB, dob);
contentValues1.put(COL_PATIENT_ADDRESS, address);
contentValues1.put(COL_PATIENT_TYPE, patienttype);
contentValues1.put(COL_PATIENT_MEDCOND,patientmedcon);
contentValues1.put(COL_PATIENT_CARINGID, caringid);
long result= db.insert(TABLE_PATIENT,null, contentValues1);
if (result == 1)
return false;
else
return true;
}
//insert prescription data
public boolean insertData(String patientid, String date, String drugname, String concentration,String dosage, String preparation, String startdate, String enddate, String doctorid) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues2 = new ContentValues();
contentValues2.put(COL_PRESCRIPTION__PATIENTID, patientid);
contentValues2.put(COL_PRESCRIPTION__DATE, date);
contentValues2.put(COL_PRESCRIPTION__DRUGNAME, drugname);
contentValues2.put(COL_PRESCRIPTION__CONCENTRATION, concentration);
contentValues2.put(COL_PRESCRIPTION__DOSAGE, dosage);
contentValues2.put(COL_PRESCRIPTION__PREPARATION, preparation);
contentValues2.put(COL_PRESCRIPTION__STARTDATE, startdate);
contentValues2.put(COL_PRESCRIPTION__ENDDATE, enddate);
contentValues2.put(COL_PRESCRIPTION__DOCTORID, doctorid);
long result= db.insert(TABLE_PRESCRIPTION,null, contentValues2);
if (result == 1)
return false;
else
return true;
//END
}
//Coding with mitch tutorial
public Cursor getListContents() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_PRESCRIPTION, null);
return data;
}
////This code is based on SQLite Database to ListView- Part 4:Search Items- Android Studio Tutorial, KOD Dev, https://www.youtube.com/watch?v=QY-O49a_Ags
public Cursor searchPrescriptions(String text) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + TABLE_PRESCRIPTION + "WHERE " + COL_PRESCRIPTION__PATIENTID + " Like '%" + text +"%'";
Cursor data = db.rawQuery(query, null);
return data;
}
}
Eight_Column_ListAdapter.java
package com.example.medicationmanagementsystem;
//The code below is based on Adding multiple columns to your ListView, CodingWithMitch, https://www.youtube.com/watch?v=8K-6gdTlGEA, https://www.youtube.com/watch?v=hHQqFGpod14, https://www.youtube.com/watch?v=jpt3Md9aDIQ
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class EightColumn_ListAdapter extends ArrayAdapter<Prescription> {
private LayoutInflater mInflater;
private ArrayList<Prescription> prescriptions;
private int mViewResourceId;
public EightColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<Prescription> prescriptions) {
super(context, textViewResourceId, prescriptions);
this.prescriptions = prescriptions;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents) {
convertView = mInflater.inflate(mViewResourceId, null);
Prescription prescription = prescriptions.get(position);
if (prescription != null) {
TextView patientID = (TextView) convertView.findViewById(R.id.textpatientID);
TextView date = (TextView) convertView.findViewById(R.id.textdate);
TextView drugname = (TextView) convertView.findViewById(R.id.textdrugname);
TextView concentration = (TextView) convertView.findViewById(R.id.textcontentration);
TextView dosage = (TextView) convertView.findViewById(R.id.textdosage);
TextView prep = (TextView) convertView.findViewById(R.id.textprep);
TextView startdate = (TextView) convertView.findViewById(R.id.textstartdate);
TextView enddate = (TextView) convertView.findViewById(R.id.textenddate);
TextView doctorID = (TextView) convertView.findViewById(R.id.textdoctorid);
if(patientID != null) {
patientID.setText((prescription.getPatientID()));
}
if(date != null) {
date.setText((prescription.getPresdate()));
}
if(drugname != null) {
drugname.setText((prescription.getDrugname()));
}
if(concentration != null) {
concentration.setText((prescription.getConcentration()));
}
if(dosage != null) {
dosage.setText((prescription.getDosage()));
}
if(prep != null) {
prep.setText((prescription.getPreparation()));
}
if(startdate != null) {
startdate.setText((prescription.getStartdate()));
}
if(enddate != null) {
enddate.setText((prescription.getEnddate()));
}
if(doctorID != null) {
patientID.setText((prescription.getDoctorID()));
}
}
return convertView;
}
}
答案 0 :(得分:0)
您正在尝试阅读名为listItem
的列表元素。该列表为空,因为您从未将其设置为其他任何内容。
更改此:
ArrayList<String> listItem;
对此:
ArrayList<String> listItem = new ArrayList<String>();
...或将其放入onCreate()
方法中,就像使用prescriptionList
一样:
prescriptionList = new ArrayList<Prescription>();
listItem = new ArrayList<String>();
另外,不要命名您的列表listItem
,因为它不是项目,而是列表!