2015-04-06 94 views
0

我正在创建一个搜索应用程序,我将XML文件解析到数据库中,然后使用searchview以列表视图返回结果。到那里,它工作正常。OnClickListener不工作在列表视图

但是,当我点击列表中的某个项目时,它不显示我希望显示的布局。我想要的是,当我点击列表视图中的一个项目时,它会显示一个新视图,该项目应该是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> 

回答

0

正如我看到你在onClickListener使用myList.getItemAtPosition(position);。 据我所知,我们不能使用听众以外的任何视图,直到它是最终的。所以如果你想在onClickListener内使用myList,你必须做出最终决定。

+0

我不认为这会工作。 我已编辑我的帖子,并添加了一个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

+0

您只想显示一个被点击的项目。如果你添加了bookLayout,那么应该选择多个项目。 –

+0

是的,这是我想要的。我该如何做到这一点,因为目前我正在用我尝试的任何方式获得原始屏幕。 – user3460355