2

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

Activity

public class ConnectActivity extends Activity {

    // unsorted list items
    List<Company> mItems;

    // array list to store section positions
    ArrayList<Integer> mListSectionPos;

    // array list to store listView data
    ArrayList<String> 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<Integer>();
        mListItems = new ArrayList<String>();

        // for handling configuration change
        if (savedInstanceState != null) {
            mListItems = savedInstanceState.getStringArrayList("mListItems");
            mListSectionPos = savedInstanceState
                    .getIntegerArrayList("mListSectionPos");

            if (mListItems != null && mListItems.size() > 0
                    && mListSectionPos != null && mListSectionPos.size() > 0) {
                setListAdaptor();
            }

            String constraint = savedInstanceState.getString("constraint");
            if (constraint != null && constraint.length() > 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<?> 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 && 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) {

        }
    };

Then i am entered my listFilter. This is the filter class

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 && constraint.toString().length() > 0) {
                ArrayList<String> filterItems = new ArrayList<String>();

                synchronized (this) {
                    for (int i = 0; i < 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<Company> filtered = (List<Company>) 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 && constraint.length() > 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<List<Company>, Void, Void> {

        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<Company>... params) {
            mListItems.clear();
            mListSectionPos.clear();
            List<Company> items = params[0];
            if (mItems.size() > 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 < 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() <= 0) {
                    showEmptyText(mListView, mLoadingView, mEmptyView);
                } else {
                    setListAdaptor();
                    showContent(mListView, mLoadingView, mEmptyView);
                }
            }
            super.onPostExecute(result);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mListItems != null && mListItems.size() > 0) {
            outState.putStringArrayList("mListItems", mListItems);
        }
        if (mListSectionPos != null && mListSectionPos.size() > 0) {
            outState.putIntegerArrayList("mListSectionPos", mListSectionPos);
        }
        String searchText = mSearchView.getText().toString();
        if (searchText != null && searchText.length() > 0) {
            outState.putString("constraint", searchText);
        }
        super.onSaveInstanceState(outState);
    }

}

Thanks in advance.......

this code is adopted by Mr. Bhavy mehta on github

Here is my log cat

     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 

and i also update my log cat

  • 1
    can you please be more specific, which method and which line is causing this issue? – ajay.patel Sep 08 '14 at 15:43
  • 1
    this line i got error –  Sep 08 '14 at 15:46
  • 1
    @Siva i have log cat but i didnot print because i dont have reputation then i cant post image if u gave ur mailid i send u personally that log cat –  Sep 08 '14 at 15:47
  • you can just copy logcat and paste as text here – Siva Sep 08 '14 at 15:50
  • 1
    @Siva its my home system and in this system i am not using eclipse –  Sep 08 '14 at 15:53
  • `items.get(i).companyName` what is company name? a variable? is it public? why not getter? – Alexander Zhak Sep 08 '14 at 15:53
  • 1
    @AlexanderZhak its alredy define in a samle data –  Sep 08 '14 at 15:54
  • 1
    @Siva I UPDATE my code with logcat hope its help you –  Sep 08 '14 at 16:12
  • what is the company name? what datatype it holds? – Siva Sep 08 '14 at 16:14
  • 1
    @Siva company is related by table @Column(name = "Company_Name") public String companyName; –  Sep 08 '14 at 16:20
  • is it possible to `mItems.get(i).companyName.toString()`? – Alexander Zhak Sep 08 '14 at 16:26
  • I am unable to understand completly but one thing I can sugest is `String current_item = String.valueOf()tems.get(i).companyName;` – Siva Sep 08 '14 at 16:27
  • 1
    i think its a object to string problem –  Sep 08 '14 at 16:30
  • 1
    But when i first time invalidate my list its going perfect..but when i go to search box its gave me error –  Sep 08 '14 at 16:31
  • @Shekhar so you need to convert to string try using `String current_item = String.valueOf(tems.get(i).companyName);` – Siva Sep 08 '14 at 16:34
  • 1
    @Siva i think its work may be....if its work then i update it. –  Sep 08 '14 at 16:36
  • 1
    @Siva i have still that error please help me and also this not work –  Sep 12 '14 at 05:34
  • 1
    @AlexanderZhak its not working please help me –  Sep 12 '14 at 05:35
  • @Shekhar what is this?`com.aspeage.quikonnect.entity.Company` – Siva Sep 12 '14 at 06:11
  • 1
    @Siva @Table(name = "Companies") in this @Column(name = "Company_Name") public String companyName; –  Sep 12 '14 at 06:31
  • @Shekhar what is the reason behind using `.companyname` this seems to be a column in table.... – Siva Sep 12 '14 at 06:48
  • 1
    @Siva for future using database –  Sep 12 '14 at 06:49
  • @Shekhar what output are you expecting from this line `items.get(i).companyName` also what is the outpur of `items.get(i)` and you told for future use how does this help database in future what is your entire thought of using database?.... your coding seems to be very complex... until and unless you debug from step1 you can't trace the issue... to be honest I didn't understand your statement of future use from that statement. – Siva Sep 12 '14 at 06:53
  • 1
    @Siva because currently i am using locally save sample data as i define up side using that. –  Sep 12 '14 at 07:16

3 Answers3

1

The problem here is in performFiltering.

You have defined filterItems in that method as

ArrayList<String> filterItems = new ArrayList<String>()

and you later add the filteritems to the filter result, like this:

                result.count = filterItems.size();
                result.values = filterItems;

(where result is of type FilterResults).

However, later you cast the value from the filterResults to be of type List < Company > in publishResults:

List<Company> filtered = (List<Company>) results.values;

This causes a runtime exception in doInBackground, when the runtime system discovers that the object that you passed it, purporting to be a List < Company > is actually a List < String >

The solution to your problem is to change filterItems to be a List of Company, not String, and to add the mItems[i] element to it when the company name matches the constraintStr in the loop inside performFiltering.

For instance:

    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();
        FilterResults result = new FilterResults();

        if (constraint != null && constraint.toString().length() > 0) {
            ArrayList<Company> filterItems = new ArrayList<Company>();

            synchronized (this) {
                for (int i = 0; i < mItems.size(); i++) {
                    Company company=mItems.get(i);
                    String companyName= company.companyName;
                    if (companyName.toLowerCase().startsWith(constraintStr)) {
                        filterItems.add(company);
                    }
                }
                result.count = filterItems.size();
                result.values = filterItems;
            }
        } else {
            synchronized (this) {
                result.count = mItems.size();
                result.values = mItems;
            }
        }
        return result;
    }
Breandán Dalton
  • 1,619
  • 15
  • 24
  • 1
    its not work when i do the List filterItems =new ArrayList() then i am add filterItems.add(item1) on that place add gave me that error The method add(Company) in the type List is not applicable for the arguments (Object) –  Sep 12 '14 at 05:24
  • 1
    please help me i am not still working on 6 days but error is not solve –  Sep 12 '14 at 05:25
  • In performFiltering, you need to find the Company in mItems whose company name is the same as the constraint string. Then you add that Company to your filterItems, which must be a List – Breandán Dalton Sep 12 '14 at 07:37
  • I edited my answer to give you an example of how you could rewrite performFiltering – Breandán Dalton Sep 12 '14 at 07:48
1
String constraintStr = constraint.toString().toLowerCase();
                 FilterResults result  = new FilterResults(); ;

                if (constraint != null && constraint.toString().length() > 0) {
                    ArrayList<Company> filterItems = new ArrayList<Company>();

                    synchronized (this) {
                        for (int i = 0; i < mItems.size(); i++) {
                            Company company=mItems.get(i);
                            String item= company.companyName;
                            if (item.toLowerCase().startsWith(constraintStr)) {
                                filterItems.add(company);
                            }
                        }
                        result.count = filterItems.size();
                        result.values = filterItems;
                    }
                } else {
                    synchronized (this) {
                        result.count = mItems.size();
                        result.values = mItems;
                    }
                }
                return result;

You use that

Shekhar
  • 151
  • 1
  • 2
  • 20
1

You have basic issue that issue is only for object not converted in to string if you do it then its done