• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with OnItemClickListener

Hi,
Basically I am following this tutorial - http://www.myandroidsolutions.com/2013/08/04/android-listview-with-searchview/.

The search functionality works fine, however, when I click on a search result it crashes. I cannot work out what the arraylist namelist does in the MyActivity class, and I believe this is what is causing the app to crash.

My version, unlike the tutorial, has more than just one 'name' column. I have also parsed xml into the database. I have highlighted some of the lines to do with what I think is causing the app to crash.

My version of the main class, in which I have commented question marks over, where I don't understand how to adapt.


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";


@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);

//ArrayList nameList = new ArrayList<String>();
//relate listView from java to the created xml
myList = (ListView) findViewById(R.id.listView);
//show list
// defaultAdapter = new MyAdapter(SearchActivity.this, nameList);
//myList.setAdapter(defaultAdapter);

//???????
//for simplicity we will add the same name for 20 times to populate the nameList view
// for (int i = 0; i< 200; i++) {
// nameList.add( + i);
// }


//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();

//???????
//Create the list of names which will be displayed on search
//for (String name : nameList) {
// what
//}


XmlResourceParser xpp = getResources().getXml(R.xml.books);

try {
mDbHelper = new BooksDBAdapter(this);
mDbHelper.open();

// get initial eventType

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() {
//myList.setAdapter(defaultAdapter);
displayResults("");
return false;
}


@override
public boolean onQueryTextChange(String newText) {
// if (!newText.isEmpty()){
displayResults(newText + "*");
// } else {
// myList.setAdapter(defaultAdapter);
///}
return false;
}
@override
public boolean onQueryTextSubmit (String query) {
displayResults(query + "*");
return false;
}

//search and results method
private void displayResults(String query) {

//REMEMBER I ADDED .toString()
Cursor cursor = mDbHelper.searchBooks((query != null ? query.toString() : "@@@@"));

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

//WAS PREVIOUSLY - myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
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);

//fix this line. modify string value
// String searchValue = cursor.getString(cursor.getColumnIndexOrThrow("searchValue"));
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("publish_date"));
String description = cursor.getString(cursor.getColumnIndexOrThrow("description"));


//?????
// Set the default adapter
myList.setAdapter(defaultAdapter);
///?????
// Find the position for the original list by the selected name from search
for (int pos = 0; pos < nameList.size(); pos++) {
if (nameList.get(pos).equals(selectedName)){
position = pos;
break;
}
}
//
// Create a handler. This is necessary because the adapter has just been set on the list again and
// the list might not be finished setting the adapter by the time we perform setSelection.
Handler handler = new Handler();
final int finalPosition = position;
handler.post(new Runnable() {
@override
public void run() {

myList.setSelection(finalPosition);
}
});

searchView.setQuery("",true);
}
}
});
}

}
}
 
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones