Android clickable list item in a search interface - android

In my android app my search function works, the list gets correctly populated, but the items are not clickable. I'm trying to get that to work. Here's my code, can anyone see why the items are not clickable?
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_TITLE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rTitleTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
// --- END click on list item ----
}
I just want to get the onClick to work, I don't care that it doesn't do anything at the moment. I'll put that functionality in later.
An example of record_2.xml:
<TextView
android:id="#+id/rMakeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Make"
android:textColor="#000000"
android:textSize="16dp"
android:textStyle="bold" >
</TextView>
I've tried this, based on what Sam is pointing out, but the list items are still not clickable.
// --- Click on list item ---
AdapterView<?> clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
// --- END click on list item ----
It seems to me that since there's no reference to the records ListAdapter in the setOnItemClickListener it won't work?

You aren't doing anything in your onItemClick() method, how will you know if it is working (unless you are using the debugger)? Since your code looks fine, try something simple like this:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("Test", "A row was clicked with OnItemClick!");
}
});
Watch your logcat for these messages.

Try this. Remove AdapterView before OnItemClickListener in the method setOnItemClickListener(). Or replace AdapterView with simple View

Related

In Listview onItemClick() listener is not working?

I have added an onItemClick() listener to a listview. The listener is not working.
Here is the code I am working with:`
MyBaseAdapter ma= new MyBaseAdapter(context, myList);
lvDetail.setAdapter(ma);
lvDetail.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ListData ld= myList.get(arg2);
String des=ld.getDescription();
Toast.makeText(context,"description"+des, Toast.LENGTH_LONG).show();
}
});
Since you are using custom adapter, please write listener inside getView of adapter on item of list and perform operation.

List View in android not visible

I'm facing problem with list view, I wrote following code for list view but when i run that code, list view not visible, the code is as follows:
l1=(ListView)findViewById(R.id.listView1);
String[] a1=new String[]{"abc", "bcd","cde","def"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView)arg1).getText(), Toast.LENGTH_LONG).show();
}
});
Here you are missing to set Adapter to listview. Update your code to below code.
l1=(ListView)findViewById(R.id.listView1);
String[] a1=new String[]{"abc", "bcd","cde","def"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView)arg1).getText(), Toast.LENGTH_LONG).show();
}
});
set listadapter first.
l1.setAdapter(ArrayAdapteradapter);
Is your layout properly written? Means ListView visibility.
Your listview doesn't get visible because you have not set the data in your listview using adapter.
set the adapter in your listview as below:
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, a1);
l1.setAdapter(adapter);

Listview Search issue

I have a listview that working perfet.Today I added a search function to the listview using this code
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
The problem: I have 114 items on my listview and the selected item depends on the position, when I do search and press on the item that I found it will return the position 1, I want to return the position of that item before the search.. not the new position, is there anyway to solve it?!
Nope, it will return the position of the item based on the current lenght of your list view/adapter.
Why would it matter?
If you want to start a different activity based on the item clicked, there are many ways to do it. Using the position in the list to choose the activity works ONLY when the positions are fixed, which is not your case, since the user can filter the items.
Instead, start the activity based on the content of the item clicked.
Say for instance, your listview is populated using the following items:
public static final String [] PLANETS = {
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
};
Using the following ArrayAdapter:
mAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, PLANETS);
Then you can edit your OnItemClickListener to start the activity based on the actual planet selected, not the position selected.
private AdapterView.OnItemClickListener mOnPlanetOnItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int iistPosition, long l) {
//Notice I don't get the value from the String array
//Rather, I tell the adapter to give me the text from the selected item.
String planet = mAdapter.getItem(iistPosition);
Intent newActivity = new Intent(Example.this, NextActivity.class);
newActivity.putExtra("planet", planet);
}
};
If you are using a custom adapter it's even easier.
EDIT It is unfortunate that because of the way your mp3 files are named you must depend on the list position to get appropiate file.
In that case you could try a few workarounds, for instance:
If your list view is populated with a set ArrayList or just a List, you could first get the text of the item selected, and then get the actual array position based on the text of the item selected.
Like this:
private AdapterView.OnItemClickListener mOnPlanetOnItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int iistPosition, long l) {
//get the text of the current item
String planet = mAdapter.getItem(iistPosition);
//get the position of this planet in the original unfiltered array
int actualListPosition = PLANETS_ARRAY.indexOf(planet);
//handle the click based on the actualListPosition
}
};
I hope that helps

Android: can not set onItemListener

I am creating a ListView with an adapter in an intent with a dialogtheme:
adapter = new SimpleAdapter(this, test_list, R.layout.list_layout2, from, to);
lv=(ListView)findViewById(R.id.listView1);
Then I am trying to add some listeners, but they aren't triggered in the running app.
onclick =new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("item",arg2 + "");
}
};
onlongclick = new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("longitem",arg2 + "");
return false;
}
};
lv.setOnItemClickListener(onclick);
lv.setOnItemLongClickListener(onlongclick);
Does anybody have an suggestion to get things running correctly?
It seems to be correct, but what i dont understand is why you make variables of your listeners?
You can just set them.
Check out the link here:
http://www.ezzylearning.com/tutorial.aspx?tid=1351248
It uses ArrayAdapter instead of SimpleAdapter and one of the parameters is the items.
It might help you out.
Okay, I found the answer.
In my item layout "R.layout.list_layout2" was a TextView with
android:focusable="true"
android:focusableInTouchMode="true"
Deleting this got my OnItemClickListener work stable.
I've found tutorial that might help you with your problem
Check out here
http://www.androidbegin.com/tutorial/android-simple-listview-tutorial/

I am not able to select a particular row in listview

Hello all
I created a custom listview through my custom adapter..
CustomAdapter adapter = new CustomAdapter(MainPage.this, dbHelper, imgId, title);
adapter.setList(list1);
list1.setAdapter(adapter);
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("hello",arg2+"item clicked ....");
Toast.makeText(MainPage.this, "Kindly enter search parameter!!", Toast.LENGTH_SHORT).show();
}
});
--
Now i am not able to select any item. I dont know why.....
try setting the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.your_layout, null);
convertView.setClickable(true);

Categories

Resources