I have an AutoCompleteTextView object with the name myAutoCompleteTextView.
myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//do something
}
}
Now I try to simulate a specific selection of an item on it like that:
myAutoCompleteTextView.setText("some item from the array of this AutoCompleteTextView");
The result is that I see the required item in the AutoCompleteTextView,
but nothing happens. Only the text shown over there.
When the user choose manually the item everythings is fine.
Can someone help me how can I simulate a selection?
I gues u call a function or something in your onItemClick function.
why dont u just call this code outside your listener ? Or am I missing something here?
something like
public void onItemClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
MyObject myObject = parentView.getItemAtPosition(position);
doStuffWithObject(myObject);
}
And u can call the function outside your adapter with any reference.
Is that what you are looking for?
This code as well as place the same order:
codselect.CommandText = "select* from team where nome Like '" + TextBox2.Text + "%'"
Related
im quite a newby to android and java. I created a my own adapter and listView and i want to know how do i find out which view or row was pressed in my listView so i can do a specific task for row was picked (task includes sending data and info to another activity).
I thought that maybe the position variable contains the name of the row clicked so i tried it but when a row is clicked the app crashes
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), position,Toast.LENGTH_SHORT);
}
});
can someone help
it could be that position is an integer, and it's expecting a string, and also the show() needs to be called on your toast to see the message.
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), position+"",Toast.LENGTH_SHORT).show();
}
});
First, you're on the right track regarding finding out which row was clicked.
The error lays in your use of Toast.makeText(). There are two versions of that method:
makeText(Context context, int resId, int duration)
makeText(Context context, CharSequence text, int duration)
Now in your vode you do Toast.makeText(..., position, ...) which invokes the first version of the method (as position is an integer), but because position is not a resource id the app crashes.
So, you have to use the second version and to do that, pass a CharSequence (or String) as second parameter: Toast.makeText(..., position+"", ...)
Btw. to get the actual value of the row, you can use hangarList.getItemAtPosition(position).
The views that make up the rows in the ListView are indexed like an array. The position value is the index of the row or view that was pressed. You can use the position value to look up the information associated with the clicked view.
Your app is crashing because Toast.makeText(Context, int, int) expects a string resource id integer for its second argument. When you give it the position as the id, it can't find that string resource and throws an exception. To display the index of the view that was clicked try:
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(),
"View at position " + position + " clicked.",
Toast.LENGTH_SHORT);
}
}
Application crashes because of your Toast.You should use Toast like
Toast.makeText(context, text, duration).show();
And you can fix it in simple way just like this:
String positionString=Integer.toString(position);
Toast.makeText(getApplicationContext(), positionString,Toast.LENGTH_SHORT).show();
And there is no problem without Toast.As you say which item of listview is clicked?
For controlling which item is clicked You can use position variable of method.
Regards.. Omer
I'm new to this android/java stuff. I have this onItemSelected that will toast what has been selected in the spinner. I want to have the string resumeTableName accessible throughout my entire class that has the value of the selected spinner object. Right now it toasts the selected value however at other places in my class the resumeTableName remains null. I thought the public modifier would make it visible. How do I make this visible, do I use some sort of return?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String resumeTableName = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), resumeTableName,
Toast.LENGTH_LONG).show();
}
Use a static String resumeTableName; declared in your class and
in your onItemSelected write
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
resumeTableName = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), resumeTableName,
Toast.LENGTH_LONG).show();
}
In this way the value of resumeTableName updated by the method will be the same for the whole class.
If You are new in Android programming there is a nice solution for the problems with visibility of variables.
You have to create class in your package:
public class GlobalVar extends Application{
private int dummy1;
public int getDummy1() {
return dummy1;
}
public void setDummy1(int dummy1) {
this.dummy1 = dummy1;
}
}
then in any place in Your application You can get/set this data, by using:
GlobalVar gV = (GlobalVar)GetApplicationContext();
and then You just modify them by using Getters/Setters or any public methods from this class. I think it's very good solution for set of variables that You use often from different places of code.
Sorry if this is a weird question, I just really don't understand how to get this working.
basically, I want a behavior so that when you click one of my gridview items, it highlights red, This works fine, but I need it to only have 2 highlighted letters at a time from the click method.
Outside this method is
public void onItemClick(AdapterView<?>parent, View v, int position, long id)
{
//selection.setText(Words[position]);
}
This is inside my method which gets fired
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
Toast.makeText(getApplicationContext(), ""+arg2,Toast.LENGTH_SHORT).show();
((TextView) arg1).setTextColor(getResources().getColor(android.R.color.holo_red_light));
((TextView) arg1).setTextSize(9);
counter++;
if(counter == 2)
{
//this needs to select everything in the gridview and set to white
}
}
Does anyone have any ideas how I would go about it ( if it's even possible)
Sorted it.
if(counter == 3)
{
gridView.setAdapter(adapter);
// do the check if its a word or not
counter = 0;
}
works like a charm
As the title suggests. I've done a bit of research and tried Jackson which seems like it should have worked but I had ridiculous amount of errors while trying to use it so that seemed a no go.
Basically my problem is this, I have my list view populated by an adapator as so:
friendsArrayAdapter = new FriendsArrayAdapter(
NetPlay.this, R.layout.rowlayout, friends);
listView.setAdapter(friendsArrayAdapter);
friendsArrayAdapter.notifyDataSetChanged();
and Then I want to handle onClick events within this listview, to copy the contents of that list view to a variable:
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Object o = listView.getItemAtPosition(position);
Object o = arg0.getItemAtPosition(position);
//mText.setText(o);
}
});
Now this all works fine, but the value returned is the actual Object ID/Reference
Shows as something#Friends.21312. What I want is the actual contents which should be a Name + ID.
Is this even possible or do I need to redo my whole list view population?
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//friends is the array that is given to the Adapter.
//given friends = Friend[]
Friend f = friends[position];
f.getName();
f.getID();
}
This is only a guess, please post your whole code for better help!
I am working with Spinner, cursors and adapters.
I want to setup a click listener for the spinner so that once a user selects an item from the spinner it gets the current selected item and then carrys out some other tasks ( all this extra code is fine, its just 1 problem I am having).... It kind of works, however, once I declare the setOnItemSelectedListener callback, since the cursor has already been populated, the event is fired as soon as the app launches.
I guess I need a way to define the cursor without selecting an initial item so that the event doesnt fire (since an item will not be selected). Or is there another better way to achieve this?
Basically, as it stands, once the app loads the setOnItemSelectedListener function is firing because the cursor is being populated ( i think). Moreover, ignoreing the fact that the event is firing too soon, if I then select the -same- item in the spinner, it doesnt fire the event sincethe item didnt change. SHould I be using a different callback instead of setonitemslectedlistener? Here is the code I have so far.
c = db.getallrecents();
startManagingCursor(c);
busnumspinner = (Spinner) findViewById(R.id.Spinner01);
SimpleCursorAdapter spinneradapter = new SimpleCursorAdapter(this,
R.layout.lvlayout, c, spincol, spinto);
busnumspinner.setAdapter(spinneradapter);
busnumspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String spinnerString = null;
Cursor cc = (Cursor)(busnumspinner.getSelectedItem());
if (cc != null) {
spinnerString = cc.getString(
cc.getColumnIndex("busnum"));
text = spinnerString;
}
showDialog(DATE_DIALOG_ID);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
This has already been discussed in this question. Look there, though it has a similar answer like the one given by blindstuff.
EDIT:
If the onItemSelectedListener is not firing when you need it, then you probably need a onClickListener in eachtext item of the droplist and get in there the current position of the selected item of the spinner. The problem is that as it is said here spinner don't support this event, but maybe you can get it by doing something similar to the explained in this stackoverflow question. I haven't tried it so I'm not sure it will work.
Use a boolean flag to ignore the first time it gets selected by the system, its not a pretty solution, but i've struggled with this a couple of times, and never found a better solution.
you can add first item of spinner by default value like selectvalues and check its position in onitemselected listener, if it's zero position then dont enter in the loop greater than 0 then enter in the method
see the example
busnumspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
position, long id) {
if(position!=0)
{
String spinnerString = null;
Cursor cc = (Cursor)(busnumspinner.getSelectedItem());
if (cc != null) {
spinnerString = cc.getString(
cc.getColumnIndex("busnum"));
text = spinnerString;
}
}
So this is not run the default value
Try this:
Extend your SimpleCursorAdapter, override bindView() and set OnClickListener for the row view.
This will overcome both issues: You do not get the initial call, and you get each selection click (inc. re-selection)
Let me know if you need example code.
EDIT: Code example:
protected class NoteAdapter extends SimpleCursorAdapter {
// Constructor
public NoteAdapter(Context context, Cursor c) {
super(context, R.layout.etb_items_strip_list_item, c, fromNote, toNote);
}
// This is where the actual binding of a cursor to view happens
#Override
public void bindView(View row, Context context, Cursor cursor) {
super.bindView(row, context, cursor);
// Save id
Long id = cursor.getLong(cursor.getColumnIndex("_id"));
row.setTag(id);
// Set callback
row.setOnClickListener(row_OnClick);
}
// Callback: Item Click
OnClickListener row_OnClick = new OnClickListener(){
public void onClick(View v) {
Long id = (Long) v.getTag();
}
};
}