我有以下类,你可以看到它包含一个EditText,Button和list。 我希望用户在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>
谢谢!
答案 0 :(得分:1)
将android:id="@android:id/list"
扩展到您的活动时,您的列表视图ID必须为ListActivity
。
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
答案 1 :(得分:1)
只需在声明listview的xml文件中更改此行。
android:id="@android:id/list"
无需声明listview。请尝试以下代码。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
请查看以下示例。
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
答案 2 :(得分:1)
更改xml文件android:id="@android:id/list"
,在java代码中,
您使用了listView = (ListView) findViewById(R.id.listView)
将其更改为listView = this.getListView()
答案 3 :(得分:1)
<强>解决方法1:强> 在xml文件中更改listview的id
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
并在你的代码中更改此行listView =(ListView)findViewById(R.id.listView);如
listView = (ListView) findViewById(R.id.list);
<强>溶液2:强>
您的活动必须扩展活动而不是ListActivity。