我有以下类,如您所见,它包含一个 EditText、Button 和列表。我希望用户在 EditText 中输入一些文本,单击搜索并显示结果
SQLite database in the list below the button/edittext field.
public class FindOrder extends ListActivity {
private List<String> arr;
private ListView listView;
Button search;
EditText search_field;
String search_val;
SQLiteDatabase db = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.find_order);
// TODO Auto-generated method stub
Log.e("myTag","in func");
search = (Button) findViewById(R.id.search_button);
search_field = (EditText) findViewById(R.id.search_field);
search.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
search_val=search_field.getText().toString();
//getOrder(search_val);
}
});
}
public void getOrder(String val) {
Log.e("myTag","in func");
String query;
String[] s_arr = new String[1];
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
arr = new ArrayList<String>();
db = this.openOrCreateDatabase( "DBname", MODE_PRIVATE, null);
query="SELECT OrderName, OrderLink, DateYear, DateMonth, DateDay, OrderPrice FROM Orders where OrderName=?";
s_arr[0]=val;
Cursor c = db.rawQuery(query, s_arr);
if (c != null ) {
if (c.moveToFirst()) {
do {
arr.add(c.getString(c.getColumnIndex(DatabaseHelper.colName)));
Log.e("myTag",c.getString(c.getColumnIndex(DatabaseHelper.colName)));
} while (c.moveToNext());
}
}
c.close();
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr));
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
listView.setAdapter(adapter);
TextView textview = new TextView(this);
LinearLayout ll2 = (LinearLayout)findViewById(R.id.linearLayout2);
ll2.addView(textview);
}
}
目前,每当我单击主菜单中导致此活动的按钮时,我都会得到一个 FC。我可以在日志中看到的错误之一是:
06-02 10:32:18.387: E/AndroidRuntime(1904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Sagi.MyOrders/com.Sagi.MyOrders.FindOrder}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
连同许多其他错误。但我真的不明白为什么我应该有 .list 属性......
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id = "@+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<LinearLayout
android:id = "@+id/linearLayout2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
谢谢 !