另一个新手XML问题,
我有以下XML支出:
这是从应用程序加载的方式:
一旦输入了名称的serached,它将再次使用相同的布局填充listview:
我确切知道为什么会发生这种情况(因为我将布局设置为'搜索'xml,但只是因为我认为它会显示列表视图)因为我之前使用过listview而没有问题但仅限于他们不拥有编辑文本和按钮。
'搜索'类的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inputSearchName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Name To Search:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/inputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSearchName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
以下代码行显示了如何在布局上设置to和from数组以及游标对象:
package com.example.flybase2;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class search extends ListActivity implements OnClickListener {
Button searchButton;
EditText searchName;
ListView searchedListResults;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchButton = (Button) findViewById(R.id.btnSearchName);
searchName = (EditText) findViewById(R.id.inputName);
searchButton.setOnClickListener(this);
}
@Override
public void onClick(View clickedSearchButton) {
String searchedName = searchName.getText().toString();
searchedListResults = (ListView)findViewById(android.R.id.list);
DBHandler DBsearchRef = new DBHandler(this, null, null);
DBHandler search = new DBHandler(this, null, null);
search.open();
Cursor cursor = search.searchOnName(searchedName);
startManagingCursor(cursor);
@SuppressWarnings("static-access")
String [] from = new String [] {DBsearchRef.KEY_NAME, DBsearchRef.KEY_TEL, DBsearchRef.KEY_EMAIL, DBsearchRef.KEY_COMMENTS};
int [] to = new int [] {R.id.txtNameList, R.id.txtTelList, R.id.txtEmailList, R.id.txtCommentsList};
@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search, cursor, from, to);
searchedListResults.setAdapter(cursorAdapter);
}
}
任何人都可以告诉我如何让listview显示我返回的光标数据,就像在普通的列表视图中一样吗?