2012-09-15 80 views
0

在我的應用程序中,我創建了一個包含textview,imageview和button的自定義listview,並且我能夠成功加載該列表中的json數據。現在我想實現一個textwatcher,這樣我可以整理我的列表,只能查看由編輯文本過濾這些項目..如何在自定義ListView上實現AddTextChangedListener(BaseAdapter)

代碼CustomList活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    lv = (ListView)findViewById(R.id.listView1); 
    et1 = (EditText)findViewById(R.id.editText1); 
    inflator = getLayoutInflater(); 
    et1.addTextChangedListener(this); 
    JsonParser jParser = new JsonParser(); 
    JSONObject json = jParser.getJSONfromUrl(url); 
    try 
    { 
     JSONArray explore = json.getJSONArray("explore"); 
     for(int i=0; i<explore.length(); i++) 
     { 
      JSONObject exp = explore.getJSONObject(i); 
      list.add(exp.getString("username")); 
     } 
    } 
    catch(JSONException e) 
    { 
     e.printStackTrace(); 
    } 

    srchadptr = new SearchAdapter(this, inflator, list); 
    lv.setAdapter(srchadptr); 
} 

public void afterTextChanged(Editable s) { 
    // TODO Auto-generated method stub 

} 

public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
    // TODO Auto-generated method stub 

} 

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    // TODO Auto-generated method stub 
    srchadptr.getItem(start); 
} 

代碼SearchAdapter類( BaseAdapter):

public class SearchAdapter extends BaseAdapter { 

    Context context; 
    LayoutInflater inflater; 
    Button btn; 
    View vw; 
    ArrayList<String> list = new ArrayList<String>(); 

    public SearchAdapter(Context context, LayoutInflater inflater, ArrayList<String> list) { 
     // TODO Auto-generated constructor stub 
     this.context = context; 
     this.inflater = inflater; 
     this.list = list; 
    } 

    /*public CharSequence filter(CharSequence cs) { 
     return cs; 
    }*/ 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return list.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     vw = inflater.inflate(R.layout.list_items, null); 
     ImageView img = (ImageView)vw.findViewById(R.id.imageView1); 
     TextView tv = (TextView)vw.findViewById(R.id.textView1); 
     btn = (Button)vw.findViewById(R.id.button1); 
     tv.setText(String.valueOf(list.get(position))); 
     btn.setText(String.valueOf(list.get(position))); 
     btn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(context, list.get(position), Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return vw; 
    } 
} 

Plase幫助我讓我知道如何才能實現這個SearchAdapter級過濾器...

在此先感謝。 ..

回答

-1

您非常接近解決方案,請在getView方法中進行以下更改。

public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View view = super.getView(position, convertView, parent); 

      LinearLayout vwParentRow = (LinearLayout) view; 

      final EditText yourtextview= ((EditText) vwParentRow.getChildAt(2)); 
      //let say your edittext is 2nd in the listview 
      yourtextview.addTextChangedListener(yourtextchangelistener); 
      .......... 

     return view ; 
    } 
+0

我將如何過濾列表,因爲沒有這樣的適配器在這裏..請幫助...感謝..和執行你的代碼後,我也越來越NullPointerException錯誤..什麼這個「2」代表在getChildAt(2)..謝謝 –

相關問題