Android: can not set onItemListener - android

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/

Related

OnClick Listner of images in Grid View Android

I have a Grid View that have 4 images .So i want to identify which image has been clicked so that corresponding to that i can start a new activity .
So please help me how can i get this
I have tried this
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
getView can be used for this purpose.for this in getView find View and apply onClicklistner their.to make all views clickable you need to setFocusable(false) on all focusable views.
I have got the solution
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
links[arg2], Toast.LENGTH_SHORT).show();
}
});
int arg2 in the OnItemClick method specifies the position.Using that you can get the item clicked.

OnItemClickListener() - Android listview

I was developing a sample app in Android and came across below code, I am just wondering what part of Java,OOPS concept is used when this line is used , can any one please explain in detail this kind of declaration ? why its being declaraed like this
mainlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
} });
This is called an Anonymous class, in this example used as part of an Observer Pattern.
OnItemClickListener is an interface.
The ListView will save the interface implementation you gave (your new OnItemClick...).
And when the ListView detects a user click on item it will call the callback you gave (your implementation) via mOnItemClickCallback.onItemClick(params...)
In addition to what "metter" has stated,
this
mainlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
} });
can also be written as
mainlistview.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// do the stuff for item click
}
Have a look at observer and command pattern. These pattern help you to understand the concept.

Android clickable list item in a search interface

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

ItemClickListener not working

I have problem with onItemClickListener, its not working for my code
My code is
listView = (ListView)findViewById(R.id.listView1);
CustomAadpter adapter = new CustomAadpter(this,R.layout.file_view_layout,R.id.contact_name,names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("view clicked is"+arg1);
}
});
Toast.makeText(context,"Position is"+ arg2,Toast.LENGTH_LONG).show();
Can be used if you want to show the postion as well. Hope this helps.
try
Toast.makeText(context,"message",Toast.LENGTH_LONG).show();

Android - How to add selection from Spinner to EditText

what I'm trying to do is make a selection from a spinner in android and then whatever is selected to be added to an edittext box. The code I have so far is this...
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//add some code here
}
);
Problem is this seems to be run even before the spinner is selected so it always sets my edittext to "". Ideally I would like to have it set the text to the selection made in the spinner. So, anyone have any ideas?
At startup, your spinner will get its defaultvalue, that counts as a selection.
Do a boolean FirstTime or something like that.
You probably initialize your spinner from some array or something?
The function actually looks like this
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id);
So just use the position variable
{
edittext.setText(myArray[position]);
}
You can use the getItem method in the adapter to get the object that is shown. Like this:
onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText((String) adapter.getItem(position));
}

Categories

Resources