OnClickListener无法在listview上运行

时间:2015-04-06 17:59:38

标签: android listview view onclicklistener layout-inflater

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

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

1 个答案:

答案 0 :(得分:0)

我看到您在myList.getItemAtPosition(position);中使用了onClickListener。 据我所知,在最终之前我们不能使用Listener之外的任何视图。因此,如果您想在onClickListener中使用myList,则必须将其设为最终版。