Note that there are some explanatory texts on larger screens.

plurals
  1. POListview with filter
    text
    copied!<p>i am trying to filter the listview using edit text at the top but it providing null pointer exception in the adapter2.filter(text) of add text changed listener . please provide me some suggestion`</p> <p>Here is my edit text`</p> <pre><code>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 } }); </code></pre> <p>Here is my adapter</p> <pre><code>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 &amp;&amp; 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 &amp;&amp; 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 &amp;&amp; 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; } } </code></pre> <p>Here is my main activity</p> <pre><code>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&lt;Model&gt; rowitems=null; ArrayList&lt;Model&gt; 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 } }); </code></pre> <p>i am passing the facebook friend list to the listview.please provide some suggestion Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload