我正在开发一个Android应用程序,其中包含一个导航抽屉活动。这反过来包含两个选项卡“Alabanzas”和“Tema”,两者都可以从侧面菜单访问。 “Alabanzas”选项卡是一个片段,其中包含一个包含ListView的自定义ArrayAdapter。 在ActionBar中为SearchView中的“Alabanzas”中的搜索放置一个SearchView元素,但是我无法将搜索与ListView的元素链接起来 我的课程是以下......
使用导航抽屉的类MainActivity
onCreateOptionsMenu方法onChiteTextChange方法中的错误生成miadaptador.getFilter().Filter(s);
,因为该适配器与具有搜索方法的Adapter类中安装的适配器不同
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.MenuInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import com.example.misael.corolldm.Fragmentos.Alabanza;
import com.example.misael.corolldm.Fragmentos.Temas;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
CargarFragmento(new Alabanza());
navigationView.getMenu().getItem(0).setChecked(true);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search,menu);
MenuItem item = menu.findItem(R.id.menuSearch);
SearchView searchView= (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
// miadaptador.getFilter().filter(s);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
CargarFragmento(new Alabanza());
} else if (id == R.id.nav_gallery) {
CargarFragmento(new Temas());
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void CargarFragmento(Fragment fragmento){
FragmentManager Manager = getSupportFragmentManager();
Manager.beginTransaction().replace(R.id.contenedorFragmento, fragmento).commit();
}
}
Xml存档activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Class Adaptador
public class Adaptador extends BaseAdapter implements Filterable { 上下文上下文;
ArrayList<Datos> ListaObjetos;
ArrayList<Datos> mStringFilterList;
ValueFilter valueFilter;
public Adaptador(Context contexto, ArrayList<Datos> listaObjetos) {
this.contexto = contexto;
this.ListaObjetos = listaObjetos;
mStringFilterList=listaObjetos;
}
/*
Retorna cantidad de elementos de la vista
*/
@Override
public int getCount() {
return this.ListaObjetos.size();
}
/*
Retorna objeto puntual de la posicion que indico
*/
@Override
public Object getItem(int i) {
return this.ListaObjetos.get(i);
}
/*
Retorna Id del objeto puntual de la posicion que indico
*/
@Override
public long getItemId(int i) {
return this.ListaObjetos.get(i).getId();
}
/*
Se ejecuta en cada secuencia de cada hito de la listview
*/
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View vista = view;
//Inyectamos el layout definido dentro de la vista del ListView
LayoutInflater inflate = LayoutInflater.from(contexto);
vista = inflate.inflate(R.layout.itemlistview, null);
//Referenciamos el titulo
TextView titulo = (TextView) vista.findViewById(R.id.tw_titulo);
titulo.setText(ListaObjetos.get(i).getTitulo().toString());
return vista;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Datos> filterList = new ArrayList<Datos>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getTitulo().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Datos data = new Datos(mStringFilterList.get(i)
.getId(), mStringFilterList.get(i)
.getTitulo());
filterList.add(data);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
ListaObjetos = (ArrayList<Datos>) filterResults.values;
notifyDataSetChanged();
}
}
}
班级Alabanza
公共类Alabanza扩展了Fragment {
ListView listaDatos;
ArrayList<Datos> Lista= new ArrayList<>();
Adaptador miadaptador;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_alabanza, container, false);
listaDatos = (ListView)view.findViewById(R.id.lstDatos);
Lista.add(new Datos(1,"A la Diestra de Dios"));
Lista.add(new Datos(2,"A solas al Huerto"));
Lista.add(new Datos(3,"A ti Señor"));
Lista.add(new Datos(4,"A veces me pregunto"));
Lista.add(new Datos(5,"Al divino Salvador"));
Lista.add(new Datos(6,"Al Mundo Paz"));
Lista.add(new Datos(7,"Alabado el gran manantial"));
Lista.add(new Datos(8,"Alabanzas a Dios dad"));
Lista.add(new Datos(9,"Aleluya Amen"));
Lista.add(new Datos(10,"Aleluya oh Creador"));
Lista.add(new Datos(11,"Angeles cantando estan"));
Lista.add(new Datos(12,"Apocalipsis 14_6"));
Lista.add(new Datos(13,"Bendito Amor"));
Lista.add(new Datos(14,"Blancos Vestidos"));
Lista.add(new Datos(15,"Cantad a Jehova"));
Lista.add(new Datos(16,"Canten a Cristo Rey"));
Lista.add(new Datos(17,"Cerca de ti señor"));
Lista.add(new Datos(18,"Contigo estoy tan Feliz"));
Lista.add(new Datos(19,"Cuan Amables son tus Moradas"));
Lista.add(new Datos(20,"Cuando el Señor manifesto"));
Lista.add(new Datos(21,"Cuarenta Años de amor"));
Lista.add(new Datos(22,"Del Celeste Pais"));
Lista.add(new Datos(23,"Don Inefable"));
Lista.add(new Datos(24,"Dulce Oracion"));
Lista.add(new Datos(25,"El Amigo del Angel"));
Lista.add(new Datos(26,"El Apóstol de la Consolación"));
Lista.add(new Datos(27,"El Evangelio de Cristo"));
Lista.add(new Datos(28,"El Lema de la Iglesia"));
Lista.add(new Datos(29,"El Manto de la Eleccion"));
Lista.add(new Datos(30,"El mas bello amor"));
Lista.add(new Datos(31,"El Nuevo Israel"));
Lista.add(new Datos(32,"El Replicar del Campanal"));
Lista.add(new Datos(33,"El Resplandor de tu nacimiento"));
Lista.add(new Datos(33,"El vino a mi corazon"));
Lista.add(new Datos(34,"Elección de Dios"));
Lista.add(new Datos(35,"En la Judea"));
Lista.add(new Datos(36,"En las mañanas de bendicion"));
Lista.add(new Datos(37,"Engrandecido sea Dios"));
Lista.add(new Datos(38,"Eres el Angel"));
Lista.add(new Datos(39,"Estoy resuelto"));
miadaptador = new Adaptador(getActivity().getApplicationContext(),Lista);
listaDatos.setAdapter(miadaptador);
//Agregamos escuchador
listaDatos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Datos obj=(Datos) adapterView.getItemAtPosition(i);
//Creamos el paso hacia la nueva activity
Intent paso=new Intent(getActivity().getApplicationContext(), DetalleActivity.class);
paso.putExtra("objeto", (Serializable) obj); // Serializamos el traspaso del objeto
//Realiza el traspaso a la otra activity
startActivity(paso);
}
});
return view;
}
}
Xml Archive menu_search
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuSearch"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always"></item>
</menu>
如果有人可以帮我在片段“Alabanzas”的listView中执行搜索,任何想法都会很受欢迎 谢谢