Я создаю приложение поиска, в котором я разбираю XML-файл в базе данных, а затем использую searchview для возврата результатов в listview. До сих пор он работает нормально.OnClickListener не работает в listview
Однако, когда я нажимаю на элемент в списке, он не отображает макет, который я хочу показать. Я хочу, когда я нажимаю элемент в списке, он показывает новое представление только с этим элементом, который должен быть файлом макета book_info.xml.
Я пытаюсь следовать этот учебник BTW: http://www.mysamplecode.com/2011/11/android-searchview-using-sqlite-fts3.html?showComment=1418227703572#c6915997130457985000
снимок экрана, прежде, чем я нажму:
(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 shot. Строки кода, которые, по моему мнению, вызывают проблему: LinearLayout bookLayout = (LinearLayout) findViewById (R.id.bookLayout); if (bookLayout == null) { // Надуйте информацию о информации о клиенте // LinearLayout leftLayout = (LinearLayout) findViewById (R.id.bookLayout); Просмотреть книгу = getLayoutInflater(). Inflate (R.layout.book_info, bookLayout, false); книгаLayout.addView (книга); } – user3460355
Вы хотите показать только один элемент, который нажал. и если вы добавите в bookLayout, тогда нужно выбрать несколько элементов. –
Да, это то, чего я хочу. Как именно я это сделаю, поскольку в настоящее время я получаю исходный экран с тем, что я пытаюсь. – user3460355