gpt4 book ai didi

java - 如何实现适配器并过滤结果?

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

我正在尝试开发一个应用程序,但我很困惑这个问题。我需要过滤一个列表(由图书馆组成),以显示它们是否在早上开放。

我为每个库都有一个类,其中有两个全局 boolean 变量,称为“isMattina”(isMorning)和“isPomeriggio”(“isAfternoon”)。

我遵循了互联网上的一些指南,现在我的代码如下所示:

package com.matteocunsolo.apposto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

import com.matteocunsolo.apposto.IOFromFile;

public class BiblioList extends Activity {
private Spinner spinner1;
public static Context context;
public int length;
public ArrayList<String> list;
private Button btnSubmit;
public boolean[] elencoOrari = TimeBooleanArray.create();
public Filter mioFiltro;
public StableArrayAdapter adapter;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
setContext();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
final ListView listview = (ListView) findViewById(R.id.listview);
String filePath = Environment.getExternalStorageDirectory() + "/ApPosto/";
//InputStream is = getResources().openRawResource(R.raw.elenco_titoli);
String[] values = IOFromFile.readTextFile(filePath + "elenco_Titoli.txt");
length = values.length;
list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
Toast.makeText(getApplicationContext(), Integer.toString(list.size()), Toast.LENGTH_SHORT).show();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
if(position == 0) {
Intent i = new Intent(BiblioList.this, InfoBiblioteca1.class);
startActivity(i);
}
else if(position == 1) {
Intent i = new Intent( BiblioList.this, InfoBiblioteca2.class);
startActivity(i);
}
else if(position == 4) {
Intent i = new Intent( BiblioList.this, InfoBiblioteca3.class);
startActivity(i);
}
else if(position == 5) {
Intent i = new Intent( BiblioList.this, InfoBiblioteca4.class);
startActivity(i);
}
}
});
}

public void setContext() {
context = getApplicationContext();
}

public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (Button) findViewById(R.id.button1);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(spinner1.getSelectedItem().toString() == null)
Toast.makeText(BiblioList.this, "adapter NULL", Toast.LENGTH_SHORT).show();
else
adapter.getFilter().filter(spinner1.getSelectedItem().toString());
}

});
}


private class StableArrayAdapter extends ArrayAdapter<String> implements Filterable {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}

@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}

@Override
public boolean hasStableIds() {
return true;
}
@Override
public Filter getFilter() {
if (mioFiltro == null)
mioFiltro = new MyFilter();
return mioFiltro;
}
}


public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.returntomap, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.change_layout){
Intent i = new Intent(BiblioList.this, LaunchActivity.class);
overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
startActivity(i);
}

return true;
}

private class MyFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = list;
results.count = list.size();
} else {
// We perform filtering operation
List<String> nlist = new ArrayList<String>();
if(constraint.equals("Mattina")) {
for(int i = 0; i < 4; i = i+2) {
if(elencoOrari[i])
nlist.add(list.get(i));
}
}
if(constraint.equals("Pomeriggio")) {
for(int i = 1; i < 4; i = i+2) {
if(elencoOrari[i])
nlist.add(list.get(i));
}
}

results.values = nlist;
results.count = nlist.size();
} return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0)
adapter.notifyDataSetInvalidated();
else {
list = (ArrayList<String>) results.values;
adapter.notifyDataSetChanged();
}
}
}

}

这是 TimeBooleanArray.java:

package com.matteocunsolo.apposto;

public class TimeBooleanArray {

public static boolean[] create() {
boolean[] elencoOrari = new boolean[4];
for(int i = 0; i<elencoOrari.length; i++) {
elencoOrari[i] = false;
}
if(InfoBiblioteca1.isMattina)
elencoOrari[0] = true;
if(InfoBiblioteca1.isPomeriggio)
elencoOrari[1] = true;
if(InfoBiblioteca2.isMattina)
elencoOrari[2] = true;
if(InfoBiblioteca2.isPomeriggio)
elencoOrari[3] = true;

return elencoOrari;
}
}

运行此应用程序并单击“Filtra”按钮,我没有从 LogCat 中收到任何错误,但什么也没有发生。该列表保持不变,具有相同的 6 个条目,而不是具有正确 boolean 值的 1 个条目。

比你们都提前!

最佳答案

您在 publishResults 中的代码实际上从未更改适配器存储的内容。适配器保留自己对列表的引用。您可以使用 clear()addAll() 更改列表的内容。

@Override  
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0)
adapter.notifyDataSetInvalidated();
else {
adapter.clear();
adapter.addAll(results.values);
adapter.notifyDataSetChanged();
}
}

关于java - 如何实现适配器并过滤结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957176/

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