title: Listview Select and Unselect Item Android
body: <p>I am able to select items and adding to sharepreference. But once I click on selected item Its not set as unselected. When I select any tag it should add in Sharepreference and when I tap on selected item then it should be remove from sharepreferance. </p>
  
  <p>Any other way of doing this will be very helpful or please show me my mistake:)</p>
  
  <p>Below is my code.</p>
  
  <pre><code>public class Filter extends Activity {
  
  ListView filterlist;
  ArrayAdapter&lt;String&gt; arrayAdapter;
  String tag;
  int tagcount = 0;
  private ArrayList&lt;String&gt; taglist = new ArrayList&lt;String&gt;();
  private ArrayList&lt;String&gt; taglist1 = new ArrayList&lt;String&gt;();
  SharedPreferences myPrefs;
  private final String TAG_NAME = "tagname";
  String prefName, tagged;
  SharedPreferences.Editor prefsEditor;
  DatabaseHandler db;
  Dbresultsummary rs;
  ImageAdapter img;
  ArrayList&lt; String&gt; preflist = new ArrayList&lt;String&gt;();
  String item;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_filter);
      myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
      prefsEditor = myPrefs.edit();
      prefName = myPrefs.getString(TAG_NAME, "No Tag");
  
      rs = new Dbresultsummary(getApplicationContext());
      img = new ImageAdapter(this);
  
      filterlist = (ListView) findViewById(R.id.filterlist);
      filterlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  
      filterlist.setAdapter(img);
  
      filterlist.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                  long arg3) {
              prefName = myPrefs.getString(TAG_NAME, "No Tag");
              String s = myPrefs.getString(TAG_NAME, "No Tag");
              if (s.equals("No Tag")) {
                  s = "";
              }
              else
              {
  
              preflist.add(s);
  
              }
  
              int len = filterlist.getCount();
  
              SparseBooleanArray checked = filterlist
                      .getCheckedItemPositions();
              for (int i = 0; i &lt; len; i++) {
                  if (checked.get(i)) {
                      item = taglist.get(i);
                      preflist.add(item);
              }
              }
  
  
  
              HashSet&lt;String&gt; hs = new HashSet();
              hs.addAll(preflist);
              preflist.clear();
              preflist.addAll(hs);
  
  
              s = preflist.toString().replace("[", "").replace("]", "")
                      .replace(", ", ",");
  
  
  
              if(s.equals(""))
              {
                  s ="No Tag";
              }
              prefsEditor.putString(TAG_NAME, s);
  
              prefsEditor.commit();
  
              img.notifyDataSetChanged();
  
          }
  
      });
  
      List&lt;Resultsummary&gt; getall = rs.getalltag();
  
      for (Resultsummary cn : getall) {
  
          tag = cn.getTag();
  
          StringTokenizer st = new StringTokenizer(tag, ",");
          while (st.hasMoreElements()) {
              String temptag = (String) st.nextElement();
  
              taglist1.add(temptag);
              HashSet&lt;String&gt; hs = new HashSet();
              hs.addAll(taglist1);
              taglist1.clear();
              taglist1.addAll(hs);
          }
  
      }
      rs.close();
  }
  
  public class ImageAdapter extends BaseAdapter {
  
      private LayoutInflater mInflater;
      String listtag, seltag;
      Context mContext;
      int count = 0;
  
      public ImageAdapter(Context c) {
          mInflater = LayoutInflater.from(c);
  
      }
  
      @Override
      public int getCount() {
  
          return taglist1.size();
      }
  
      @Override
      public Object getItem(int position) {
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
              convertView = mInflater.inflate(R.layout.lv, null);
  
              holder = new ViewHolder();
              holder.title = (TextView) convertView
                      .findViewById(R.id.customrow);
  
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          rs = new Dbresultsummary(getApplicationContext());
  
          List&lt;Resultsummary&gt; getall = rs.getalltag();
  
          for (Resultsummary cn : getall) {
  
              tag = cn.getTag();
  
              StringTokenizer st = new StringTokenizer(tag, ",");
              while (st.hasMoreElements()) {
                  String temptag = (String) st.nextElement();
  
                  taglist.add(temptag);
                  HashSet&lt;String&gt; hs = new HashSet();
                  hs.addAll(taglist);
                  taglist.clear();
                  taglist.addAll(hs);
              }
  
          }
          holder.title.setText(taglist.get(position));
  
          prefName = myPrefs.getString(TAG_NAME, "No Tag");
          List&lt;String&gt; prefList = Arrays.asList(prefName.split(","));
          for (int k = 0; k &lt; prefList.size(); k++) {
              listtag = holder.title.getText().toString();
              seltag = prefList.get(k);
  
              if (seltag.equals(listtag)) {
                  convertView.setSelected(true);
                  break;
                  }
              else
              {
                  convertView.setSelected(false);
              }
  
          }
          if (convertView.isSelected()) {
              convertView.setBackgroundColor(0x9934B5E4);
          } else {
              convertView.setBackgroundColor(android.R.color.transparent);
          }
  
  
          rs.close();
          return convertView;
      }
  
      class ViewHolder {
          TextView title;
  
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.filter, menu);
      return true;
  }
  
  public boolean onOptionsItemSelected(MenuItem item) {
  
      // when you click setting menu
      switch (item.getItemId()) {
      case R.id.back:
          Intent intent = new Intent(this, CompleteResult.class);
          startActivity(intent);
          return true;
      case R.id.cleartag:
          String defaulttag = "No Tag";
          prefsEditor.putString(TAG_NAME, defaulttag);
          prefsEditor.commit();
          img.notifyDataSetChanged();
  
          return true;
  
      default:
          return super.onOptionsItemSelected(item);
      }
  
  }
  
  @Override
  public void onBackPressed()
  {
      Intent intent = new Intent(this, CompleteResult.class);
      startActivity(intent);
  }
  </code></pre>
  
  <p>}</p>