2016-05-02 6 views
0

私は助けが必要です。誰かが私を助けてくれたコードがありますが、リスト内の名前を検索するとリストが空になり、リストを消去すると空のままです。活動リストビューで検索する

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(); 
} 
} 

:活動の

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(); 
} 
} 

レイアウト:

<?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> 

リストビューのための私のレイアウトが含まれている私は

アダプタを私の活動を掲示し、私のアダプターます画像ボタンとノームとntelemovelと2つのラベル。

+0

カスタムアダプタを持つListViewの[SearchView]の可能な複製(0120-663-0311) –

+1

http:// wwwをチェックしてください.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html – Bharatesh

答えて

0

このチュートリアルでは、私の問題を解決してくれてありがとう、このチュートリアルのメソッドでいくつかのメソッドを置き換え、それは働いていた。 http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html私のケースで問題が発生するのは、listviewに存在しないものを入力するときだけです。リストビューdoes notは空になります。なぜなら、そこには何も入力していないからです。

関連する問題