2013-11-23 144 views
0

我想过滤使用顶部的编辑文本的列表视图,但它在adapter2.filter(文本)添加文本更改监听器提供空指针异常。请给我一些suggestion`带过滤器的列表视图

这里是我的编辑text`

friendsList.setAdapter(new FriendListAdapter(this)); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

      String text = search.getText().toString().toLowerCase(Locale.getDefault()); 
      System.out.println("test=="+text); 
      adapter2.filter(text); 
     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 
      //adapter.getFilter().filter(arg0.toString()); 

     } 

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

     } 


    }); 

这里是我的适配器

public class FriendListAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 

    FriendsList friendsList; 
    Context context; 
    ViewHolder holder; 
    private boolean userSelected = false; 
    private RadioButton mCurrentlyCheckedRB; 

    private int mResourceId = 0; 
    private LayoutInflater mLayoutInflater; 
    private RadioButton mSelectedRB; 
    private int mSelectedPosition = -1; 

    public FriendListAdapter(FriendsList friendsList) { 
     this.friendsList = friendsList; 
     if (Utility.model == null) { 
      Utility.model = new FriendsGetProfilePics(); 
     } 
     Utility.model.setListener(this); 
     mInflater = LayoutInflater.from(friendsList.getBaseContext()); 
    } 

    @Override 
    public int getCount() { 
     return jsonArray.length(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup viewgroup) { 
     JSONObject jsonObject = null; 
     Model model = (Model) getItem(position); 
     try { 
      jsonObject = jsonArray.getJSONObject(position); 
     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     } 
     View hView = convertView; 
     if (convertView == null) { 
      hView = mInflater.inflate(R.layout.friend_item, null); 
      ViewHolder holder = new ViewHolder(); 
      holder.profile_pic = (ImageView) hView 
        .findViewById(R.id.profile_pic); 
      holder.name = (TextView) hView.findViewById(R.id.name); 
      holder.info = (TextView) hView.findViewById(R.id.info); 
      holder.radiobt = (RadioButton) hView.findViewById(R.id.radio); 
      hView.setTag(holder); 
     } 
     ViewHolder holder = (ViewHolder) hView.getTag(); 
     if (position == getCount() - 1 && userSelected == false) { 
      holder.radiobt.setChecked(true); 
      mCurrentlyCheckedRB = holder.radiobt; 
     } else { 
      holder.radiobt.setChecked(false); 
     } 

     holder.radiobt.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 



       if((position != mSelectedPosition && mSelectedRB != null)){ 
        mSelectedRB.setChecked(false); 
       } 

       mSelectedPosition = position; 
       mSelectedRB = (RadioButton)v; 


       System.out.println("onItemClick "); 
       try { 

        if (graph_or_fql.equals("graph")) { 

         System.out.println("in if loop "); 
         friendId = jsonArray.getJSONObject(position).getLong("id"); 
         image = jsonArray.getJSONObject(position).getString("picture"); 
         // sb.append(friendId).append(","); 
         freind_id = String.valueOf(friendId); 

        } else { 

         System.out.println("in else loop "); 
         friendId = jsonArray.getJSONObject(position).getLong("uid"); 
         image = jsonArray.getJSONObject(position).getString(
           "pic_square"); 
         // sb.append(friendId).append(","); 
         freind_id = String.valueOf(friendId); 
        } 
        check = true; 
        name = jsonArray.getJSONObject(position).getString("name"); 

        Toast.makeText(getApplicationContext(), "You Selected : " + name, 
          Toast.LENGTH_SHORT).show(); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
        e.getMessage(); 
       } 



      } 
     }); 


     if(mSelectedPosition != position){ 
      holder.radiobt.setChecked(false); 
     }else{ 
      holder.radiobt.setChecked(true); 
      if(mSelectedRB != null && holder.radiobt != mSelectedRB){ 
       mSelectedRB = holder.radiobt; 
      } 
     } 

    try { 
      if (graph_or_fql.equals("graph")) { 
       holder.profile_pic.setImageBitmap(Utility.model.getImage(
         jsonObject.getString("id"), 
         jsonObject.getString("picture"))); 
      } else { 
       holder.profile_pic.setImageBitmap(Utility.model.getImage(
         jsonObject.getString("uid"), 
         jsonObject.getString("pic_square"))); 
      } 
     } catch (JSONException e) { 
      holder.name.setText(""); 
     } 
     try { 
      holder.name.setText(jsonObject.getString("name")); 
     } catch (JSONException e) { 
      holder.name.setText(""); 
     } 
     try { 
      if (graph_or_fql.equals("graph")) { 
       holder.info.setText(jsonObject.getJSONObject("location") 
         .getString("name")); 
      } else { 
       JSONObject location = jsonObject 
         .getJSONObject("current_location"); 
       holder.info.setText(location.getString("city") + ", " 
         + location.getString("state")); 
      } 

     } catch (JSONException e) { 
      holder.info.setText(""); 
     } 
     return hView; 

    } 


    // Filter Class 
    public void filter(String charText) { 
     System.out.println("in adapter filter"); 
     charText = charText.toLowerCase(Locale.getDefault()); 
     System.out.println("1"); 
     rowitems.clear(); 
     System.out.println("2"); 

     if (charText.length() == 0) { 
      System.out.println("3"); 
      rowitems.addAll(listData); 
     } else { 
      for (Model wp : listData) { 
       if (wp.getName().toLowerCase(Locale.getDefault()) 
         .contains(charText)) { 
        rowitems.add(wp); 
       } 
      } 
     } 
     notifyDataSetChanged(); 
    } 


    private class ViewHolder { 
     ImageView profile_pic; 
     TextView name; 
     TextView info; 
     // CheckBox check; 
     RadioButton radiobt; 
    }  
} 

这是我的主要活动

public class FriendsList extends Activity implements OnItemClickListener{ 

private Handler mHandler; 
public static Long friendId; 
public static String name = ""; 
protected ListView friendsList; 
protected static JSONArray jsonArray; 
protected String graph_or_fql; 
public Button bt; 
public static String image = "0"; 
public boolean check = false; 
public static String freind_id = ""; 
public boolean select = false; 
public RadioButton radiobtn;  

public ListView friendList; 
private List<Model> rowitems=null; 
ArrayList<Model> listData; 
    AdapterList adapter; 
    EditText search; 

FriendListAdapter adapter2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mHandler = new Handler(); 
    setContentView(R.layout.friends_list); 
    //radioSexGroup = (RadioGroup) findViewById(R.id.radioSex); 
    bt = (Button) findViewById(R.id.com_facebook_picker_done_button); 
    radiobtn = (RadioButton) findViewById(R.id.radio); 

    search=(EditText) findViewById(R.id.editText100); 

    Bundle extras = getIntent().getExtras(); 
    String apiResponse = extras.getString("API_RESPONSE"); 
    graph_or_fql = extras.getString("METHOD"); 
    try { 
     if (graph_or_fql.equals("graph")) { 
      jsonArray = new JSONObject(apiResponse).getJSONArray("data"); 
     } else { 
      jsonArray = new JSONArray(apiResponse); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     e.getMessage(); 
     return; 
    } 
    friendsList = (ListView) findViewById(R.id.friends_list); 

// friendsList.setAdapter(new FriendListAdapter(this)); 

    adapter2=new FriendListAdapter(this); 
    friendsList.setAdapter(adapter2); 

    friendsList.setOnItemClickListener(this); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

     } 

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

       adapter2.getFilter().filter(arg0.toString()); 
     } 

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

     } 
    }); 

我传递的Facebook的朋友名单listview.please提供一些建议 在此先感谢

+0

我认为你的问题是在Filter Class中有'rowitems',你能打印'1'吗? –

+0

没有它根本没有getfilter方法 – Gajendran

+0

你为'adapter2'设置了什么 – SathishKumar

回答

0

变化与下面的代码过滤条件:

@Override 
public Filter getFilter() { 
    //Log.d("in filter", "yes"); 
    return new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       final FilterResults oReturn = new FilterResults(); 

,并设置此功能列表和后置列表:

oReturn.values = YourList; 

和finall

return oReturn; 

,并在你的search.addTextChangedListener(new TextWatcher())只需在onTextChanged添加以下行:

adapter2.getFilter().filter(s.toString()); 
+0

它不在所有调用过滤器方法,它显示NPE在addtext更改监听器 – Gajendran

+0

什么是'adapter2'?这是你显示数据的第一个适配器还是新的适配器? –

+0

好吧,让我试试 – Gajendran

0

试试这个,

adapter2=new FriendListAdapter(this); 
friendsList.setAdapter(adapter2); 
+0

NPE没有同样的问题 – Gajendran

1

对于过滤器使用编辑文本顶部的列表视图。 我使用了这段代码。 请叫searchResults比在edittextonTextChanged方法一个列表简单地做这个 -

String searchString = `your edittext object`.getText().toString(); 
int textLength = searchString.length(); 
searchResults.clear(); 
for (int i = 0; i < `your main list of items`.size(); i++) { 
String name = `your main list of items`.get(i).get("`your key`").toString(); 
System.out.println(" name " + name); 
if (textLength <= title.length()) { 
// compare the String in EditText with Names in the 
// ArrayList 
if (searchString.equalsIgnoreCase(name.substring(0, textLength))) { 
    searchResults.add(`your main list of items`.get(i)); 
    System.out.println("the array list is "+ `your main list of items`.get(i)); 
    mAdapter = new Adapter(this, searchResults); 
    `your ListView object`.setAdapter(mAdapter); 
    } 
    } 
    } 
    if (searchResults.isEmpty()) { 
    Toast toast = Toast.makeText(getApplicationContext(),"No Items Matched",Toast.LENGTH_SHORT); 
    toast.show(); 
    mAdapter = new Adapter(this, searchResults); 
    `your ListView object`.setAdapter(mAdapter); 
} 
    mAdapter.notifyDataSetChanged(); 

setOnItemClickListener如果真比只使用检查searchResults.isEmpty()your main list of items,如果假不是使用searchResults名单。 可能会帮助你。试试这个。