2014-04-01 6 views
1

내 어댑터 클래스에서 텍스트 뷰 ..에서 arraylist를 설정하는 응용 프로그램을 만들었지 만 검색시 텍스트 검색기로 해당 사용자 검색을 필터링 한 다른 목록을 추가하려고합니다. textview.As 내 어댑터 전체 연락처 목록을 가지고, 나는 그것을 textwatcher의 도움으로 만든 새 목록으로 교체하고 싶습니다. MainAactivitytextview 데이터를 변경하는 방법

case R.id.action_search: 

        searchText.setVisibility(View.VISIBLE); 


        searchText.addTextChangedListener(new TextWatcher() { 

         public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub 
          s=searchText.getText().toString(); 
          ma.getFilter().filter(s); 

         } 
        }); 


        break; 
       } 
       return super.onOptionsItemSelected(item); 
      } 

MyAdapter

@Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View vi = convertView; 
      if (convertView == null) 
       vi = mInflater.inflate(R.layout.row, null); 
      tv = (TextView) vi.findViewById(R.id.textView1); 
      tv1 = (TextView) vi.findViewById(R.id.textView2); 
      cb = (CheckBox) vi.findViewById(R.id.checkBox1); 

      tv.setText(name1.get(position)); 
      tv1.setText(phno1.get(position)); 



      cb.setTag(position); 

      cb.setChecked(mCheckStates.get(position, false)); 
      cb.setOnCheckedChangeListener(this); 
      // Toast.makeText(getApplication(), String.valueOf(name1.size()).toString(), Toast.LENGTH_LONG).show(); 
      return vi; 

     } 

getfilter()

@Override 
    public Filter getFilter() { 

     // TODO Auto-generated method stub 
     Filter filter = new Filter() { 

      public void publishResults(CharSequence s, FilterResults results) { 
       //Toast.makeText(getApplication(), String.valueOf(name1.size()).toString(), Toast.LENGTH_LONG).show(); 
       arrayListNames = (List<String>) results.values; 
       notifyDataSetChanged(); 
      } 

      @Override 
      public FilterResults performFiltering(CharSequence s) { 
       search=true; 
       FilterResults results = new FilterResults(); 
       ArrayList<String> FilteredArrayNames = new ArrayList<String>(); 
      Toast.LENGTH_LONG).show(); 
       for (int i = 0; i < name1.size(); i++) { 

        String dataNames = name1.get(i).toString(); 
        if (dataNames.toLowerCase().trim().startsWith(s.toString())) { 

         Toast.makeText(getApplication(),s.toString(), Toast.LENGTH_LONG).show(); 
         FilteredArrayNames.add(name1.get(i)); 
        } 
       } 

       results.count = FilteredArrayNames.size(); 
       results.values = FilteredArrayNames; 


       return results; 
      } 
     }; 

     return filter; 
    } 
} 
+0

와우, 무엇을? 귀하의 질문을 단순화하고 아무도 그것을 읽을 수없는 코드의 2000 줄을 붙여 넣지 마십시오. 우리가 한 일을 보여줘. – VM4

+0

@ VMI m app를 개발 중입니다. 목록 및 위의 편집 텍스트가있는 곳에서 editText에 무언가를 입력합니다. 입력 한 단어와 일치하는 결과를 얻어야합니다 .bt 결과를 얻지 못합니다. Plz이 실수를 저지르고 있습니다. – Himani

+0

@ Himani 내 getFilter() 메서드를 확인하십시오 –

답변

1

MainActivity.java 012,351

6,
package com.example.mylistviewtest; 

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

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.Toast; 




public class MainActivity extends Activity { 

    private ListView listview; 
    private EditText edittext; 
    private List<ProfileBean> list; 
    private SearchableAdapter adapter ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     listview = (ListView) findViewById(R.id.listview); 
     edittext = (EditText) findViewById(R.id.edittext); 

     list = new ArrayList<ProfileBean>(); 

     list.add(new ProfileBean("AAA", "AA", "123")); 
     list.add(new ProfileBean("AAB", "AB", "100")); 
     list.add(new ProfileBean("AAC", "BC", "101")); 
     list.add(new ProfileBean("AAD", "DC", "154")); 
     list.add(new ProfileBean("BBB", "DF", "22")); 
     list.add(new ProfileBean("BBA", "AD", "22")); 
     list.add(new ProfileBean("BBD", "FD", "44")); 
     list.add(new ProfileBean("BAT", "AD", "44")); 
     list.add(new ProfileBean("BBD", "FF", "75")); 
     list.add(new ProfileBean("BAD", "AA", "88")); 


     adapter = new SearchableAdapter(getApplicationContext(), list); 
     listview.setAdapter(adapter); 


     edittext.addTextChangedListener(new TextWatcher(){ 
      @Override 
      public void afterTextChanged(Editable arg0) { 

      } 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       adapter.getFilter().filter(s.toString()); 
      } 
     }); 


     listview.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 
       ProfileBean bean = (ProfileBean) arg1.getTag(); 

       Toast.makeText(getApplicationContext(), bean.getName(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

} 

ProfileBean

package com.example.mylistviewtest; 

public class ProfileBean { 
    private String name; 
    private String lname; 
    private String no; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getLname() { 
     return lname; 
    } 
    public void setLname(String lname) { 
     this.lname = lname; 
    } 
    public String getNo() { 
     return no; 
    } 
    public void setNo(String no) { 
     this.no = no; 
    } 
    public ProfileBean(String name, String lname, String no) { 
     super(); 
     this.name = name; 
     this.lname = lname; 
     this.no = no; 
    } 



} 

SearchableAdapter.java

package com.example.mylistviewtest; 

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

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Filter; 
import android.widget.Filterable; 
import android.widget.TextView; 

// The standard text view adapter only seems to search from the beginning of whole words 
// so we've had to write this whole class to make it possible to search 
// for parts of the arbitrary string we want 
public class SearchableAdapter extends BaseAdapter implements Filterable { 

private List<ProfileBean>originalData = null; 
private List<ProfileBean>filteredData = null; 
private LayoutInflater mInflater; 
private ItemFilter mFilter = new ItemFilter(); 

public SearchableAdapter(Context context, List<ProfileBean> data) { 
    this.filteredData = data ; 
    this.originalData = data ; 
    mInflater = LayoutInflater.from(context); 
} 

public int getCount() { 
    return filteredData.size(); 
} 

public Object getItem(int position) { 
    return filteredData.get(position); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    // A ViewHolder keeps references to children views to avoid unnecessary calls 
    // to findViewById() on each row. 
    ViewHolder holder; 

    // When convertView is not null, we can reuse it directly, there is no need 
    // to reinflate it. We only inflate a new View when the convertView supplied 
    // by ListView is null. 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 

     // Creates a ViewHolder and store references to the two children views 
     // we want to bind data to. 
     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.name); 
     holder.lname = (TextView) convertView.findViewById(R.id.lname); 
     holder.no = (TextView) convertView.findViewById(R.id.no); 

     // Bind the data efficiently with the holder. 

     convertView.setTag(R.layout.list_item,holder); 
    } else { 
     // Get the ViewHolder back to get fast access to the TextView 
     // and the ImageView. 
     holder = (ViewHolder) convertView.getTag(R.layout.list_item); 
    } 

    // If weren't re-ordering this you could rely on what you set last time 

    ProfileBean bean = filteredData.get(position); 
    holder.name.setText(bean.getName()); 
    holder.lname.setText(bean.getLname()); 
    holder.no.setText(bean.getNo()); 
    convertView.setTag(bean); 

    return convertView; 
} 

static class ViewHolder { 
    TextView name; 
    TextView lname; 
    TextView no; 
} 

public Filter getFilter() { 
    return mFilter; 
} 

private class ItemFilter extends Filter { 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 

     String filterString = constraint.toString().toLowerCase(); 

     FilterResults results = new FilterResults(); 

     final List<ProfileBean> list = originalData; 

     int count = list.size(); 
     final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count); 

     String filterableString ; 

     for (int i = 0; i < count; i++) { 

      ProfileBean bean = list.get(i); 
      filterableString = bean.getName(); 
      if (filterableString.toLowerCase().contains(filterString)) { 
       nlist.add(bean); 
      } 
     } 

     results.values = nlist; 
     results.count = nlist.size(); 

     return results; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
     filteredData = (ArrayList<ProfileBean>) results.values; 
     notifyDataSetChanged(); 
    } 

} 
} 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/edittext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 
    </ListView> 

</LinearLayout> 

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="100dp" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textColor="@android:color/black" 
     android:textSize="16sp" > 
    </TextView> 

    <TextView 
     android:id="@+id/lname" 
     android:layout_width="100dp" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textColor="@android:color/black" 
     android:textSize="16sp" > 
    </TextView> 

    <TextView 
     android:id="@+id/no" 
     android:layout_width="100dp" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textColor="@android:color/black" 
     android:textSize="16sp" > 
    </TextView> 

</LinearLayout> 
+0

FilteredArrayNames.add (name1.get (i)); 이게 내가 무엇에 .. ?? – Himani

+0

확인 코드 FilteredArrayNames.add (p)를 업데이트했습니다. –

+0

@ Kanaiya Bhanushali tnx하지만 그것은 도움이되지 않았다 .. 실제로 내가 아는 것이 문제는 내 getview() MyAdapter에서, 나는 하드 코드 textviews name1 arraylist와 함께,하지만 난 knw 어떻게 arraylist 이름을 변경 FIlteredArrayNames와 – Himani

관련 문제