我有一个listView,其中每个项目都有一个对应的复选框。我也有一个EditText作为使用addTextChangedListener的搜索栏。我做了一个方法,它接受一个TextView和一个LinearLayout(在本例中是一个选项卡),并将TextView添加到该选项卡。我调用这个函数两次,一次在方法setOnItemClickListener中工作正常,一次在方法addTextChangedListener中不起作用。我的代码被编译,应用程序启动,一旦我在搜索栏中输入文本,应用程序意外退出。这里是代码:Prolems在addTextChangedListener中添加一个textView
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);
}
}
通过注释行,我知道代码失败时,我创建新的选项卡。
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.
怎么可以这样解决:在logcat的参照线tab.addView(newTextView)
输出这个错误?谢谢。
谢谢你做的伎俩! – solalito