0

I need help i have a code that someone helped me but its not working because when i search a name thats in the list the list just goes empty and when i erase the list it stays empty. i will post my activity and my adapter

adapter:

public class ClienteSearchListAdapter extends ArrayAdapter<Cliente> {

protected static final String LOG_TAG = ClienteListAdapter.class.getSimpleName();

private List<Cliente> items;
private int layoutResourceId;
private Context context;
private ArrayList<Cliente> arraylist;

public ClienteSearchListAdapter(Context context, int layoutResourceId, List<Cliente> items) {
    super(context, layoutResourceId, items);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.items = items;
    this.arraylist = new ArrayList<Cliente>();
    this.arraylist.addAll(items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);

    AtomPaymentHolder holder = new AtomPaymentHolder();
    holder.cliente = items.get(position);
    holder.searchCliente = (ImageButton)row.findViewById(R.id.actionsearch_button);
    holder.searchCliente.setTag(holder.cliente);

    holder.nomecliente = (TextView)row.findViewById(R.id.nomecliente);
    holder.ntele = (TextView)row.findViewById(R.id.ntelecliente);

    row.setTag(holder);

    setupItem(holder);
    return row;
}

private void setupItem(AtomPaymentHolder holder) {
    holder.nomecliente.setText(holder.cliente.getNomeCompleto());
    holder.ntele.setText(String.valueOf(holder.cliente.getNtelemovel()));
}

public static class AtomPaymentHolder {
    Cliente cliente;
    TextView nomecliente;
    TextView ntele;
    ImageButton searchCliente;
}

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    items.clear();
    if (charText.length() == 0) {
        items.addAll(arraylist);
    }
    else
    {
        for (Cliente wp : arraylist)
        {
            if (wp.getNomeCompleto().toLowerCase(Locale.getDefault()).contains(charText))
            {
                items.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}
}

activity:

public class search_cli extends BaseNavegationActivity {

private ClienteSearchListAdapter adapter;

List<Cliente> cliente;
EditText editsearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_cli);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    editsearch = (EditText) findViewById(R.id.searchname);

    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub
        }
    });

    ServerRequests serverRequests = new ServerRequests(this);
    serverRequests.FetchClienteDataInBackground(userLocalStore.getLoggedInUser(), new GetContactosCallBack() {
        @Override
        public void done(List<Cliente> returnUser) {
            try {
                if (returnUser == null) {
                    throw new Exception("Não existem dados ou ocorreu um erro no servidor\nTente novamente mais tarde.");

                }
                for (Cliente cliente : returnUser) {
                    adapter.add(cliente);
                }
            }
            catch (Exception erro){
                showError(erro);
            }
        }
    });

    cliente = new ArrayList<>();

    setupListViewAdapter();

}

public void actionOnClickHandler( final View v) {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        try{
            final Cliente itemToaction = (Cliente) v.getTag();


        }
        catch (Exception er)
        {
            showError();
        }
    }
}

private void setupListViewAdapter() {
    adapter = new ClienteSearchListAdapter(search_cli.this, R.layout.clientesearch, cliente);
    ListView atomPaysListView = (ListView)findViewById(R.id.EnterPays_atomPaysList);
    atomPaysListView.setAdapter(adapter);
}

private void showError(Exception ero){
    AlertDialog.Builder dialogBuilder=new AlertDialog.Builder(this);
    dialogBuilder.setMessage("Ocorreu um erro:\n" + ero.getMessage());
    dialogBuilder.setPositiveButton("ok", null);
    dialogBuilder.show();
}

private void showError(){
    android.app.AlertDialog.Builder dialogBuilder=new android.app.AlertDialog.Builder(search_cli.this);
    dialogBuilder.setMessage("Ocorreu um erro, por favor tente novamente mais tarde.");
    dialogBuilder.setPositiveButton("Ok", null);
    dialogBuilder.show();
}
}

layout of the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".search_cli"
android:id="@+id/search_cli_container"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchname"
    android:layout_marginTop="10dp"
    android:hint="Pesquisar"/>

<ListView
    android:id="@+id/EnterPays_atomPaysList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/clientesearch"
    android:layout_below="@+id/layout_search_cli" >
</ListView>
</LinearLayout>

My layout for the list view contains an image button and two labels with nome and ntelemovel.

André Marques
  • 180
  • 1
  • 15
  • Possible duplicate of [SearchView In ListView having a custom Adapter](http://stackoverflow.com/questions/23422072/searchview-in-listview-having-a-custom-adapter) – Devendra Singh May 02 '16 at 19:57
  • 1
    check this http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html – Bharatesh May 03 '16 at 06:54

1 Answers1

0

thank you skadoosh this tutorial solved my problem just replaced some methods with methods from this tutorial and it was working. http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html Only problem in my case is when you type something that doesnt exists in listview the list view doesnt goes empty and it should because what im typing its not there but if i type something that is in list it will appear.

André Marques
  • 180
  • 1
  • 15