Android how to select id of autocompletetextview inside onitemclick overridden function - android

I have three autoCompleteTextView box as home , work , other .
So in home autocomplete box i get a data from server and select one item and that item i stored to home_latlong string. Similarly i have to get value from other autocomplete work which i am storing that value in another string called home_latlong. Below code shows onItemClick overridden function where i will store home_latlong or work_latlong.
`
ontemClick(AdapterView<?> adapterView, View view,
int position, long id) {
System.out.println("POSITION ="+position);
for (int i = 0; i < latlong.size(); i++) {
if(i==position){
home_latlong=latlong.get(i);
System.out.println("ARRAY"+latlong.get(i));
}
}
}`
So problem is i am not able differenciate when i will store home_latlong and when to store work_latlong. I tried with id of autocompletetextview but it did not help in this function.

I solved it by using anonymous inner class ,
actv1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
actv1 .setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int position,
long id) {
// TODO Auto-generated method stub
for (int i = 0; i < latlong.size(); i++) {
if (i == position) {
home_latlong = latlong.get(i);
System.out.println("ARRAY" + latlong.get(i));
}
}
}
});
Similarly for work_latlong i had another anonymous inner class so i can get which autocompleteview i have clicked.

Check the documentation for AdapterView.OnItemClickListener:
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Parameters
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
You need to use the first parameter (the AdapterView) to identify the AutocompleteTextView.

Related

Android OnItemClickListener long id parameter is not giving field id

I have generated a list of customers. On click this should open edit view to edit the customer. Here the parameter should pass the _id of the row according to stored in the database. But everytime passing it's position in the list. So the edit view is opening wrong data. Please help.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + id);
customerEdit(customerEditUri);
}
});
Answer:
Thank you all. Your comments helped me to solve this. I have created following function inside my CustomerObject class:
#Override
public String toString() {
return this.name;
}
After that created an array of CustomerObject in activity like following:
List<CustomerObject> customers = new ArrayList<CustomerObject>();
Created ArrayAdapter by following:
adapter = new ArrayAdapter<CustomerObject>(this, R.layout.list, R.id.customer_name, customers);
Finally called setOnItemClickListener() like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomerObject custObj = adapter.getItem(position);
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + custObj.pkid);
customerEdit(customerEditUri);
}
});
You will have to set the ID you would like it to return in the adapter, the List View Adapter that you used to bind data to the ListView.
If I am not wrong, the method is in the adapter class under the following method name:
public long getItemId(int position) {
return myitem[position].getId();
}
Returning the appropriate ID will get you the results you wanted.
I believe that the "long id" is not the record id but the internally generated view id.
If you want to get back to the datasource id then you need to use position and something like:
// Assuming datasource is an ArrayAdapter<Customer>
Customer customer = customerAdapter.getItemAtPosition(position);
// then you can do
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + customer.getId());
customerEdit(customerEditUri);
Replace id with position.
Use
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + position);
customerEdit(customerEditUri);
}
})
In my opinion, through the position, you can get row item with adapter's getItem(position).
So, the position mean the data position in the adapter.
For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter.
By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.

Get value of item on OnItemClick Listview

I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...

android get position of clicked item in gridview

as we know using android grid view, we can do the following and get notified when item is clicked:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.
so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?
thanks
Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:
button.setTag(new Integer(position));
Then, in the onClickListener method, with the view that is passed in (the button) you can do:
Integer position = (Integer)view.getTag();
And then handle the position value from there.
EDIT:
It appears the best practice would be to do:
button.setTag(Integer.valueOf(position));
rather than using the Integer constructor.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String a = String.valueOf(position);
Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
}
});

How to get data from object in Android

In the following code, on click of list item I want to fetch variable present inside the object, How I can achive this
Below is code snippet
private void onListViewItemClick() {
// TODO Auto-generated method stub
// item click switch to next activity
listCustomListViewId.setOnItemClickListener(new OnItemClickListener() {
/* on click gets list view item id */
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
// fetching clicked item id
Object o = listCustomListViewId.getItemAtPosition(myItemInt);
Log.i("Victory Item Id:.....", String.valueOf(o);
long strid = (long) (listCustomListViewId
.getItemIdAtPosition((int) mylng));
Log.i("Item Id...#######", String.valueOf(strid));
/* switch on next 'ListItemDeleteUpdateActivity' activity */
Intent intent = new Intent(FeedsActivity.this,
VictoryDetailActivity.class);
intent.putExtra("customElements", o.toString());
startActivity(intent);
}
});
}
I have tried to get data into Object but unable to fetch.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the data associated with selected item
Object item = l.getItemAtPosition(position);
String myitem = item.toString();
edittxt.setText("Selected item is :"+ myitem); // You can Set EditText from Here.
/* switch on next 'VictoryDetailActivity' activity */
Intent intent = new Intent(FeedsActivity.this,VictoryDetailActivity.class);
intent.putExtra("customElements",myitem);
startActivity(intent);
}
Hope this helps you.
On DataSource you have probably an collection of something. myItemInt represent the corresponding item in your collection to the item clicked. Use that.
If you have some views inside the clicked view you need to get, use findViewbyId on myView, like this:
myView.findViewbyId(R.id.myEditTextControl) //demo, use yours
If this answer is not enough for you, post some code from your adapter and tell us more details about what you need to do.
To handle events when the items of ListView is selected. You must override onListItemClick() method. The medthod have 4 parameters:
#Override
protected void onListItemClickonListItemClick(ListView l, View v, int position, long id)
To get information about ItemSelected, you just call getItemAtPosition(position) method to return a object which contains data.
It should be onListItemClick()

How to get the selected item from ListView?

in my Android app I have created a ListView component called myList, and filled it with objects of my own custom type:
class MyClass{
private String displayName;
private String theValue;
... //here constructor, getters, setters and toString() are implemented
}
I used the ArrayAdapter to bound the ArrayList theObjects with myList:
ArrayAdapter<MyClass> adapter=
new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);
This works fine, the list is populated and etc., but when I'm trying to access the selected item, i receive a Null object. I've done this using
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method
}
What seems to be the problem? Thank you
By default, when you click on a ListView item it doesn't change its state to "selected". So, when the event fires and you do:
myList.getSelectedItem();
The method doesn't have anything to return. What you have to do is to use the position and obtain the underlying object by doing:
myList.getItemAtPosition(position);
You are implementing the Click Handler rather than Select Handler. A List by default doesn't suppose to have selection.
What you should change, in your above example, is to
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position);
}
Since the onItemClickLitener() will itself provide you the index of the selected item, you can simply do a getItemAtPosition(i).toString(). The code snippet is given below :-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = listView.getItemAtPosition(i).toString();
Toast.makeText(activity.getApplicationContext(), s, Toast.LENGTH_LONG).show();
adapter.dismiss(); // If you want to close the adapter
}
});
On the method above, the i parameter actually gives you the position of the selected item.
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) adapter.getItem(position);
}
}
Using setOnItemClickListener is the correct answer, but if you have a keyboard you can change selection even with arrows (no click is performed), so, you need to implement also setOnItemSelectedListener :
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
MyObject tmp=(MyObject) adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// your stuff
}
});
In touch mode, there is no focus and no selection. Your UI should use a different type of widget, such as radio buttons, for selection.
The documentation on ListView about this is terrible, just one obscure mention on setSelection.
Though I am using kotlin, the following code answered your question. This return selected item:
val item = myListView.adapter.getItem(i).toString()
The following is the whole selecteditem Listener
myListView.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, i: Int,
id: Long) {
val item = myListView.adapter.getItem(i).toString()
}
})
The code returns the item clicked by its index i as shown in the code
MyClass selItem = (MyClass)
myList.getSelectedItem(); //
You never instantiated your class.

Categories

Resources