Android两个适配器ListView& AutoCompleteTextView

时间:2014-11-21 15:35:08

标签: android android-listview android-arrayadapter autocompletetextview

由于某种原因,我的适配器混合起来, 如果我在allItems.add(item.getString());方法中评论addItemToList(View view),那么我可以将项目添加到我的列表视图中,但它们不会添加到autocompleteTextView(这是正常的)。 如果我取消注释该部分我得到错误:

堆栈跟踪

11-21 16:31:35.649: E/AndroidRuntime(12269): FATAL EXCEPTION: main
11-21 16:31:35.649: E/AndroidRuntime(12269): Process: com.ititup.shoppingaid, PID: 12269
11-21 16:31:35.649: E/AndroidRuntime(12269): java.lang.ClassCastException: java.lang.String cannot be cast to com.ititup.shoppingaid.StringBoolean
11-21 16:31:35.649: E/AndroidRuntime(12269):    at com.ititup.shoppingaid.CreateNewActivity$CustomArrayAdapter.getView(CreateNewActivity.java:170)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.AbsListView.obtainView(AbsListView.java:2306)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.ListView.fillDown(ListView.java:691)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.ListView.fillFromTop(ListView.java:752)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.ListView.layoutChildren(ListView.java:1630)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.AbsListView.onLayout(AbsListView.java:2130)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.View.layout(View.java:14891)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewGroup.layout(ViewGroup.java:4646)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2026)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1783)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1038)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5890)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.Choreographer.doFrame(Choreographer.java:544)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.os.Handler.handleCallback(Handler.java:733)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.os.Looper.loop(Looper.java:136)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at android.app.ActivityThread.main(ActivityThread.java:5118)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at java.lang.reflect.Method.invoke(Method.java:515)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
11-21 16:31:35.649: E/AndroidRuntime(12269):    at dalvik.system.NativeStart.main(Native Method)

所以它以某种方式将item.getString()添加到同一个适配器? 这是我的问题,我不知道如何做到这一点或我如何解决这个问题。

代码:

按钮:onClick action =

public void addItemToList(View view){
        StringBoolean item = new StringBoolean(mItemNameAutoCompleteTextView.getText().toString(),true);
        if(!itemList.contains(item)){
            itemList.add(item);         
            Toast.makeText(getApplicationContext(), item.getString() + getApplicationContext()
                    .getString(R.string.Toast_Item_Added_To_List), Toast.LENGTH_SHORT).show();
            initListView();
        }else {
            Toast.makeText(getApplicationContext(), item.getString() + getApplicationContext()
                    .getString(R.string.Toast_Item_Already_In_List), Toast.LENGTH_SHORT).show();
        }
        mItemNameAutoCompleteTextView.setText("");
        if(!allItems.contains(item.getString())){
            allItems.add(item.getString());
            initAutoCompleteTextView();
        }
    }

班级成员=

    private AutoCompleteTextView mItemNameAutoCompleteTextView;
private ListView mShoppingListPreviewListView;

    private ArrayList<String> allItems = new ArrayList<String>();
    private ArrayList<StringBoolean> itemList = new ArrayList<StringBoolean>();

    private ArrayAdapter<StringBoolean> mListAdapter;
    private ArrayAdapter<String> mAutoCompletionAdapter;

init methodes =

private void initListView(){
        mListAdapter = new CustomArrayAdapter(this, itemList);
        mShoppingListPreviewListView.setAdapter(mListAdapter);
    }
    private void initAutoCompleteTextView(){
        mAutoCompletionAdapter = new ArrayAdapter<String>(this,R.layout.autocompletion_row, allItems);
        mItemNameAutoCompleteTextView.setAdapter(mAutoCompletionAdapter);
    }

0 个答案:

没有答案