In the Listview without using the filter when it clicks it shows the correct data in the other activity, but when it filters an item it changes the position and passes another ID the data has nothing to do with the item that passes to the other activity
ADAPTER code:
public class AdapterListaDebitosPendentes extends BaseAdapter implements Filterable{
private List<DebitosPendentes> debitosPendentesList;
private List<DebitosPendentes> debitosPendentesFiltrados;
private LayoutInflater layoutInflater;
private Activity context;
private ValueFilter valueFilter;
public AdapterListaDebitosPendentes(List<DebitosPendentes> debitosPendentesList, Activity context) {
this.debitosPendentesList=debitosPendentesList;
this.debitosPendentesFiltrados=debitosPendentesList;
this.context = context;
this.layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getFilter();
}
@Override
public int getCount() {
return debitosPendentesList.size();
}
@Override
public Object getItem(int position) {
return debitosPendentesFiltrados.get(position);
}
@Override
public long getItemId(int position) {
return debitosPendentesFiltrados.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DebitosPendentes debitosPendentes = debitosPendentesList.get(position);
convertView=layoutInflater.inflate(R.layout.lista_debitos_pendentes,null);
convertView=layoutInflater.inflate(R.layout.lista_debitos_pendentes,null);
TextView txtVencimentos = (TextView) convertView.findViewById(R.id.txtVencimento);
TextView txtValores = (TextView) convertView.findViewById(R.id.txtValores);
String[] valoresExploded = debitosPendentes.getVencimento().split("-");
txtVencimentos.setText("Vencimento: "+ valoresExploded[2]+"/"+valoresExploded[1]+"/"+valoresExploded[0]);
txtValores.setText("Valor: "+debitosPendentes.getValor()+ " Valor atualizado: " +debitosPendentes.getValorAtualizado());
return convertView;
}
@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){
List<DebitosPendentes> filterDebitos = new ArrayList<DebitosPendentes>();
for (int i=0; i<debitosPendentesFiltrados.size();i++){
if((debitosPendentesFiltrados.get(i).getVencimento().toUpperCase())
.contains(constraint.toString().toUpperCase())){
DebitosPendentes debitosPendentes = new DebitosPendentes();
debitosPendentes= debitosPendentesFiltrados.get(i);
filterDebitos.add(debitosPendentes);
}
if((debitosPendentesFiltrados.get(i).getValor().toUpperCase())
.contains(constraint.toString().toUpperCase())){
DebitosPendentes debitosPendentes = new DebitosPendentes();
debitosPendentes= debitosPendentesFiltrados.get(i);
filterDebitos.add(debitosPendentes);
}
if((debitosPendentesFiltrados.get(i).getValorAtualizado().toUpperCase())
.contains(constraint.toString().toUpperCase())){
DebitosPendentes debitosPendentes = new DebitosPendentes();
debitosPendentes= debitosPendentesFiltrados.get(i);
filterDebitos.add(debitosPendentes);
}
}
results.count=filterDebitos.size();
results.values=filterDebitos;
}else{
results.count=debitosPendentesFiltrados.size();
results.values=debitosPendentesFiltrados;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
debitosPendentesList = (List<DebitosPendentes>) results.values;
notifyDataSetChanged();
}
}
}
Activity where I use the Adapter:
public class DebitosPendentesActivity extends AppCompatActivity{
private ListView listDebitosPendentes;
private ArrayAdapter<DebitosPendentes> arrayAdapter;
public DatabaseHelper databaseHelper;
public DebitoPendentesDao debitoPendentesDao;
public ItensDao itensDao;
private SearchView searchView;
private EditText edtPesquisa;
AdapterListaDebitosPendentes adapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_debitos_pendentes);
try {
databaseHelper= new DatabaseHelper(DebitosPendentesActivity.this);
debitoPendentesDao= new DebitoPendentesDao(databaseHelper.getConnectionSource());
final List<DebitosPendentes>debitosPendentesList = debitoPendentesDao.queryForAll();
listDebitosPendentes = (ListView) findViewById(R.id.listDebitosPendentes);
adapter= new AdapterListaDebitosPendentes(debitosPendentesList, this);
listDebitosPendentes.setAdapter(adapter);
//searchView = (SearchView) findViewById(R.id.search_view);
listDebitosPendentes.setTextFilterEnabled(true);
// setupSearchView();
listDebitosPendentes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent= new Intent(getApplicationContext(),DetalhesDebitosActivity.class);
//Passa para a activity o id no banco de dados
intent.putExtra("ID",id);
startActivity(intent);
}
});
} catch (SQLException e) {
e.printStackTrace();
}
edtPesquisa= (EditText) findViewById(R.id.edtPesquisa);
edtPesquisa.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//adapter.getFilter().filter(s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
///adapter.getFilter().filter(s);
}
});
}
/* private void setupSearchView() {
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(false);
}
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
listDebitosPendentes.clearTextFilter();
} else {
listDebitosPendentes.setFilterText(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}*/
}