title: Java.lang.Classcast Exception: java.lang.String cannot be cast to error in android
body: <p>I am working on listview app and in this Activity i am use a search box but when i enter a text in search box, i got error something Java.lang.Classcast Exception: java.lang.String cannot be cast to</p>
  
  <p>Activity</p>
  
  <pre><code>public class ConnectActivity extends Activity {
  
      // unsorted list items
      List&lt;Company&gt; mItems;
  
      // array list to store section positions
      ArrayList&lt;Integer&gt; mListSectionPos;
  
      // array list to store listView data
      ArrayList&lt;String&gt; mListItems;
  
      // custom list view with pinned header
      PinnedHeaderListView mListView;
  
      // custom adapter
      PinnedHeaderAdapter mAdaptor;
  
      // search box
      EditText mSearchView;
  
      // loading view
      ProgressBar mLoadingView;
  
      // empty view
      TextView mEmptyView;
  
  
  
      @SuppressWarnings("unchecked")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          overridePendingTransition(R.anim.activity_open_translate,
                  R.anim.activity_close_scale);
  
          // UI elements
          setupViews();
  
          mListSectionPos = new ArrayList&lt;Integer&gt;();
          mListItems = new ArrayList&lt;String&gt;();
  
          // for handling configuration change
          if (savedInstanceState != null) {
              mListItems = savedInstanceState.getStringArrayList("mListItems");
              mListSectionPos = savedInstanceState
                      .getIntegerArrayList("mListSectionPos");
  
              if (mListItems != null &amp;&amp; mListItems.size() &gt; 0
                      &amp;&amp; mListSectionPos != null &amp;&amp; mListSectionPos.size() &gt; 0) {
                  setListAdaptor();
              }
  
              String constraint = savedInstanceState.getString("constraint");
              if (constraint != null &amp;&amp; constraint.length() &gt; 0) {
                  mSearchView.setText(constraint);
                  setIndexBarViewVisibility(constraint);
              }
          } else {
              new Poplulate().execute(mItems);
          }
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          // closing transition animations
          overridePendingTransition(R.anim.activity_open_scale,
                  R.anim.activity_close_translate);
      }
  
      private void setupViews() {
          setContentView(R.layout.activity_connect);
          mSearchView = (EditText) findViewById(R.id.search_view);
          mLoadingView = (ProgressBar) findViewById(R.id.loading_view);
          mListView = (PinnedHeaderListView) findViewById(R.id.list_view);
          mListView.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  TextView selectedCompanyName = (TextView) view.findViewById(R.id.row_title);
                  Toast.makeText(getApplicationContext(), selectedCompanyName.getText().toString(), Toast.LENGTH_SHORT).show();
                  Company selectedCopmanyData = new Select().from(Company.class).where("Company_Name = ?", selectedCompanyName.getText().toString().trim()).executeSingle();
                  startActivity(new Intent(ConnectActivity.this, CompanyActivity.class).putExtra("SELECTED_COMPANY", selectedCopmanyData));
              }
          });
          mEmptyView = (TextView) findViewById(R.id.empty_view);
  
          //mItems = new Select().from(Company.class).where("Bookmark = ?", true).orderBy("Company_Name ASC").execute();
          mItems = new Select().from(Company.class).orderBy("Company_Name ASC").execute();
  
          if (mItems.size() == 0) {
              Company sampleData1 = new Company();
              sampleData1.companyName = "MahenCO";
              sampleData1.companyId = new Long(new SimpleDateFormat("ddMMyyyyHHmmSS").format(new Date()));
              sampleData1.groupId = new Integer(1);
              sampleData1.groupName = "Finance";
              sampleData1.customerSupportNo = "12345678";
              sampleData1.customerWebSite = "http://www.mahenco.com";
              sampleData1.customerSupportEmail = "customersupport@mahenco.com";
              sampleData1.bookmarked = false;
              sampleData1.save();
  
              Company sampleData2 = new Company();
              sampleData2.companyName = "ECG Ltd";
              sampleData2.companyId = new Long(new SimpleDateFormat("ddMMyyyyHHmmSS").format(new Date()));
              sampleData2.groupId = new Integer(1);
              sampleData2.groupName = "Insurance";
              sampleData2.customerSupportNo = "18002666";
              sampleData2.customerWebSite = "http://www.ecg.com";
              sampleData2.customerSupportEmail = "customersupport@ecg.com";
              sampleData2.bookmarked = true;
              sampleData2.save();
  
              mItems = new Select().from(Company.class).orderBy("Company_Name ASC").execute();
          }
      }
  
      // I encountered an interesting problem with a TextWatcher listening for
      // changes in an EditText.
      // The afterTextChanged method was called, each time, the device orientation
      // changed.
      // An answer on Stackoverflow let me understand what was happening: Android
      // recreates the activity, and
      // the automatic restoration of the state of the input fields, is happening
      // after onCreate had finished,
      // where the TextWatcher was added as a TextChangedListener.The solution to
      // the problem consisted in adding
      // the TextWatcher in onPostCreate, which is called after restoration has
      // taken place
      //
      // http://stackoverflow.com/questions/6028218/android-retain-callback-state-after-configuration-change/6029070#6029070
      // http://stackoverflow.com/questions/5151095/textwatcher-called-even-if-text-is-set-before-adding-the-watcher
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          mSearchView.addTextChangedListener(filterTextWatcher);
          super.onPostCreate(savedInstanceState);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater menuInflater = getMenuInflater();
          menuInflater.inflate(R.menu.connect, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          default:
              return super.onOptionsItemSelected(item);
          }
  
      }
  
      @Override
      public void onOptionsMenuClosed(Menu menu) {
          openOptionsMenu();
      }
  
      private void setListAdaptor() {
          // create instance of PinnedHeaderAdapter and set adapter to list view
          mAdaptor = new PinnedHeaderAdapter(this, mListItems, mListSectionPos);
          mListView.setAdapter(mAdaptor);
  
          LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  
          // set header view
          View pinnedHeaderView = inflater.inflate(R.layout.section_row_view,
                  mListView, false);
          mListView.setPinnedHeaderView(pinnedHeaderView);
  
          // set index bar view
          IndexBarView indexBarView = (IndexBarView) inflater.inflate(
                  R.layout.index_bar_view, mListView, false);
          indexBarView.setData(mListView, mListItems, mListSectionPos);
          mListView.setIndexBarView(indexBarView);
  
          // set preview text view
          View previewTextView = inflater.inflate(R.layout.preview_view,
                  mListView, false);
          mListView.setPreviewView(previewTextView);
  
          // for configure pinned header view on scroll change
          mListView.setOnScrollListener(mAdaptor);
      }
  
      private TextWatcher filterTextWatcher = new TextWatcher() {
          public void afterTextChanged(Editable s) {
              String str = s.toString();
              if (mAdaptor != null &amp;&amp; str != null)
                  mAdaptor.getFilter().filter(str);
          }
  
          public void beforeTextChanged(CharSequence s, int start, int count,
                  int after) {
          }
  
          public void onTextChanged(CharSequence s, int start, int before,
                  int count) {
  
          }
      };
  </code></pre>
  
  <p>Then i am entered my listFilter. This is the filter class</p>
  
  <pre><code>public class ListFilter extends Filter {
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
              // NOTE: this function is *always* called from a background thread,
              // and
              // not the UI thread.
              String constraintStr = constraint.toString().toLowerCase(
                      Locale.getDefault());
              FilterResults result = new FilterResults();
  
              if (constraint != null &amp;&amp; constraint.toString().length() &gt; 0) {
                  ArrayList&lt;String&gt; filterItems = new ArrayList&lt;String&gt;();
  
                  synchronized (this) {
                      for (int i = 0; i &lt; mItems.size(); i++) {
                          String item = mItems.get(i).companyName;
                          if (item.toLowerCase(Locale.getDefault()).startsWith(
                                  constraintStr)) {
                              filterItems.add(item);
                          }
                      }
                      result.count = filterItems.size();
                      result.values = filterItems;
                  }
              } else {
                  synchronized (this) {
                      result.count = mItems.size();
                      result.values = mItems;
                  }
              }
              return result;
          }
  
          @SuppressWarnings("unchecked")
          @Override
          protected void publishResults(CharSequence constraint,
                  FilterResults results) {
              List&lt;Company&gt; filtered = (List&lt;Company&gt;) results.values;
              setIndexBarViewVisibility(constraint.toString());
              // sort array and extract sections in background Thread
              new Poplulate().execute(filtered);
          }
  
      }
  
      private void setIndexBarViewVisibility(String constraint) {
          // hide index bar for search results
          if (constraint != null &amp;&amp; constraint.length() &gt; 0) {
              mListView.setIndexBarVisibility(false);
          } else {
              mListView.setIndexBarVisibility(true);
          }
      }
  
      // sort array and extract sections in background Thread here we use
      // AsyncTask
      private class Poplulate extends AsyncTask&lt;List&lt;Company&gt;, Void, Void&gt; {
  
          private void showLoading(View contentView, View loadingView,
                  View emptyView) {
              contentView.setVisibility(View.GONE);
              loadingView.setVisibility(View.VISIBLE);
              emptyView.setVisibility(View.GONE);
          }
  
          private void showContent(View contentView, View loadingView,
                  View emptyView) {
              contentView.setVisibility(View.VISIBLE);
              loadingView.setVisibility(View.GONE);
              emptyView.setVisibility(View.GONE);
          }
  
          private void showEmptyText(View contentView, View loadingView,
                  View emptyView) {
              contentView.setVisibility(View.GONE);
              loadingView.setVisibility(View.GONE);
              emptyView.setVisibility(View.VISIBLE);
          }
  
          @Override
          protected void onPreExecute() {
              // show loading indicator
              showLoading(mListView, mLoadingView, mEmptyView);
              super.onPreExecute();
          }
  
          @Override
          protected Void doInBackground(List&lt;Company&gt;... params) {
              mListItems.clear();
              mListSectionPos.clear();
              List&lt;Company&gt; items = params[0];
              if (mItems.size() &gt; 0) {
  
                  // NOT forget to sort array
                  // No need for sort as it has already sorted while feathing from database
                  //Collections.sort(items.);
  
                  int i = 0;
                  String prev_section = "";
                  while (i &lt; items.size()) {
                      String current_item = items.get(i).companyName;
                      String current_section = current_item.substring(0, 1)
                              .toUpperCase(Locale.getDefault());
  
                      if (!prev_section.equals(current_section)) {
                          mListItems.add(current_section);
                          mListItems.add(current_item);
                          // array list of section positions
                          mListSectionPos
                                  .add(mListItems.indexOf(current_section));
                          prev_section = current_section;
                      } else {
                          mListItems.add(current_item);
                      }
                      i++;
                  }
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              if (!isCancelled()) {
                  if (mListItems.size() &lt;= 0) {
                      showEmptyText(mListView, mLoadingView, mEmptyView);
                  } else {
                      setListAdaptor();
                      showContent(mListView, mLoadingView, mEmptyView);
                  }
              }
              super.onPostExecute(result);
          }
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          if (mListItems != null &amp;&amp; mListItems.size() &gt; 0) {
              outState.putStringArrayList("mListItems", mListItems);
          }
          if (mListSectionPos != null &amp;&amp; mListSectionPos.size() &gt; 0) {
              outState.putIntegerArrayList("mListSectionPos", mListSectionPos);
          }
          String searchText = mSearchView.getText().toString();
          if (searchText != null &amp;&amp; searchText.length() &gt; 0) {
              outState.putString("constraint", searchText);
          }
          super.onSaveInstanceState(outState);
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance.......</p>
  
  <p>this code is adopted by Mr. Bhavy mehta on github</p>
  
  <p>Here is my log cat</p>
  
  <pre><code>     FATAL EXCEPTION: AsyncTask #2
  java.lang.RuntimeException: An error occured while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:299)
  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  at java.lang.Thread.run(Thread.java:856)
  Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to 
  at 
  </code></pre>
  
  <p>and i also update my log cat</p>