我正在尝试创建一个顶部带有搜索框的简单列表视图。Hamy在此线程中提供了出色的教程。但是,我在此结束时遇到了一个问题 - 有一行引用了布局 XML 文件:
filterText = (EditText) findViewById(R.id.search_box);
Eclipse 抛出一个错误,因为“id”不能被解析(或者不是一个字段)。我的布局 XML 文件包含以下内容:
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+building_list/search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
我尝试了一些明显的修复,例如更换线路
<EditText android:id="@+building_list/search_box"
和:
<EditText android:id="@+id/search_box"
...这消除了错误,但如果我尝试在文本框中输入任何内容,应用程序就会崩溃。
我哪里出错了?为了完整起见,这是我的 java 文件的内容:
package mjd.listview.test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.id;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import au.com.bytecode.opencsv.CSVReader;
public class ListProjectActivity extends ListActivity {
private EditText filterText = null; // used in filtering the list
ArrayAdapter<String> adapter = null; // used in filtering the list
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview); // load the layout file 'filterable_listview.xml'
filterText = (EditText) findViewById(R.id.search_box); // used in filtering the list
filterText.addTextChangedListener(filterTextWatcher); // used in filtering the list
String next[] = {}; // 'next' is used to iterate through dictionaryFile
List<String[]> dictionaryArray = new ArrayList<String[]>();
try {
CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("dictionaryFile.csv")));
while((next = reader.readNext()) != null) {
dictionaryArray.add(next);
}
} catch (IOException e) {
e.printStackTrace();
}
String[] terms = dictionaryArray.get(0); // load terms from dictionaryArray
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, terms));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() { // when an item is clicked on...
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog alertDialog = new AlertDialog.Builder(ListProjectActivity.this).create(); // create a dialog box in memory
alertDialog.setTitle(((TextView) view).getText()); // set title of dialog box to term name
alertDialog.setMessage("Put definition here"); // set dialog box message to term definition
alertDialog.show(); // actually display the dialog box
}
});
}
// The whole block below is used in filtering the list
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
}
编辑:我怀疑这与我后来使用不同的列表布局有关(见下面的行)
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, terms));
也许“list_item”需要类似于“android:id/list”?但我无法获得正确的语法以使其正确引用......有什么想法吗?