gpt4 book ai didi

java - 与 ArrayList 结合使用时可搜索的 ListView

转载 作者:行者123 更新时间:2023-12-02 02:19:43 25 4
gpt4 key购买 nike

我正在尝试创建一个可搜索的 ListView 。最初,硬编码数组工作得很好。但是当我使用 ArrayList 并用 json 填充它时,它现在不显示任何搜索结果。现在可以正确加载列表,但搜索不起作用。这是我的代码。

public class MainActivity extends AppCompatActivity {

public ProgressDialog pd;
Toolbar mToolbar;
ArrayAdapter mAdapter;
ListView mListView;
TextView mEmptyView;
PatientListAdapter adapter = null;
String clinic_id, response;
public BufferedReader reader = null;
public ArrayList<PatientListDetails> patientListDetailsList;
private String url = "http://192.168.2.4/projectFolder1/FetchData/getDataList.php?";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("View Project List");

pd = new ProgressDialog(this);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setTitle("Niramaya");
pd.setMessage("Fetching patient list. Please wait....");


mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

mListView = (ListView) findViewById(R.id.list);
mEmptyView = (TextView) findViewById(R.id.emptyView);

new GetPatientListTask().execute(url+"cid=1");


// mAdapter = new ArrayAdapter(MainActivity.this,
// android.R.layout.simple_list_item_1,
// getResources().getStringArray(R.array.months_array));
// mListView.setAdapter(mAdapter);
//
// mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Toast.makeText(MainActivity.this, adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_SHORT).show();
// }

// });

//

// mListView.setEmptyView(mEmptyView);
}

private class GetPatientListTask extends AsyncTask<String,String,List<PatientListDetails>> {

List<PatientListDetails> patientDetailsList = new ArrayList<>();
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}

@Override
protected List<PatientListDetails> doInBackground(String... strings) {
HttpURLConnection connection;

StringBuffer buffer = null;
JSONObject jsonObject;
JSONArray jsonArray;

try{

URL url = new URL(strings[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();

reader = new BufferedReader(new
InputStreamReader(stream));

buffer = new StringBuffer();
String line = "";

while((line=reader.readLine())!=null){
buffer.append(line);
}


jsonArray = new JSONArray(buffer.toString());


for(int i=0; i<jsonArray.length(); i++){
jsonObject = jsonArray.getJSONObject(i);
response = jsonObject.getString("status");

if(response.equals("0")){
PatientListDetails patientListDetails = new PatientListDetails();


patientListDetails.setStatus(jsonObject.getString("status"));

patientListDetails.setPatient_id(jsonObject.getString("patient_id"));

patientListDetails.setFull_name(jsonObject.getString("full_name"));

patientListDetails.setGender(jsonObject.getString("gender"));


//Log.e("name",jsonObject.getString("full_name"));
patientDetailsList.add(patientListDetails);
}

}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return patientDetailsList;

}

@Override
protected void onPostExecute(List<PatientListDetails> s) {
super.onPostExecute(s);
pd.dismiss();
adapter = new
PatientListAdapter(getApplicationContext(),R.layout.patient_list,s);
mListView.setAdapter(adapter);
}


}

class MyViewHolder{
TextView tvFull_name,tvGender,btn;
EditText pid;
MyViewHolder(View v){
tvFull_name = (TextView)v.findViewById(R.id.patient_name);
tvGender = (TextView)v.findViewById(R.id.patient_gender);
pid = (EditText) v.findViewById(R.id.patient_id);
btn = (TextView) v.findViewById(R.id.btn_view);

}
}

public class PatientListAdapter extends ArrayAdapter implements
Filterable{


int resource;
private LayoutInflater inflater;

public PatientListAdapter(@NonNull Context context, int resource,
@NonNull List<PatientListDetails> objects) {
super(context, resource, objects);
patientListDetailsList= (ArrayList<PatientListDetails>)
objects;
this.resource=resource;
inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
}

@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

return null;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
try {
patientListDetailsList = (ArrayList<PatientListDetails>) results.values;
}catch (NullPointerException e){
Log.e("Error",e.toString());
}
notifyDataSetChanged();

}
};
}

@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("InflateParams")
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//return super.getView(position, convertView, parent);


MyViewHolder holder = null;
if(convertView==null){
convertView = inflater.inflate(R.layout.patient_list, null);
holder = new MyViewHolder(convertView);
convertView.setTag(holder);
}


if (holder != null) {


holder.tvFull_name.setText(patientListDetailsList.get(position).getFull_name());

}

// if (holder != null) {

// holder.tvGender.setText(patientListDetailsList.get(position).getGender());

// }

// final String pat_id = patientListDetailsList.get(position).getPatient_id();




// if (holder != null) {

// holder.btn.setOnClickListener(new View.OnClickListener() {

// @Override

// public void onClick(View v) {



// }

// });

// }

return convertView;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbarmenu, menu);

MenuItem mSearch = menu.findItem(R.id.action_search);

SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setQueryHint("Search");

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {

adapter.getFilter().filter(newText);
return true;
}
});

return super.onCreateOptionsMenu(menu);
}


}

我在堆栈溢出流上尝试了许多其他选项,但没有任何效果。所以我不得不问这个问题。

最佳答案

如果您正在寻找基于文本匹配的过滤器,您可以使用以下代码 -

@Override
protected FilterResults performFiltering(CharSequence constraint) {

String filterString = constraint.toString().toLowerCase();

FilterResults results = new FilterResults();

final List<String> list = originalData;

int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);

String filterableString ;

for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}

results.values = nlist;
results.count = nlist.size();

return results;
}

关于java - 与 ArrayList 结合使用时可搜索的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com