0

我有以下类,如您所见,它包含一个 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>

谢谢 !

4

4 回答 4

1

android:id="@android:id/list"当您扩展ListActivity到您的活动时,您的列表视图 ID 必须是。

           <ListView
            android:id="@android:id/list" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
于 2012-06-02T10:44:57.343 回答
1

只需在声明 listview 的 xml 文件中更改此行。

android:id="@android:id/list" 

无需声明列表视图。请尝试以下代码。

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);
    }
}
于 2012-06-02T10:45:42.833 回答
1

更改 xml 文件中的列表视图 IDandroid:id="@android:id/list"

在java代码中,

您已经使用listView = (ListView) findViewById(R.id.listView)
更改为listView = this.getListView()

于 2012-06-02T10:53:29.280 回答
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:

您的活动必须扩展 Activity 而不是 ListActivity。

于 2012-06-02T10:54:16.957 回答