在addTextChangedListener中添加textView时出现问题

时间:2014-03-27 11:23:08

标签: android

我有一个listView,其中每个项目都有一个相应的复选框。我还有一个EditText,它使用addTextChangedListener作为搜索栏。我创建了一个采用TextView和LinearLayout(在本例中为选项卡)的方法,并将TextView添加到选项卡。我调用这个函数两次,一次在方法setOnItemClickListener中工作正常,一次在方法addTextChangedListener不起作用。我的代码被编译,应用程序启动,一旦我在搜索栏中输入文本,应用程序就会意外退出。这是代码:

public class myList extends Activity {

// Tab Host
TabHost th;
LinearLayout tab1, tab2, tab3;

// List
ListView myListView;
String string_list[] = { "String1", String2", "String3", "String4", "String5", String6", "String7", "String8" };
ArrayAdapter<String> adapter;
EditText searchBar;

// TextView
TextView tab3TextView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // XML File
    setContentView(R.layout.main_list);

    // Sort Array
    Arrays.sort(string_list, String.CASE_INSENSITIVE_ORDER);

    // TabHost
    initializeTab();

    // EditText
    searchBar     = (EditText) findViewById(R.id.searchBar);
    tab3TextView1 = (TextView) findViewById (R.id.tab3TextView1);

    // ListView
    myListView = (ListView) findViewById(R.id.listView1);
    adapter    = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, string_list);
    myListView.setAdapter(adapter);
    myListView.setItemsCanFocus(false);
    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("InlinedApi")
        @SuppressWarnings("deprecation")
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3) {
            TextView temp = (TextView) view;

            if (myListView.isItemChecked(position)) {
                TextView newTextView = new TextView(myList.this);
                newTextView.setText(temp.getText());

                // Checks for duplicate TextView
                boolean duplicate = false;
                for(int i = 0; i < tab1.getChildCount(); i++){
                    temp = (TextView) tab1.getChildAt(i);
                    if(newTextView.getText().equals(temp.getText())){
                        duplicate = true;
                    }
                }

                // If no duplicate, add TextView
                if (duplicate == false){
                    addTextView(newTextView, tab1);
                }
            } else {
                TextView newTV = new TextView(myList.this);
                newTV.setText(temp.getText());

                for (int i = 0; i < tab1.getChildCount(); i++) {
                    temp = (TextView) tab1.getChildAt(i);

                    if (newTV.getText().equals(temp.getText())) {
                        tab1.removeView(temp);
                        break;
                    }
                }
            }
        };
    });

    // Search Bar
    searchBar.addTextChangedListener(new TextWatcher() {
        TextView newTextView = new TextView(myList.this);
        Map <String, Boolean> myMap;
        ListView tempList;
        TextView tempText;

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
            tempText = new TextView(myList.this);
            myMap  = new HashMap<String, Boolean>();

            tempList = new ListView(myList.this);
            tempList = myListView;
            tempList.setFilterText(arg0.toString());

            for(int j = 0; j < tempList.getChildCount()-1; j++){
                tempText = (TextView) tempList.getChildAt(j);
                addTextView(tempText, tab3);
            }
        }
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
            // When user changes the text
            myList.this.adapter.getFilter().filter(arg0);
        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
}


//Initialize tabs
private void initializeTab() {
    // Tab Host
    th = (TabHost) findViewById(R.id.tabhost);
    th.setup();

    // Linear Layout
    tab1 = (LinearLayout) findViewById(R.id.tab1);
    tab2 = (LinearLayout) findViewById(R.id.tab2);
    tab3 = (LinearLayout) findViewById(R.id.tab3);

    // Tab1
    TabSpec spec1 = th.newTabSpec("tag1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Favorites");
    th.addTab(spec1);

    // Tab2
    TabSpec spec2 = th.newTabSpec("tag2");
    spec2.setContent(R.id.tab2);
    spec2.setIndicator("My List");
    th.addTab(spec2);

    // Tab3
    TabSpec spec3 = th.newTabSpec("tag3");
    spec3.setContent(R.id.tab3);
    spec3.setIndicator("Upcoming Release");
    th.addTab(spec3);
}

// Add a TextView
@SuppressWarnings("deprecation")
private void addTextView(TextView newTextView, LinearLayout tab){
    newTextView.setTextAppearance(myList.this,
            android.R.style.TextAppearance_Holo_Large);
    newTextView
            .setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
    tab.addView(newTextView);
}
}

通过注释掉行,我知道在创建新标签时代码失败了。 logCat在引用行tab.addView(newTextView)时输出此错误:

03-27 13:12:57.340: E/AndroidRuntime(32500): FATAL EXCEPTION: main
03-27 13:12:57.340: E/AndroidRuntime(32500): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

如何解决这个问题?谢谢。

2 个答案:

答案 0 :(得分:1)

您可以通过创建TextView

的新实例来解决此问题

由于您从TextView获得了ListView,因此TextView已有父级,无法添加到其他ViewGroup

你应该改变

tempList = new ListView(myList.this);
        tempList = myListView;
        tempList.setFilterText(arg0.toString());

        for(int j = 0; j < tempList.getChildCount()-1; j++){
            tempText = (TextView) tempList.getChildAt(j);
            addTextView(tempText, tab3);
        }

进入

tempList = new ListView(myList.this);
        tempList = myListView;
        tempList.setFilterText(arg0.toString());

        for(int j = 0; j < tempList.getChildCount()-1; j++){
            tempText = (TextView) tempList.getChildAt(j);
            TextView newTextView = new TextView(myList.this);
            newTextView.setText(tempText.getText());

            addTextView(newTextView, tab3);
        }

我认为应该做的伎俩

答案 1 :(得分:0)

您正在尝试将另一个视图的子元素添加到TAB。您无法添加另一个子项。创建类似的textview并将其添加到TAB或从listview中删除该textview并将其添加到TAB。

在那个循环中 添加以下代码。

       for(int j = 0; j < tempList.getChildCount()-1; j++){
          if( tempList.getChildAt(j) instanceOf TextView){
        tempText = (TextView) tempList.getChildAt(j);
        TextView tempView = new TextView(yourclass.this);
         tempView.setText(tempText.getText());
        addTextView(tempView , tab3);
          }
    }