0
我正在創建一個搜索應用程序,我將XML文件解析到數據庫中,然後使用searchview以列表視圖返回結果。到那裏,它工作正常。OnClickListener不工作在列表視圖
但是,當我點擊列表中的某個項目時,它不顯示我希望顯示的佈局。我想要的是,當我點擊列表視圖中的一個項目時,它會顯示一個新視圖,該項目應該是book_info.xml佈局文件。
屏幕截圖,之前我點擊:
(http://i.imgur.com/FVg93QH.png)
我點擊之後,它去之前,我做搜索!這是main.xml中
我的代碼如下:
SearchActivity.java
public class SearchActivity extends Activity implements SearchView.OnQueryTextListener,
SearchView.OnCloseListener {
private SearchView searchView;
private ListView myList;
private BooksDBAdapter mDbHelper;
@SuppressWarnings("rawtypes")
// private ArrayList<String> nameList;
// private MyAdapter defaultAdapter;
private static String myTag = "Books";
private TextView authorText;
private TextView titleText;
private TextView genreText;
private TextView priceText;
private TextView publishDateText;
private TextView descriptionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
myList = (ListView) findViewById(R.id.listView);
//set searchview
searchView = (SearchView) findViewById(R.id.searchView);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
mDbHelper = new BooksDBAdapter(this);
mDbHelper.open();
//Clear all names
mDbHelper.deleteAllBooks();
XmlResourceParser xpp = getResources().getXml(R.xml.books);
try {
while (xpp.next() != XmlPullParser.END_TAG) {
if (xpp.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = xpp.getName();
if (name.equals("book")) {
String author = null, title = null, genre = null, price = null, publish_date = null, description = null;
while (xpp.next() != XmlPullParser.END_TAG) {
if (xpp.getEventType() != XmlPullParser.START_TAG) {
continue;
}
name = xpp.getName();
switch (name) {
case "author":
author = readText(xpp);
break;
case "title":
title = readText(xpp);
break;
case "genre":
genre = readText(xpp);
break;
case "price":
price = readText(xpp);
break;
case "publish_date":
publish_date = readText(xpp);
break;
case "description":
description = readText(xpp);
break;
}
}
mDbHelper.createBooks(author,title,genre, price, publish_date, description);
}
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}finally{
Log.d(myTag, "Database created");
}
}
private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mDbHelper != null && mDbHelper == mDbHelper.open()){
mDbHelper.close();
}
}
@Override
public boolean onClose() {
displayResults("");
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
displayResults(newText + "*");
return false;
}
@Override
public boolean onQueryTextSubmit (String query) {
displayResults(query + "*");
return false;
}
private void displayResults(String query) {
Cursor cursor = mDbHelper.searchBooks((query != null ? query : "@@@@"));
if (cursor != null) {
String[] from = new String[] {
BooksDBAdapter.KEY_AUTHOR,
BooksDBAdapter.KEY_TITLE,
BooksDBAdapter.KEY_GENRE,
BooksDBAdapter.KEY_PRICE,
BooksDBAdapter.KEY_PUBLISH_DATE,
BooksDBAdapter.KEY_DESCRIPTION,
};
//view we want to set results
int[] to = new int[] {R.id.author, R.id.title, R.id.genre, R.id.price, R.id.publish_date, R.id.description};
//create cursor adapter. which exposes data from a Cursor to a ListView
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search_results, cursor, from, to, 0);
myList.setAdapter(cursorAdapter);
//listview Click listener for selected results
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) myList.getItemAtPosition(position);
String author = cursor.getString(cursor.getColumnIndexOrThrow("author"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String genre = cursor.getString(cursor.getColumnIndexOrThrow("genre"));
String price = cursor.getString(cursor.getColumnIndexOrThrow("price"));
String publish_date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
String description = cursor.getString(cursor.getColumnIndexOrThrow("description"));
***//Check if the Layout already exists
LinearLayout bookLayout = (LinearLayout)findViewById(R.id.bookLayout);
if(bookLayout == null){
//Inflate the Customer Information View
// LinearLayout leftLayout = (LinearLayout)findViewById(R.id.bookLayout);
View book = getLayoutInflater().inflate(R.layout.book_info, bookLayout, false);
bookLayout.addView(book);
}***
//Get References to the TextViews
authorText = (TextView) findViewById(R.id.xauthor);
titleText = (TextView) findViewById(R.id.xtitle);
genreText = (TextView) findViewById(R.id.xgenre);
priceText = (TextView) findViewById(R.id.xprice);
publishDateText = (TextView) findViewById(R.id.xpublish_date);
descriptionText = (TextView) findViewById(R.id.xdescription);
// Update the parent class's TextView
authorText.setText(author);
titleText.setText(title);
genreText.setText(genre);
priceText.setText(price);
publishDateText.setText(publish_date);
descriptionText.setText(description);
searchView.setQuery("",true);
}
});
}
}
}
我的佈局文件如下:
search.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:id="@+id/main"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerHorizontal="true"
android:layout_below="@+id/searchView"
android:layout_marginTop="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</SearchView>
</RelativeLayout>
search_results.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bookLayout" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:id="@+id/search_results">
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/author_text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/title_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/author"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/genre_text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/genre"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/price_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/publish_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/price"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/publish_date_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/publish_date"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/description_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
book_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xbookLayout" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:id="@+id/book_info">
<TextView
android:id="@+id/xauthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/author_text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="@+id/xtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/title_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/xgenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/author"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/genre_text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="@+id/xprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/genre"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/price_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/xpublish_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/price"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/publish_date_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/xdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/publish_date"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="@string/description_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
我不認爲這會工作。 我已編輯我的帖子,並添加了一個screeen鏡頭。我認爲導致問題的代碼行如下: if(bookLayout == null){ //膨脹客戶信息視圖 // LinearLayout leftLayout =(LinearLayout)findViewById(R.id.bookLayout); View book = getLayoutInflater()。inflate(R.layout.book_info,bookLayout,false); bookLayout.addView(book); bookLayout.addView(book); } – user3460355
您只想顯示一個被點擊的項目。如果你添加了bookLayout,那麼應該選擇多個項目。 –
是的,這是我想要的。我該如何做到這一點,因爲目前我正在用我嘗試的任何方式獲得原始屏幕。 – user3460355