Java 我试图在自定义列表视图中实现搜索功能。我正在为搜索功能使用菜单
我试图在自定义列表视图中实现搜索功能。我正在使用搜索功能的菜单。当我在搜索栏中键入时,我的项目因此错误而崩溃 2019-11-19 18:50:05.762 17072-17072/?E/AndroidRuntime:致命 例外情况:主要 流程:com.example.medicationmanagementsystem,PID:17072 java.lang.NullPointerException:尝试对空对象调用虚拟方法“java.util.Iterator java.util.ArrayList.Iterator()” 参考 在com.example.MedicalationManagementSystem.ViewListContents$1.onQueryTextChange(ViewListContents.java:72)上 我认为该数组正在变为null,因此无法显示任何详细信息。任何帮助都将不胜感激 ViewListContents.javaJava 我试图在自定义列表视图中实现搜索功能。我正在为搜索功能使用菜单,java,android,listview,arraylist,Java,Android,Listview,Arraylist,我试图在自定义列表视图中实现搜索功能。我正在使用搜索功能的菜单。当我在搜索栏中键入时,我的项目因此错误而崩溃 2019-11-19 18:50:05.762 17072-17072/?E/AndroidRuntime:致命 例外情况:主要 流程:com.example.medicationmanagementsystem,PID:17072 java.lang.NullPointerException:尝试对空对象调用虚拟方法“java.util.Iterator java.util.Array
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);
}
}
八列列表适配器.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;
}
}
包com.example.medicationmanagementsystem;
//下面的代码基于向ListView添加多个列,使用Mitch编码,https://www.youtube.com/watch?v=8K-6gdTlGEA,https://www.youtube.com/watch?v=hHQqFGpod14, https://www.youtube.com/watch?v=jpt3Md9aDIQ
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类EightColumn_ListAdapter扩展了ArrayAdapter{
私人停车场;
私人ArrayList处方;
私有int-mViewResourceId;
公共EightColumn_ListAdapter(上下文上下文、int-textViewResourceId、ArrayList说明){
super(上下文、textViewResourceId、处方);
这个。处方=处方;
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
mViewResourceId=textViewResourceId;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
convertView=mInflater.inflate(mViewResourceId,null);
处方=处方。获取(位置);
如果(处方!=null){
TextView patientID=(TextView)convertView.findViewById(R.id.textpatientID);
TextView日期=(TextView)convertView.findViewById(R.id.textdate);
TextView drugname=(TextView)convertView.findViewById(R.id.textdrugname);
TextView浓度=(TextView)convertView.findViewById(R.id.textcontentration);
TextView剂量=(TextView)convertView.findViewById(R.id.textPosition);
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());
}
如果(日期!=null){
date.setText((prescription.getPresdate());
}
if(drugname!=null){
drugname.setText((prescription.getDrugname());
}
如果(浓度!=null){
concentration.setText((prescription.getConcentration());
}
如果(剂量!=null){
剂量.setText((处方.getdo剂());
}
如果(prep!=null){
prep.setText((prescription.getPreparation());
}
如果(起始日期!=null){
startdate.setText((prescription.getStartdate());
}
如果(enddate!=null){
setText((prescription.getEnddate());
}
if(doctorID!=null){
patientID.setText((prescription.getDoctorID());
}
}
返回视图;
}
}
您正在尝试读取名为列表项的列表元素。该列表为空,因为您从未将其设置为任何其他值
更改此项:
ArrayList列表项代码>
为此:
ArrayList listItem=new ArrayList()代码>
。。。或者,您可以将其放入onCreate()
方法中,就像使用处方列表一样:
prescriptionList = new ArrayList<Prescription>();
listItem = new ArrayList<String>();
prescriptionList=newarraylist();
listItem=新的ArrayList();
另外,不要给列表命名listItem
,因为它不是一个项目。。。这是一张单子 谢谢,它现在没有崩溃,但是没有返回任何数据。我很高兴我能提供帮助。请将答案标记为已接受。祝你解决问题的练习好运。如果你自己无法解决这个问题,请稍后再来问另一个问题。
prescriptionList = new ArrayList<Prescription>();
listItem = new ArrayList<String>();